public _OpTree(Operator Operator, _OpTree Left, _OpTree Right) { this.Operator = Operator; this.Left = Left; this.Right = Right; }
/// <summary> /// Combines an optree with a term using the given operator. /// </summary> public static _OpTree Combine(_OpTree Left, Operator Operator, Expression Right) { if (Left.Operator == null || Left.Operator.Precedence >= Operator.Precedence) return new _OpTree(Operator, Left, new _OpTree(Right)); else return new _OpTree(Left.Operator, Left.Left, Combine(Left.Right, Operator, Right)); }
/// <summary> /// Tries parsing an expression in the given text. /// </summary> public static bool AcceptExpression(Dictionary<string, Expression> Variables, string Text, ref int Index, ref Expression Expression, out int ErrorIndex) { ErrorIndex = Index; if (AcceptTerm(Variables, Text, ref Index, ref Expression, out ErrorIndex)) { _OpTree curtree = new _OpTree(Expression); int cur = Index; while (true) { AcceptExtendedWhitespace(Text, ref cur); Operator op = null; if (AcceptOperator(Text, ref cur, ref op)) { AcceptExtendedWhitespace(Text, ref cur); if (AcceptTerm(Variables, Text, ref cur, ref Expression, out ErrorIndex)) { curtree = _OpTree.Combine(curtree, op, Expression); Index = cur; continue; } return false; } break; } Expression = curtree.Expression; return true; } return false; }