public ExpressionResult Evaluate(T target, EvaluationContext context) { ExpressionResult result = null; foreach (var expr in Expressions) { result = expr.Evaluate(target, context); } return(result); }
public Func <IReadOnlyList <object>, ExpressionResult> GetMethod(T target, string name) { var method = target.GetType().GetMethod(name); var parameters = method.GetParameters(); return(args => parameters.Length != args.Count ? ExpressionResult.Error( "ArgumentLengthMismatch", $"Expected {parameters.Length} arguments to {name} method got {args.Count}") : Wrap(name, () => method.Invoke(target, args.ToArray()))); }
public ExpressionResult Evaluate(T target, EvaluationContext context) { var results = ArgList.Select(arg => arg.Evaluate(target, context)).ToArray(); if (results.Any(r => r.IsError)) { return(ExpressionResult.Error(results)); } var args = results.Select(arg => arg.Value).ToList(); return(new ExpressionResult(args)); }
public ExpressionResult Evaluate(T target, EvaluationContext context) { var mapped = items .Select(item => item.Evaluate(target, context)) .ToList(); if (mapped.Any(m => m.IsError)) { return(ExpressionResult.Error(mapped.ToArray())); } return(new ExpressionResult( new System.Collections.Generic.List <object>(mapped.Select(m => m.Value)))); }
public ExpressionResult Evaluate(T target, EvaluationContext context) { var args = Args.Evaluate(target, context); if (args.IsError) { return(ExpressionResult.Error(args)); } if (args.Value is IReadOnlyList <object> funcArgs) { return(EvalFunc(target, funcArgs, context)); } return(ExpressionResult.TypeError("FunctionArgs", args.Value?.GetType())); }
private ExpressionResult Wrap(string name, Func <object> invoke) { try { var result = invoke(); if (result is ExpressionResult er) { return(er); } return(new ExpressionResult(result)); } catch (Exception ex) { return(ExpressionResult.Error("MethodEvalError", $"Exception evaluating method '{name}', '{ex.Message}'")); } }
public ExpressionResult Evaluate(T target, EvaluationContext context) { var baseRes = Base.Evaluate(target, context); var exponentRes = Exponent.Evaluate(target, context); if (baseRes.IsError || exponentRes.IsError) { return(ExpressionResult.Error(baseRes, exponentRes)); } var baseValue = baseRes.Value.CastDouble(); var exponentValue = exponentRes.Value.CastDouble(); if (baseValue == null || exponentValue == null) { return(ExpressionResult.TypeError("^", baseRes.Value?.GetType(), exponentRes.Value?.GetType())); } var result = Math.Pow(baseValue.Value, exponentValue.Value); return(new ExpressionResult(result)); }
protected override ExpressionResult Evaluate(object left, object right) { if (right is IList <object> rl && left is IComparable lc) { if (rl.Count != 2) { return(ExpressionResult.Error( "ParameterMismatch", "Between operator expects an array of exactly two elements on the right side")); } var first = rl[0]; var second = rl[1]; if (first is IComparable fc && second is IComparable sc) { return(Value(lc.CompareTo(fc) >= 0 && lc.CompareTo(sc) <= 0)); } return(ExpressionResult.TypeError(Op, first?.GetType(), second?.GetType())); } return(ExpressionResult.TypeError(Op, left?.GetType(), right?.GetType())); }
protected virtual ExpressionResult Evaluate(bool left, bool right) { return(ExpressionResult.TypeError(Op, typeof(bool))); }
protected virtual ExpressionResult Evaluate(string left, string right) { return(ExpressionResult.TypeError(Op, typeof(string))); }
protected virtual ExpressionResult Evaluate(long left, long right) { return(ExpressionResult.TypeError(Op, typeof(int), typeof(long))); }
protected virtual ExpressionResult Evaluate(double left, double right) { return(ExpressionResult.TypeError(Op, typeof(double))); }
protected virtual ExpressionResult Evaluate(decimal left, decimal right) { return(ExpressionResult.TypeError(Op, typeof(decimal))); }