public CompileResult CompileFunction(string functionText) { var result = new CompileResult(); if (string.IsNullOrEmpty(functionText)) { return result; } Node ast = Parse(functionText, result); if (result.Errors.Any()) { return result; } var builder = new ExpressionTreeBuilder(); var expressionTree = builder.CreateFunction(ast, result); if (expressionTree == null || result.Errors.Any()) { return result; } Func<double, double> function = evaluator.InterpretFunction(expressionTree); result.Function = function; return result; }
private static Node Parse(string text, CompileResult result) { ParseResult ast = Parser.Parse(text); if (ast.Errors.Any()) { result.Errors.AddRange(ast.Errors); } return ast.Root; }
public Expression<Func<double, double>> CreateFunction(Node root, CompileResult status) { Status = status; ParameterExpression parameter = Expression.Parameter(typeof(double), "x"); Binder.RegisterParameter(parameter); Expression body = CreateExpressionCore(root); if (body == null) { return null; } var expressionTree = Expression.Lambda<Func<double, double>>(body, parameter); return expressionTree; }
public Expression<Func<double>> CreateExpression(Node root, CompileResult status) { Status = status; Expression body = CreateExpressionCore(root); if (body == null) { return null; } // If expression does not return double, we'll get an error. if (body.Type != typeof(double)) { return null; } var expressionTree = Expression.Lambda<Func<double>>(body); return expressionTree; }