124 lines
4.2 KiB
C#
124 lines
4.2 KiB
C#
namespace Bts.BooleanExpressions;
|
|
|
|
using Bts.BooleanExpressions.Arguments;
|
|
|
|
using Spark.Core.Collections;
|
|
|
|
using System.Collections;
|
|
|
|
class EvaluationEnvironment : IDictionary<string, object?> {
|
|
IDictionary<string, object?> userBindings;
|
|
IDictionary<string, FunctionDefinition> definitions;
|
|
|
|
public EvaluationEnvironment(
|
|
IDictionary<string, object?> userBindings,
|
|
IDictionary<string, FunctionDefinition> definitions) {
|
|
this.userBindings = userBindings;
|
|
this.definitions = definitions;
|
|
}
|
|
|
|
public int Count =>
|
|
definitions.Count + userBindings.Count;
|
|
|
|
public bool IsReadOnly => true;
|
|
|
|
public ICollection<string> Keys =>
|
|
AggregateCollection.Create(userBindings.Keys, definitions.Keys);
|
|
|
|
public ICollection<object?> Values =>
|
|
AggregateCollection.Create(
|
|
userBindings.Values, SelectorCollection.Create(definitions.Values, v => (object?)v));
|
|
|
|
public object? this[string key] {
|
|
get {
|
|
var keystr = key.ToString();
|
|
if (keystr is null || !definitions.TryGetValue(keystr, out var fd)) {
|
|
return userBindings.TryGetValue(key, out var ubnd) ? ubnd :
|
|
throw new KeyNotFoundException(
|
|
$"EvaluationEnvironment does not contain key '{key}'");
|
|
}
|
|
return new Function.Delegate(Evaluate);
|
|
|
|
object? Evaluate(List<object?> args) {
|
|
if (args.Count != fd.args.Count)
|
|
throw new BooleanExpressionInterpreterException(
|
|
$"Parameter count mismatch for {fd.name}. Expected: {fd.args} Got: {args.Count}");
|
|
else {
|
|
var newBindings = fd.args.Zip(args).ToDictionary();
|
|
return fd.expression.Evaluate(new EvaluationEnvironment(
|
|
AggregateDictionary.Create(newBindings, userBindings), definitions));
|
|
}
|
|
}
|
|
}
|
|
set => throw new NotSupportedException();
|
|
}
|
|
|
|
public bool ContainsKey(string key) {
|
|
var keystr = key.ToString();
|
|
return
|
|
(keystr is not null && definitions.ContainsKey(keystr)) ||
|
|
userBindings.ContainsKey(key);
|
|
}
|
|
|
|
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() {
|
|
foreach (var kv in userBindings) {
|
|
yield return kv;
|
|
}
|
|
foreach (var kv in definitions) {
|
|
if (!userBindings.ContainsKey(kv.Key)) {
|
|
yield return new(kv.Key, kv.Value);
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool TryGetValue(string key, out object? value) {
|
|
var keystr = key.ToString();
|
|
if (keystr is null || !definitions.TryGetValue(keystr, out var fd)) {
|
|
return userBindings.TryGetValue(key, out value);
|
|
}
|
|
value = new Function.Delegate(Evaluate);
|
|
return true;
|
|
|
|
object? Evaluate(List<object?> args) {
|
|
if (args.Count != fd.args.Count)
|
|
throw new BooleanExpressionInterpreterException(
|
|
$"Parameter count mismatch for {fd.name}. Expected: {fd.args} Got: {args.Count}");
|
|
else {
|
|
var newBindings = fd.args.Zip(args).ToDictionary();
|
|
return fd.expression.Evaluate(new EvaluationEnvironment(
|
|
AggregateDictionary.Create(newBindings, userBindings), definitions));
|
|
}
|
|
}
|
|
}
|
|
|
|
public void CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex) {
|
|
foreach (var kv in userBindings) {
|
|
array[arrayIndex++] = kv;
|
|
}
|
|
foreach (var kv in definitions) {
|
|
array[arrayIndex++] = new(kv.Key, kv.Value);
|
|
}
|
|
}
|
|
|
|
void IDictionary<string, object?>.Add(string key, object? value) =>
|
|
throw new NotSupportedException();
|
|
|
|
void ICollection<KeyValuePair<string, object?>>.Add(KeyValuePair<string, object?> item) =>
|
|
throw new NotSupportedException();
|
|
|
|
void ICollection<KeyValuePair<string, object?>>.Clear() =>
|
|
throw new NotSupportedException();
|
|
|
|
bool ICollection<KeyValuePair<string, object?>>.Contains(KeyValuePair<string, object?> item) =>
|
|
TryGetValue(item.Key, out var v) && Equals(v, item.Value);
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() =>
|
|
GetEnumerator();
|
|
|
|
bool IDictionary<string, object?>.Remove(string key) =>
|
|
throw new NotSupportedException();
|
|
|
|
bool ICollection<KeyValuePair<string, object?>>.Remove(KeyValuePair<string, object?> item) =>
|
|
throw new NotSupportedException();
|
|
}
|