Beispiel #1
0
        private static void AddOperator(string opName)
        {
            Operator op = new Operator(opName);

            while (operators.Count > 0 &&                                 // there are operators in the stack  AND
                   (op < operators.First() ||                             // there is an operator with higher precedence  OR
                    (op == operators.First() && op.IsLeftAssociative()))) // there are multiple left associative operators of the same kind */
            {
                Operator nextOp = operators.Peek();

                // If next operator is OpenParenthesis and current operator is not ClosedParenthesis
                if (nextOp.Name == OperatorNames.OpenParenthesis && op.Name != OperatorNames.ClosedParenthesis)
                {
                    break;
                }

                // remove operator and create expression
                CreateExpression(operators.Pop());

                // If last operator was OpenParenthesis
                if (nextOp.Name == OperatorNames.OpenParenthesis)
                {
                    break;
                }
            }

            if (op.Name != OperatorNames.ClosedParenthesis)
            {
                operators.Push(op);
            }
        }