Exemple #1
0
        public Expression(LogicalExpression expression, EvaluateOptions options)
        {
            if (expression == null)
                throw new
                    ArgumentException("Expression can't be null", "expression");

            ParsedExpression = expression;
            Options = options;
        }
Exemple #2
0
 public TernaryExpression(LogicalExpression leftExpression, LogicalExpression middleExpression, LogicalExpression rightExpression)
 {
     this.LeftExpression   = leftExpression;
     this.MiddleExpression = middleExpression;
     this.RightExpression  = rightExpression;
 }
Exemple #3
0
        public string CalExpressionTreeResult(NCalc2.Expressions.LogicalExpression lgInput, int intSpaceInput)
        {
            if (lgInput == null)
            {
                return("\r\n");
            }
            //
            string strRet = "";
            string strTemp = "";
            int    i, j = 0;

            //Each time we call this function in another level, add more indication space
            int intSpace = intSpaceInput + 1;

            //
            if ((lgInput == null) || (lgInput.objResult == null)) //This one is Recursion stop condition
            {
                strRet = strRet + "null" + "\r\n";
            }
            else if (lgInput is NCalc2.Expressions.ValueExpression) //This one is Recursion stop condition
            {
                NCalc2.Expressions.ValueExpression ValueEx = (NCalc2.Expressions.ValueExpression)lgInput;
                strRet = strRet + lgInput.ToString() + ":" + ValueEx.Value.ToString() + "\r\n";
            }
            else if (lgInput is NCalc2.Expressions.IdentifierExpression) //This one is Recursion stop condition
            {
                strRet = strRet + lgInput.ToString() + ": " + lgInput.objResult.ToString() + "\r\n";
            }
            else if (lgInput is NCalc2.Expressions.FunctionExpression) //This case may do recursion
            {
                strRet = strRet + lgInput.ToString() + ": " + lgInput.objResult.ToString() + "\r\n";
                //
                NCalc2.Expressions.FunctionExpression FuncExpress = (NCalc2.Expressions.FunctionExpression)lgInput;
                for (j = 0; j < intSpace; j++)
                {
                    strTemp = strTemp + "___";
                }

                for (i = 0; i < FuncExpress.Expressions.Length; i++)
                {
                    strRet = strRet + strTemp + this.CalExpressionTreeResult(FuncExpress.Expressions[i], intSpace);// + "\r\n";
                }
            }
            else if (lgInput is NCalc2.Expressions.BinaryExpression) //This case may do recursion
            {
                NCalc2.Expressions.BinaryExpression BinaryEx = (NCalc2.Expressions.BinaryExpression)lgInput;
                strRet = strRet + lgInput.ToString() + ": " + lgInput.objResult.ToString() + "\r\n";
                for (j = 0; j < intSpace; j++)
                {
                    strTemp = strTemp + "___";
                }

                strRet = strRet + strTemp + this.CalExpressionTreeResult(BinaryEx.LeftExpression, intSpace);  // + "\r\n";
                strRet = strRet + strTemp + this.CalExpressionTreeResult(BinaryEx.RightExpression, intSpace); // + "\r\n";
            }
            else if (lgInput is NCalc2.Expressions.TernaryExpression)                                         //This case may do recursion
            {
                NCalc2.Expressions.TernaryExpression TernaryEx = (NCalc2.Expressions.TernaryExpression)lgInput;
                strRet = strRet + lgInput.ToString() + ": " + lgInput.objResult.ToString() + "\r\n";
                for (j = 0; j < intSpace; j++)
                {
                    strTemp = strTemp + "___";
                }

                strRet = strRet + strTemp + this.CalExpressionTreeResult(TernaryEx.LeftExpression, intSpace);   // + "\r\n";
                strRet = strRet + strTemp + this.CalExpressionTreeResult(TernaryEx.MiddleExpression, intSpace); // + "\r\n";
                strRet = strRet + strTemp + this.CalExpressionTreeResult(TernaryEx.RightExpression, intSpace);  // + "\r\n";
            }
            else if (lgInput is NCalc2.Expressions.UnaryExpression)                                             //This case may do recursion
            {
                NCalc2.Expressions.UnaryExpression UnaryEx = (NCalc2.Expressions.UnaryExpression)lgInput;
                strRet = strRet + lgInput.ToString() + ": " + lgInput.objResult.ToString() + "\r\n";
                for (j = 0; j < intSpace; j++)
                {
                    strTemp = strTemp + "___";
                }

                strRet = strRet + strTemp + this.CalExpressionTreeResult(UnaryEx.Expression, intSpace);// + "\r\n";
            }
            else //This one is Recursion stop condition
            {
                //strRet = strRet + lgInput.ToString() + ": " + lgInput.objResult.ToString() + "\r\n";
            }

            //
            return(strRet);
        }
Exemple #4
0
 public BinaryExpression(BinaryExpressionType type, LogicalExpression leftExpression, LogicalExpression rightExpression)
 {
     Type            = type;
     LeftExpression  = leftExpression;
     RightExpression = rightExpression;
 }
Exemple #5
0
 public UnaryExpression(UnaryExpressionType type, LogicalExpression expression)
 {
     Type       = type;
     Expression = expression;
 }
Exemple #6
0
 public TernaryExpression(LogicalExpression leftExpression, LogicalExpression middleExpression, LogicalExpression rightExpression)
 {
     this.LeftExpression = leftExpression;
     this.MiddleExpression = middleExpression;
     this.RightExpression = rightExpression;
 }
Exemple #7
0
 public BinaryExpression(BinaryExpressionType type, LogicalExpression leftExpression, LogicalExpression rightExpression)
 {
     Type = type;
     LeftExpression = leftExpression;
     RightExpression = rightExpression;
 }
Exemple #8
0
        /// <summary>
        /// Pre-compiles the expression in order to check syntax errors.
        /// If errors are detected, the Error property contains the message.
        /// </summary>
        /// <returns>True if the expression syntax is correct, otherwiser False</returns>
        public bool HasErrors()
        {
            try
            {
                if (ParsedExpression == null)
                {
                    ParsedExpression = Compile(OriginalExpression, (Options & EvaluateOptions.NoCache) == EvaluateOptions.NoCache);
                }

                // In case HasErrors() is called multiple times for the same expression
                return ParsedExpression != null && Error != null;
            }
            catch (Exception e)
            {
                Error = e.Message;
                return true;
            }
        }
Exemple #9
0
        public object Evaluate()
        {
            if (HasErrors())
            {
                throw new EvaluationException(Error);
            }

            if (ParsedExpression == null)
            {
                ParsedExpression = Compile(OriginalExpression, (Options & EvaluateOptions.NoCache) == EvaluateOptions.NoCache);
            }

            var visitor = new EvaluationVisitor(Options);
            visitor.EvaluateFunction += EvaluateFunction;
            visitor.EvaluateParameter += EvaluateParameter;
            visitor.Parameters = Parameters;

            // if array evaluation, execute the same expression multiple times
            if ((Options & EvaluateOptions.IterateParameters) == EvaluateOptions.IterateParameters)
            {
                int size = -1;
                ParametersBackup = new Dictionary<string, object>();
                foreach (string key in Parameters.Keys)
                {
                    ParametersBackup.Add(key, Parameters[key]);
                }

                ParameterEnumerators = new Dictionary<string, IEnumerator>();

                foreach (object parameter in Parameters.Values)
                {
                    if (parameter is IEnumerable)
                    {
                        int localsize = 0;
                        foreach (object o in (IEnumerable)parameter)
                        {
                            localsize++;
                        }

                        if (size == -1)
                        {
                            size = localsize;
                        }
                        else if (localsize != size)
                        {
                            throw new EvaluationException("When IterateParameters option is used, IEnumerable parameters must have the same number of items");
                        }
                    }
                }

                foreach (string key in Parameters.Keys)
                {
                    var parameter = Parameters[key] as IEnumerable;
                    if (parameter != null)
                    {
                        ParameterEnumerators.Add(key, parameter.GetEnumerator());
                    }
                }

                var results = new List<object>();
                for (int i = 0; i < size; i++)
                {
                    foreach (string key in ParameterEnumerators.Keys)
                    {
                        IEnumerator enumerator = ParameterEnumerators[key];
                        enumerator.MoveNext();
                        Parameters[key] = enumerator.Current;
                    }

                    ParsedExpression.Accept(visitor);
                    results.Add(visitor.Result);
                }

                return results;
            }

            ParsedExpression.Accept(visitor);
            return visitor.Result;
        }
Exemple #10
0
 public FunctionExpression(IdentifierExpression identifier, LogicalExpression[] expressions)
 {
     Identifier = identifier;
     Expressions = expressions;
 }
Exemple #11
0
        protected void EncapsulateNoValue(LogicalExpression expression)
        {
            if (expression is ValueExpression)
            {
                expression.Accept(this);
            }
            else
            {
                Result.Append("(");
                expression.Accept(this);

                // trim spaces before adding a closing paren
                while (Result[Result.Length - 1] == ' ')
                    Result.Remove(Result.Length - 1, 1);

                Result.Append(") ");
            }
        }