Пример #1
0
        private static T Evaluate <T>(string[] tokens)
        {
            Stack <string> stack = new Stack <string>();
            T result;

            for (int i = 0; i < tokens.Length; i++)
            {
                string text = tokens[i];
                if (ExpressionEvaluator.IsOperator(text))
                {
                    ExpressionEvaluator.Operator @operator = ExpressionEvaluator.CharToOperator(text[0]);
                    List <T> list = new List <T>();
                    bool     flag = true;
                    while (stack.Count > 0 && !ExpressionEvaluator.IsCommand(stack.Peek()) && list.Count < @operator.inputs)
                    {
                        T item;
                        flag &= ExpressionEvaluator.TryParse <T>(stack.Pop(), out item);
                        list.Add(item);
                    }
                    list.Reverse();
                    if (!flag || list.Count != @operator.inputs)
                    {
                        result = default(T);
                        return(result);
                    }
                    Stack <string> arg_CF_0 = stack;
                    T t = ExpressionEvaluator.Evaluate <T>(list.ToArray(), text[0]);
                    arg_CF_0.Push(t.ToString());
                }
                else
                {
                    stack.Push(text);
                }
            }
            if (stack.Count == 1)
            {
                T t2;
                if (ExpressionEvaluator.TryParse <T>(stack.Pop(), out t2))
                {
                    result = t2;
                    return(result);
                }
            }
            result = default(T);
            return(result);
        }
Пример #2
0
        private static T Evaluate <T>(string[] tokens)
        {
            Stack <string> source = new Stack <string>();

            foreach (string token in tokens)
            {
                if (ExpressionEvaluator.IsOperator(token))
                {
                    ExpressionEvaluator.Operator @operator = ExpressionEvaluator.CharToOperator(token[0]);
                    List <T> objList = new List <T>();
                    bool     flag    = true;
                    while (source.LongCount <string>() > 0L && !ExpressionEvaluator.IsCommand(source.Peek()) && objList.Count < @operator.inputs)
                    {
                        T result;
                        flag &= ExpressionEvaluator.TryParse <T>(source.Pop(), out result);
                        objList.Add(result);
                    }
                    objList.Reverse();
                    if (!flag || objList.Count != @operator.inputs)
                    {
                        return(default(T));
                    }
                    source.Push(ExpressionEvaluator.Evaluate <T>(objList.ToArray(), token[0]).ToString());
                }
                else
                {
                    source.Push(token);
                }
            }
            T result1;

            if (source.LongCount <string>() == 1L && ExpressionEvaluator.TryParse <T>(source.Pop(), out result1))
            {
                return(result1);
            }
            return(default(T));
        }