private NumericalNode Term() { NumericalNode result = Factor(); Token token; while (CurrentToken.Type != TokenType.EOF) { token = CurrentToken; if (CurrentToken.Type == TokenType.TIMES) { Eat(TokenType.TIMES); } else if (CurrentToken.Type == TokenType.INTEGER_DIV) { Eat(TokenType.INTEGER_DIV); } else if (CurrentToken.Type == TokenType.FLOAT_DIV) { Eat(TokenType.FLOAT_DIV); } else { break; } result = new BinaryOperator(result, token, Factor()); } return(result); }
public dynamic VisitBinaryOperator(NumericalNode node) { dynamic result; var TmpNode = node as BinaryOperator; switch (TmpNode.Op.Type) { case TokenType.ADD: result = Visit(TmpNode.Left) + Visit(TmpNode.Right); break; case TokenType.SUBTRACT: result = Visit(TmpNode.Left) - Visit(TmpNode.Right); break; case TokenType.TIMES: result = Visit(TmpNode.Left) * Visit(TmpNode.Right); break; case TokenType.INTEGER_DIV: result = Visit(TmpNode.Left) / Visit(TmpNode.Right); break; case TokenType.FLOAT_DIV: result = Visit(TmpNode.Left) / Visit(TmpNode.Right); break; default: throw new Exception("Unrecognised op"); } return(result); }
public dynamic Visit(NumericalNode node) { var nodeName = node.GetType().Name; var visitorMethod = this.GetType().GetMethod("Visit" + nodeName); return(visitorMethod.Invoke(this, new object[] { node })); }
public dynamic VisitNum(NumericalNode node) { var num = node as Num; if (num.Value.Value.Contains('.')) { return(float.Parse(num.Value.Value)); } return(int.Parse((node as Num).Value.Value)); }
public int VisitUnaryOperator(NumericalNode node) { var tmpNode = node as UnaryOperator; if (tmpNode.Op.Type == TokenType.ADD) { return(Visit(tmpNode.Node)); } else if (tmpNode.Op.Type == TokenType.SUBTRACT) { return(-Visit(tmpNode.Node)); } else { throw new Exception("Runtime Error: Could not interpret unary operator"); } }
private NumericalNode Expr() { NumericalNode result = Term(); Token token; while (CurrentToken.Type == TokenType.ADD || CurrentToken.Type == TokenType.SUBTRACT) { token = CurrentToken; if (CurrentToken.Type == TokenType.ADD) { Eat(TokenType.ADD); } else if (CurrentToken.Type == TokenType.SUBTRACT) { Eat(TokenType.SUBTRACT); } result = new BinaryOperator(result, token, Term()); } return(result); }
public int VisitVariable(NumericalNode node) { var varNode = node as Variable; return(GlobalScope[varNode.Value.Value]); }
public Assignment(ASTNode Left, Token Op, NumericalNode Right) { this.Left = Left; this.Right = Right; this.Op = Op; }
public UnaryOperator(NumericalNode node, Token op) { Node = node; Op = op; }
public BinaryOperator(NumericalNode Left, Token Op, NumericalNode Right) { this.Left = Left; this.Right = Right; this.Op = Op; }