예제 #1
0
파일: Compiler.cs 프로젝트: ondrej11/o106
        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;
        }
예제 #2
0
파일: Compiler.cs 프로젝트: ondrej11/o106
        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;
        }
예제 #3
0
파일: Compiler.cs 프로젝트: ondrej11/o106
 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;
 }
예제 #4
0
 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;
 }
예제 #5
0
 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;
 }
예제 #6
0
 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;
 }
예제 #7
0
파일: LabelBase.cs 프로젝트: ondrej11/o106
        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);
            }
        }
예제 #8
0
        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();
            }
        }
예제 #9
0
파일: Compiler.cs 프로젝트: ondrej11/o106
 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;
 }