Пример #1
0
 private static bool IsOperator(string token)
 {
     if (token.Length != 1)
     {
         return(false);
     }
     return(ExpressionEvaluator.IsOperator(token[0]));
 }
Пример #2
0
 private static bool IsCommand(char character)
 {
     if ((int)character == 40 || (int)character == 41)
     {
         return(true);
     }
     return(ExpressionEvaluator.IsOperator(character));
 }
Пример #3
0
 private static bool NeedToPop(Stack <char> operatorStack, ExpressionEvaluator.Operator newOperator)
 {
     if (operatorStack.LongCount <char>() > 0L)
     {
         ExpressionEvaluator.Operator @operator = ExpressionEvaluator.CharToOperator(operatorStack.Peek());
         if (ExpressionEvaluator.IsOperator(@operator.character) && (newOperator.associativity == ExpressionEvaluator.Associativity.Left && newOperator.presedence <= @operator.presedence || newOperator.associativity == ExpressionEvaluator.Associativity.Right && newOperator.presedence < @operator.presedence))
         {
             return(true);
         }
     }
     return(false);
 }
Пример #4
0
        private static string PreFormatExpression(string expression)
        {
            string str = expression.Trim();

            if (str.Length == 0)
            {
                return(str);
            }
            char character = str[str.Length - 1];

            if (ExpressionEvaluator.IsOperator(character))
            {
                str = str.TrimEnd(character);
            }
            return(str);
        }
Пример #5
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);
        }
Пример #6
0
        private static string PreFormatExpression(string expression)
        {
            string text = expression.Trim();

            if (text.Length == 0)
            {
                return(text);
            }
            char c = text[text.Length - 1];

            if (ExpressionEvaluator.IsOperator(c))
            {
                text = text.TrimEnd(new char[]
                {
                    c
                });
            }
            return(text);
        }
Пример #7
0
        private static bool NeedToPop(Stack <char> operatorStack, ExpressionEvaluator.Operator newOperator)
        {
            bool result;

            if (operatorStack.Count > 0)
            {
                ExpressionEvaluator.Operator @operator = ExpressionEvaluator.CharToOperator(operatorStack.Peek());
                if (ExpressionEvaluator.IsOperator(@operator.character))
                {
                    if ((newOperator.associativity == ExpressionEvaluator.Associativity.Left && newOperator.presedence <= @operator.presedence) || (newOperator.associativity == ExpressionEvaluator.Associativity.Right && newOperator.presedence < @operator.presedence))
                    {
                        result = true;
                        return(result);
                    }
                }
            }
            result = false;
            return(result);
        }
Пример #8
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));
        }
Пример #9
0
 private static bool IsOperator(string token)
 {
     return(token.Length == 1 && ExpressionEvaluator.IsOperator(token[0]));
 }
Пример #10
0
 private static bool IsCommand(char character)
 {
     return(character == '(' || character == ')' || ExpressionEvaluator.IsOperator(character));
 }