public Tree() { _root = new ExprNode(null); _top = _root; _nodeStack = new Stack(); _isStart = true; }
protected override IExecutor CreateExecutorCore(ExprNode node) { Expression expr = node.Expression; if (expr is ConditionalOp) { return new ConditionalOpExecutor(); } else if (expr is BooleanAnd) { return new BooleanAndExecutor(); } else if (expr is BooleanOr) { return new BooleanOrExecutor(); } else if (expr is BinaryOp) { return new BinaryOpExecutor(); } else if (expr is Operand) { return new OperandExecutor(); } else { return new Executor(); } }
internal void Add(ExprNode node) { if (node._parent != null) { throw new ArgumentException(); } node._parent = this; this.Operands.Add(node); }
internal void Push(char ch) { StackNode data = new StackNode(); data.root = _root; data.top = _top; data.charPushed = ch; _nodeStack.Push(data); _root = new ExprNode(null); _top = _root; _isStart = true; }
public Result Execute(ExprNode node) { if ((node == null)||(node.Expression ==null )) return new Result(string.Empty); IExecutor start = this.CreateExecutor(node); IExecutor executor = start; this.Push(executor); while (executor != null) { ExecuteAction result = executor.Execute(this); if (result == ExecuteAction.End) { this.Pop(); } else if (result == ExecuteAction.Exit) { return start.Result; } executor = this.Top; } return start.Result; }
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; }
protected abstract IExecutor CreateExecutorCore(ExprNode node);
public virtual IExecutor CreateExecutor(ExprNode node) { IExecutor executor = CreateExecutorCore(node); executor.Initialize(this, node); return executor; }
public virtual void Initialize(IExecutorContext context, ExprNode node) { _node = node; Reset(context); }
internal void Remove(ExprNode node) { if (node.Parent == this) { node._parent = null; this.Operands.Remove(node); } else { throw new ArgumentException(); } }
static void PrintNode(ExprNode node,System.IO.TextWriter w,int indent) { PrintIndent(w, indent); w.Write(node.Expression.ToString()); if (node.Expression is Operator) w.Write(':'); w.WriteLine(); if (node.OperandCount != 0) { foreach (ExprNode child in node.Operands) { PrintNode(child, w, indent+1); } } }
internal void Complete() { if (_nodeStack.Count != 0) { StackNode data = (StackNode)_nodeStack.Pop(); throw ParserException.ParenNotMatch(data.charPushed); } _nodeStack = null; // fix null if (_root.Expression == null) { if (_root.OperandCount == 0) { _root = null; } else if (_root.OperandCount > 1) { throw ParserException.InternalError(); } else { _root = (ExprNode)_root.Operands[0]; _root._parent = null; } } }
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; } } } } }
internal void AddOperand(Operand expr) { _isStart = false; ExprNode argNode = new ExprNode(expr); _top.Add(argNode); }