private AST Term() { var node = Factor(); while (currentToken.TokenType == TokenType.MUL || currentToken.TokenType == TokenType.INTEGER_DIV || currentToken.TokenType == TokenType.FLOAT_DIV) { var token = currentToken; if (token.TokenType == TokenType.MUL) { Eat(TokenType.MUL); } else if (token.TokenType == TokenType.INTEGER_DIV) { Eat(TokenType.INTEGER_DIV); } else if (token.TokenType == TokenType.FLOAT_DIV) { Eat(TokenType.FLOAT_DIV); } node = new BinOp(left: node, op: token, right: Factor()); } return(node); }
public override int VisitBinOp(BinOp node) { if (node.op.TokenType == TokenType.PLUS) { return(Visit(node.left) + Visit(node.right)); } if (node.op.TokenType == TokenType.MINUS) { return(Visit(node.left) - Visit(node.right)); } if (node.op.TokenType == TokenType.MUL) { return(Visit(node.left) * Visit(node.right)); } if (node.op.TokenType == TokenType.DIV) { return(Visit(node.left) / Visit(node.right)); } throw new Exception("Unknown node!"); }
public AST Expr() { var node = Term(); while (currentToken.TokenType == TokenType.PLUS || currentToken.TokenType == TokenType.MINUS) { var token = currentToken; if (token.TokenType == TokenType.PLUS) { Eat(TokenType.PLUS); } if (token.TokenType == TokenType.MINUS) { Eat(TokenType.MINUS); } node = new BinOp(left: node, op: token, right: Term()); } return(node); }
public override object VisitBinOp(BinOp node) { if (node.op.TokenType == TokenType.PLUS) { return((double)Visit(node.left) + (double)Visit(node.right)); } if (node.op.TokenType == TokenType.MINUS) { return((double)Visit(node.left) - (double)Visit(node.right)); } if (node.op.TokenType == TokenType.MUL) { return((double)Visit(node.left) * (double)Visit(node.right)); } if (node.op.TokenType == TokenType.INTEGER_DIV) { return(double.Parse(Visit(node.left).ToString()) / double.Parse(Visit(node.right).ToString())); } if (node.op.TokenType == TokenType.FLOAT_DIV) { return((double)Visit(node.left) / (double)Visit(node.right)); } throw new Exception("Unknown node!"); }
public abstract int VisitBinOp(BinOp op);
public abstract object VisitBinOp(BinOp op);