Example #1
0
        private IEnumerator <EvaluationNode> EvaluationTree(Expression expression, EvaluationNode parentNode)
        {
            ListStack      evaluationstack = new ListStack();
            EvaluationNode prevNode        = null;

            for (int j = 0; j < expression.ExpressionStack.Count; j++)
            {
                Expression e = expression.ExpressionStack[j];
                if (e.Valuable)
                {
                    evaluationstack.Push(e);
                }
                else
                {
                    Expression[] values = new Expression[e.ArgumentsCount];
                    if (expression.ExpressionStack.Count < e.ArgumentsCount)
                    {
                        throw new EvaluateException("Syntax Error");
                    }
                    for (int i = e.ArgumentsCount - 1; i >= 0; i--)
                    {
                        values[i] = evaluationstack.Pop();
                    }
                    evaluationstack.Push(e);
                    prevNode = new EvaluationNode()
                    {
                        Node = e, Params = values, Prevoius = prevNode, ParentNode = parentNode
                    };
                    yield return(prevNode);

                    if (e.InnerStack)
                    {
                        IEnumerator <EvaluationNode> enChild = e.EvaluationTree(e, prevNode);
                        while (enChild.MoveNext())
                        {
                            EvaluationNode childNode = enChild.Current;
                            yield return(childNode);
                        }
                    }
                }
            }
        }
Example #2
0
        internal object Evaluate(object[] variables, out bool evaluated)
        {
            evaluated = true;
            ListStack evaluationstack = new ListStack();

            for (int j = 0; j < ExpressionStack.Count; j++)
            {
                Expression e = ExpressionStack[j];
                if (e.Valuable)
                {
                    VariableExpression v = e as VariableExpression;
                    if (v != null)
                    {
                        e = new ConstExpression(variables[v.Ordinal]);
                    }
                    evaluationstack.Push(e);
                }
                else
                {
                    Expression[] values = new Expression[e.ArgumentsCount];
                    if (ExpressionStack.Count < e.ArgumentsCount)
                    {
                        throw new EvaluateException("Syntax Error");
                    }
                    for (int i = e.ArgumentsCount - 1; i >= 0; i--)
                    {
                        values[i] = evaluationstack.Pop();
                    }
                    evaluated = true;
                    Expression[] ret = e.Evaluate(values, out evaluated);
                    if (evaluated && ret != null)
                    {
                        for (int i = 0; i < ret.Length; i++)
                        {
                            evaluationstack.Push(ret[i]);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (evaluated)
            {
                if (evaluationstack.Count == 1)
                {
                    if (evaluationstack[0].NumericValue.HasValue)
                    {
                        return(evaluationstack[0].NumericValue.Value);
                    }
                    else if (evaluationstack[0].BoolValue.HasValue)
                    {
                        return(evaluationstack[0].BoolValue.Value);
                    }
                    else if (evaluationstack[0].ArrayValue != null)
                    {
                        return(evaluationstack[0].ArrayValue);
                    }
                    else if (evaluationstack[0].DataTimeValue != null)
                    {
                        return(evaluationstack[0].DataTimeValue);
                    }
                    else if (evaluationstack[0].StringValue != null)
                    {
                        return(evaluationstack[0].StringValue);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    throw new EvaluateException("Syntax Error");
                }
            }
            else
            {
                return(null);
            }
        }