209 lines
6.1 KiB
C#
209 lines
6.1 KiB
C#
namespace Bts.ArithmeticExpressions;
|
|
|
|
using System.Collections;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
class EvaluationEnvironment : IDictionary<string, object?>, IReadOnlyDictionary<string, object?> {
|
|
IDictionary<string, object?> outerEnv;
|
|
IDictionary<string, object?> innerEnv = new Dictionary<string, object?>();
|
|
int? count;
|
|
|
|
public EvaluationEnvironment(IDictionary<string, object?> initial) =>
|
|
outerEnv = initial;
|
|
|
|
public object? this[string key] {
|
|
get {
|
|
if (innerEnv.TryGetValue(key, out var rv)) {
|
|
return rv;
|
|
}
|
|
if (outerEnv.TryGetValue(key, out rv)) {
|
|
return rv;
|
|
}
|
|
throw new Exception($"Undefined identifier '{key}'");
|
|
}
|
|
set {
|
|
if (count.HasValue && !ContainsKey(key)) {
|
|
count = count.Value + 1;
|
|
innerEnv.Add(key, value);
|
|
return;
|
|
}
|
|
innerEnv[key] = value;
|
|
}
|
|
}
|
|
|
|
public IDictionary<string, object?> Result =>
|
|
innerEnv;
|
|
|
|
public ICollection<string> Keys =>
|
|
new KeyCollection(outerEnv, innerEnv);
|
|
|
|
public ICollection<object?> Values =>
|
|
new ValueCollection(outerEnv, innerEnv);
|
|
|
|
public int Count =>
|
|
count ??= countKeys(outerEnv, innerEnv);
|
|
|
|
public bool IsReadOnly => throw new NotImplementedException();
|
|
|
|
public bool ContainsKey(string key) =>
|
|
innerEnv.ContainsKey(key) || outerEnv.ContainsKey(key);
|
|
|
|
public bool TryGetValue(string key, [MaybeNullWhen(false)] out object? value) =>
|
|
innerEnv.TryGetValue(key, out value) || outerEnv.TryGetValue(key, out value);
|
|
|
|
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() {
|
|
foreach (var kv in innerEnv) {
|
|
yield return kv;
|
|
}
|
|
foreach (var kv in outerEnv.Except()) {
|
|
if (!innerEnv.ContainsKey(kv.Key)) {
|
|
yield return kv;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Add(string key, object? value) {
|
|
if (ContainsKey(key)) {
|
|
throw new ArgumentException(
|
|
$"An element with the same key ({key}) already exists", nameof(key));
|
|
}
|
|
innerEnv.Add(key, value);
|
|
}
|
|
|
|
public void Add(KeyValuePair<string, object?> item) =>
|
|
Add(item.Key, item.Value);
|
|
|
|
public void Clear() =>
|
|
throw new NotSupportedException();
|
|
|
|
public bool Contains(KeyValuePair<string, object?> item) =>
|
|
TryGetValue(item.Key, out var v) && Equals(v, item.Value);
|
|
|
|
public void CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex) {
|
|
foreach (var kv in innerEnv) {
|
|
array[arrayIndex++] = kv;
|
|
}
|
|
foreach (var kv in outerEnv) {
|
|
if (!innerEnv.ContainsKey(kv.Key)) {
|
|
array[arrayIndex++] = kv;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool Remove(string key) =>
|
|
throw new NotSupportedException();
|
|
|
|
public bool Remove(KeyValuePair<string, object?> item) =>
|
|
throw new NotSupportedException();
|
|
|
|
static int countKeys(IDictionary<string, object?> d1, IDictionary<string, object?> d2) =>
|
|
d1.Count > d2.Count ?
|
|
d1.Count + d2.Keys.WhereNot(d1.ContainsKey).Count() :
|
|
d2.Count + d1.Keys.WhereNot(d2.ContainsKey).Count();
|
|
|
|
IEnumerable<string> IReadOnlyDictionary<string, object?>.Keys => Keys;
|
|
IEnumerable<object?> IReadOnlyDictionary<string, object?>.Values => Values;
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
|
|
class KeyCollection : ICollection<string> {
|
|
readonly IDictionary<string, object?> outerEnv;
|
|
readonly IDictionary<string, object?> innerEnv;
|
|
|
|
public KeyCollection(
|
|
IDictionary<string, object?> outerEnv, IDictionary<string, object?> innerEnv) {
|
|
this.outerEnv = outerEnv;
|
|
this.innerEnv = innerEnv;
|
|
}
|
|
|
|
public int Count =>
|
|
countKeys(outerEnv, innerEnv);
|
|
|
|
public bool IsReadOnly => true;
|
|
|
|
public void Add(string item) =>
|
|
throw new NotSupportedException();
|
|
|
|
public void Clear() =>
|
|
throw new NotSupportedException();
|
|
|
|
public bool Contains(string item) =>
|
|
innerEnv.ContainsKey(item) || outerEnv.ContainsKey(item);
|
|
|
|
public void CopyTo(string[] array, int arrayIndex) {
|
|
foreach (var k in this) {
|
|
array[arrayIndex++] = k;
|
|
}
|
|
}
|
|
|
|
public IEnumerator<string> GetEnumerator() {
|
|
foreach (var k in innerEnv.Keys) {
|
|
yield return k;
|
|
}
|
|
foreach (var k in outerEnv.Keys) {
|
|
if (!innerEnv.ContainsKey(k)) {
|
|
yield return k;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool Remove(string item) =>
|
|
throw new NotSupportedException();
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() =>
|
|
GetEnumerator();
|
|
}
|
|
|
|
class ValueCollection : ICollection<object?> {
|
|
readonly IDictionary<string, object?> outerEnv;
|
|
readonly IDictionary<string, object?> innerEnv;
|
|
|
|
public ValueCollection(
|
|
IDictionary<string, object?> outerEnv, IDictionary<string, object?> innerEnv) {
|
|
this.outerEnv = outerEnv;
|
|
this.innerEnv = innerEnv;
|
|
}
|
|
|
|
public int Count =>
|
|
// Not a typo (obv, numValues == numKeys)
|
|
countKeys(outerEnv, innerEnv);
|
|
|
|
public bool IsReadOnly => true;
|
|
|
|
public void Add(object? item) =>
|
|
throw new NotSupportedException();
|
|
|
|
public void Clear() =>
|
|
throw new NotSupportedException();
|
|
|
|
public bool Contains(object? item) =>
|
|
innerEnv.Values.Contains(item) ||
|
|
outerEnv
|
|
.Where(kv => !innerEnv.ContainsKey(kv.Key))
|
|
.Select(kv => kv.Value)
|
|
.Contains(item);
|
|
|
|
public void CopyTo(object?[] array, int arrayIndex) {
|
|
foreach (var k in this) {
|
|
array[arrayIndex++] = k;
|
|
}
|
|
}
|
|
|
|
public IEnumerator<object?> GetEnumerator() {
|
|
foreach (var k in innerEnv.Values) {
|
|
yield return k;
|
|
}
|
|
foreach (var kv in outerEnv) {
|
|
if (!innerEnv.ContainsKey(kv.Key)) {
|
|
yield return kv.Value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool Remove(object? item) =>
|
|
throw new NotSupportedException();
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() =>
|
|
GetEnumerator();
|
|
}
|
|
}
|