Exemplo n.º 1
0
        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;
        }
Exemplo n.º 2
0
        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;
        }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
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.
            if (body.Type != typeof(double))
            {
                return null;
            }

            var expressionTree = Expression.Lambda<Func<double>>(body);
            return expressionTree;
        }