public Expression(object[] tokens) { try { List<object> ex = new List<object>(); for (int i = 0; i < tokens.Length; i++) ex.Add(tokens[i]); int index = 0; for (index = 1; index < ex.Count; index++) { if (ex[index - 1] is NumberToken && ex[index] is IdentifierToken) { var num = ex[index - 1] as NumberToken; var unit = ex[index] as IdentifierToken; ex.RemoveAt(index); var d = new Dictionary<string, Unit>(); d.Add(unit.Text, new Unit(unit.Text, 1)); ex[index - 1] = new NumberType(num.Text, new UnitType(d)); index--; } } for (index = 0; index < ex.Count; index++) { if (ex[index] is ParenthOpenToken) { int ti = index; index++; int nest = 1; List<object> tt = new List<object>(); while (true) { if (ex[index] is ParenthOpenToken) nest++; if (ex[index] is ParenthCloseToken) nest--; if (nest == 0) { index++; break; } tt.Add(ex[index]); index++; } for (int i = index - 1; i > ti; i--) { ex.RemoveAt(i); } ex[ti] = new Expression(tt.ToArray()).RootExpr; index = ti + 1; } } for (index = 0; index < ex.Count - 2; index++) { if (ex[index] is IdentifierToken && ex[index + 1] is ListStartToken) { int ti = index + 2; List<ExprNode> p = new List<ExprNode>(); int nest = 0; while (!(ex[ti] is ListEndToken)) { List<object> tl = new List<object>(); if (ex[ti] is ListSeperatorToken) ti++; while (!(ex[ti] is ListSeperatorToken) && !(ex[ti] is ListEndToken) || nest != 0) { if (ex[ti] is ListStartToken) nest++; if (ex[ti] is ListEndToken) nest--; tl.Add(ex[ti++]); } p.Add(new Expression(tl.ToArray()).RootExpr); } for (int i = ti; i > index; i--) ex.RemoveAt(i); ex[index] = new FunctionNode(((IdentifierToken)ex[index]).Text, p.ToArray()); } } //Number Lists for (index = 0; index < ex.Count - 1; index++) { if (ex[index] is ListStartToken) { int ti = index + 1; List<ExprNode> p = new List<ExprNode>(); int nest = 0; while (!(ex[ti] is ListEndToken)) { List<object> tl = new List<object>(); if (ex[ti] is ListSeperatorToken) ti++; while (!(ex[ti] is ListSeperatorToken) && !(ex[ti] is ListEndToken) || nest != 0) { if (ex[ti] is ListStartToken) nest++; if (ex[ti] is ListEndToken) nest--; tl.Add(ex[ti++]); } p.Add(new Expression(tl.ToArray()).RootExpr); } for (int i = ti; i > index; i--) ex.RemoveAt(i); ex[index] = new ListNode(p.ToArray()); } } //======= for (index = 0; index < ex.Count; index++) { if (ex[index] is PowToken) { var left = ex[index - 1]; var right = ex[index + 1]; ex.RemoveAt(index + 1); ex.RemoveAt(index); ex[index - 1] = new PowerNode(Expr(left), Expr(right)); index--; } } for (index = 0; index < ex.Count; index++) { if (ex[index] is MulToken) { var left = ex[index - 1]; var right = ex[index + 1]; ex.RemoveAt(index + 1); ex.RemoveAt(index); ex[index - 1] = new MultiplicationNode(Expr(left), Expr(right)); index--; } if (ex[index] is DivToken) { var left = ex[index - 1]; var right = ex[index + 1]; ex.RemoveAt(index + 1); ex.RemoveAt(index); ex[index - 1] = new DivisionNode(Expr(left), Expr(right)); index--; } if (ex[index] is ModToken) { var left = ex[index - 1]; var right = ex[index + 1]; ex.RemoveAt(index + 1); ex.RemoveAt(index); ex[index - 1] = new ModuloNode(Expr(left), Expr(right)); index--; } } for (index = 0; index < ex.Count; index++) { if (ex[index] is AddToken) { var left = ex[index - 1]; var right = ex[index + 1]; ex.RemoveAt(index + 1); ex.RemoveAt(index); ex[index - 1] = new AdditionNode(Expr(left), Expr(right)); index--; } if (ex[index] is SubToken) { var left = ex[index - 1]; var right = ex[index + 1]; ex.RemoveAt(index + 1); ex.RemoveAt(index); ex[index - 1] = new SubtractionNode(Expr(left), Expr(right)); index--; } } for (index = 0; index < ex.Count; index++) { if (ex[index] is EqualToken) { var left = ex[index - 1]; var right = ex[index + 1]; ex.RemoveAt(index + 1); ex.RemoveAt(index); ex[index - 1] = new CompareNode(Expr(left), Expr(right), CompareType.Equal); index--; } if (ex[index] is NotEqualToken) { var left = ex[index - 1]; var right = ex[index + 1]; ex.RemoveAt(index + 1); ex.RemoveAt(index); ex[index - 1] = new CompareNode(Expr(left), Expr(right), CompareType.NotEqual); index--; } if (ex[index] is LessToken) { var left = ex[index - 1]; var right = ex[index + 1]; ex.RemoveAt(index + 1); ex.RemoveAt(index); ex[index - 1] = new CompareNode(Expr(left), Expr(right), CompareType.Less); index--; } if (ex[index] is LessEqualToken) { var left = ex[index - 1]; var right = ex[index + 1]; ex.RemoveAt(index + 1); ex.RemoveAt(index); ex[index - 1] = new CompareNode(Expr(left), Expr(right), CompareType.LessEqual); index--; } if (ex[index] is GreaterToken) { var left = ex[index - 1]; var right = ex[index + 1]; ex.RemoveAt(index + 1); ex.RemoveAt(index); ex[index - 1] = new CompareNode(Expr(left), Expr(right), CompareType.Greater); index--; } if (ex[index] is GreaterEqualToken) { var left = ex[index - 1]; var right = ex[index + 1]; ex.RemoveAt(index + 1); ex.RemoveAt(index); ex[index - 1] = new CompareNode(Expr(left), Expr(right), CompareType.GreaterEqual); index--; } } RootExpr = Expr(ex[0]); Console.WriteLine(RootExpr); } catch (ArgumentOutOfRangeException ex) { throw new ExpressionException(((Token)tokens[0]).Line, -1, "Invalid expression. (Forgot closing parenthesis?)"); } }
public FunctionStatement(FunctionNode fn) { Func = fn; }