Пример #1
0
        /*
         * Pushes the current operator token to the stack and pops Evaluatables from the stack
         */
        private void FoundOperator(string token, List <string> result)
        {
            var op = Evaluatables[token];

            if (Hold.Count != 0)
            {
                while (Hold.Count != 0)
                {
                    var topOfStack = Hold.Peek();
                    if (!Evaluatables.OperatorExists(topOfStack))
                    {
                        break;
                    }
                    var operatorTop = Evaluatables[topOfStack];
                    if ((OperatorHelper.CheckAssociativity(op, Associativity.L) && op.Precedence <= operatorTop.Precedence) ||
                        OperatorHelper.CheckAssociativity(op, Associativity.R) && op.Precedence < operatorTop.Precedence)
                    {
                        result.Add(Hold.Pop());
                    }
                    else
                    {
                        break;
                    }
                }
            }
            Hold.Push(token);
        }
Пример #2
0
        private void FoundSpecialChar(char partialToken, List <string> result)
        {
            switch (partialToken)
            {
            case '(':
                Hold.Push(partialToken.ToString());
                return;

            case ')':
                /*
                 * Pop Evaluatables from the stack until a matching parenthesis is found
                 */

                var foundLeftParenthesis = false;

                do
                {
                    var topOfStack = Hold.Pop();
                    if (topOfStack == "(")
                    {
                        foundLeftParenthesis = true;
                        break;
                    }

                    result.Add(topOfStack);
                } while (Hold.Count != 0);

                if (!foundLeftParenthesis)
                {
                    throw new MissMatchedParenthesisException();
                }

                break;

            case ',':
                var topOfstack = Hold.Peek();
                while (topOfstack != "(")
                {
                    if (Hold.Count != 0)
                    {
                        result.Add(Hold.Pop());
                        topOfstack = Hold.Peek();
                    }
                    else
                    {
                        throw new MissMatchedParenthesisException();
                    }
                }

                break;

            default:
                throw new UnexpectedOperatorException($"Unexpected Operator {partialToken}");
            }
        }