25 lines
946 B
C#
25 lines
946 B
C#
namespace Bts.ArithmeticExpressions;
|
|
|
|
public class Assignment {
|
|
public Assignment(string? identifier, IArithmeticExpression? value) {
|
|
Identifier = identifier ?? throw new ArgumentNullException(nameof(identifier));
|
|
Value = value ?? throw new ArgumentNullException(nameof(identifier));
|
|
}
|
|
|
|
public string? Identifier { get; }
|
|
public IArithmeticExpression? Value { get; }
|
|
|
|
public IDictionary<string, object?> Evaluate(IDictionary<string, object?> env) {
|
|
if (Identifier is null) {
|
|
throw new InvalidOperationException("Identifier is null");
|
|
}
|
|
if (Value is null) {
|
|
throw new InvalidOperationException($"Value is null (Identifier '{Identifier}')");
|
|
}
|
|
env[Identifier] = Value.Evaluate(env) ?? throw new Exception(
|
|
$"{nameof(Assignment)}.{nameof(Value)} (of type '{Value.GetType()}') " +
|
|
$"evaluated to null (Identifier '{Identifier}')");
|
|
return env;
|
|
}
|
|
}
|