namespace Bts.ArithmeticExpressions; using Spark.Core; using System.Collections.Immutable; using System.Data; public class Function : IArithmeticExpression { public Function(string? identifier, ImmutableList? arguments) { Identifier = identifier; Arguments = arguments; } public string? Identifier { get; } public ImmutableList? Arguments { get; } public object? Evaluate(IDictionary env) { if (Identifier is null) { throw new InvalidOperationException("Identifier is null"); } if (Arguments is null) { throw new InvalidOperationException("Arguments is null"); } if (Identifier == "ifNull") { throw new NotImplementedException("'ifNull' function not yet implemented"); } var argVals = Arguments.Select(e => e.Evaluate(env)).ToArray(); var argTypes = Array.ConvertAll(argVals, v => v?.GetType() ?? typeof(object)); var method = typeof(Math).GetMethod(Identifier, argTypes) ?? throw new EvaluateException( $"Failed to find method '{Identifier}({", ".join(argTypes)})' in {typeof(Math)}"); return method.Invoke(null, argVals); } }