public CompileResult CompileFunction(Drawing drawing, string functionText) { CompileResult result = new CompileResult(); if (string.IsNullOrEmpty(functionText)) { return result; } ParseTree ast = Parse(functionText, result); if (!result.Errors.IsEmpty()) { return result; } ExpressionTreeBuilder builder = new ExpressionTreeBuilder(); builder.SetContext(drawing, f => true); var expressionTree = builder.CreateFunction(ast.Root, result); if (expressionTree == null || !result.Errors.IsEmpty()) { return result; } Func<double, double> function = ExpressionTreeEvaluatorProvider.InterpretFunction(expressionTree); result.Function = function; return result; }
public CompileResult CompileExpression( Drawing drawing, string expressionText, Predicate<IFigure> isFigureAllowed) { CompileResult result = new CompileResult(); if (expressionText.IsEmpty()) { return result; } ParseTree ast = Parse(expressionText, result); if (!result.Errors.IsEmpty()) { return result; } ExpressionTreeBuilder builder = new ExpressionTreeBuilder(); builder.SetContext(drawing, isFigureAllowed); var expressionTree = builder.CreateExpression(ast.Root, result); if (expressionTree == null || !result.Errors.IsEmpty()) { return result; } Func<double> function = ExpressionTreeEvaluatorProvider.InterpretExpression(expressionTree); result.Expression = function; return result; }
private static Node Parse(string text, CompileResult result) { ParseResult ast = Parser.Parse(text); if (!ast.Errors.IsEmpty()) { result.Errors.AddRange(ast.Errors); } return ast.Root; }
public Expression<Func<double>> CreateExpression(ParseTreeNode root, CompileResult status) { Status = status; Expression body = CreateExpressionCore(root); if (body == null) { return null; } var expressionTree = Expression.Lambda<Func<double>>(body); return expressionTree; }
public Expression<Func<double, double>> CreateFunction(ParseTreeNode 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. - D.H. if (body.Type != typeof(double)) { return null; } var expressionTree = Expression.Lambda<Func<double>>(body); return expressionTree; }
void ProcessMatch(Match match) { var result = match.Value; if (result.Length < 3) { CompileResult error = new CompileResult(); error.AddError("Empty expression"); embeddedExpressions.Add(error); return; } var expression = result.Substring(1, result.Length - 2); var compileResult = Drawing.CompileExpression(expression); embeddedExpressions.Add(compileResult); if (compileResult.IsSuccess) { Dependencies.AddRange(compileResult.Dependencies); } }
void SetFunction(CompileResult result) { Function = result.Function; this.UnregisterFromDependencies(); Dependencies.SetItems(result.Dependencies); // See explanation in DrawingExpression.Recalculate(). if (Drawing.Figures.Contains(this)) { this.RegisterWithDependencies(); this.RecalculateAllDependents(); } }
private static ParseTree Parse(string text, CompileResult result) { ParseTree ast = ParserInstance.Parse(text); var compileErrors = GetCompilerErrors(ast.ParserMessages); if (!compileErrors.IsEmpty()) { result.Errors.AddRange(compileErrors); } return ast; }