コード例 #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}");
            }
        }
コード例 #3
0
        private List <string> InternalConvert(string input)
        {
            var result = new List <string>();

            var inputQueue = new Queue <char>(input.ToCharArray());

            do
            {
                var partialToken = inputQueue.Dequeue();                   //Get the next in line to be parsed

                var numberToken = CheckIfNumber(partialToken, inputQueue); //Checks if number, empty string if not
                if (numberToken != "")
                {
                    QueueHelper.RemoveFromQueue(numberToken.Length - 1, inputQueue); //Remove the items that have already been parsed from the input queue
                    result.Add(numberToken);
                }
                else
                {
                    //Checks for operator, returns a stack of possible Evaluatables, the top member of the token stack will be the one used
                    var operatorTokens = CheckIfOperator(partialToken, inputQueue);


                    if (operatorTokens.Count != 0)
                    {
                        var operatorToken = operatorTokens.Pop();
                        QueueHelper.RemoveFromQueue(operatorToken.Length - 1, inputQueue);

                        FoundOperator(operatorToken, result); //Push and pop Evaluatables from the holding stack
                    }
                    else
                    {
                        FoundSpecialChar(partialToken, result); //If it doesn't find an operator it's most likely a parenthesis
                    }
                }
            } while (inputQueue.Count != 0);

            while (Hold.Count != 0) //Pop the remaining Evaluatables to the output list
            {
                var item = Hold.Pop();
                if (!Evaluatables.OperatorExists(item))
                {
                    throw new MissMatchedParenthesisException();
                }

                result.Add(item);
            }
            return(result);
        }