Exemplo n.º 1
0
        internal void AddOperator(Operator expr)
        {
            _isStart = false;
            ExprNode newNode     = new ExprNode(expr);
            ExprNode lastOperand = _top.LastOperand;

            if (_top.Expression == null)
            {
                _top.Expression = expr;
            }
            else
            {
                Operator op = _top.Expression as Operator;
                if (op == null)
                {
                    throw ParserException.InternalError();
                }
                else
                {
                    if (expr.Priority > op.Priority)
                    {
                        if (lastOperand != null)
                        {
                            _top.Remove(lastOperand);
                            newNode.Add(lastOperand);
                            _top.Add(newNode);
                            _top = newNode;
                        }
                        else
                        {
                            throw ParserException.InternalError();
                        }
                    }
                    else
                    {
                        if (_top.Parent != null)
                        {
                            newNode.Add(_root);
                            _root = newNode;
                            _top  = newNode;
                        }
                        else
                        {
                            newNode.Add(_top);
                            _top  = newNode;
                            _root = _top;
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        internal void Pop(char ch)
        {
            _isStart = false;
            if (_nodeStack.Count == 0)
            {
                if (ch == ',')
                {
                    throw ParserException.NoParenBefore("'( or ']'");
                }
                else
                {
                    throw ParserException.NoParenBefore(ch.ToString());
                }
            }

            StackNode data = (StackNode)_nodeStack.Pop();

            if (ch != ',' && data.charPushed != ',')
            {
                if (ch != data.charPushed)
                {
                    throw ParserException.ParenNotMatch(ch);
                }
            }
            if (this._root.Expression == null)
            {
                if (this._root.OperandCount > 0)
                {
                    foreach (ExprNode node in this._root.Operands)
                    {
                        node._parent = data.top;
                    }
                    data.top.Operands.AddRange(this._root.Operands);
                }
            }
            else
            {
                data.top.Add(this._root);
            }
            this._root = data.root;
            this._top  = data.top;
        }