public object VisitBinaryOperation(BinaryOperation node) { TokenType opType = node.operat.type; object nodeLeft = Visit(node.left); object nodeRight = Visit(node.right); if (nodeLeft is int && nodeRight is int) { return(IntBinaryOperations(opType, (int)nodeLeft, (int)nodeRight)); } else if (nodeLeft is string && nodeRight is string) { return(StringBinaryOperations(opType, (string)nodeLeft, (string)nodeRight)); } else if (nodeLeft is bool && nodeRight is bool) { return(BoolBinaryOperations(opType, (bool)nodeLeft, (bool)nodeRight)); } throw new InterpreterError("VisitBinaryOperation error"); }
// <term> ::= <factor> ( ("MUL" | "DIV") <factor> )* private AST Term() { AST node = Factor(); while (this.currentToken.type == TokenType.MUL || this.currentToken.type == TokenType.DIV) { Token token = this.currentToken; if (token.type == TokenType.MUL) { Eat(TokenType.MUL); } else if (token.type == TokenType.DIV) { Eat(TokenType.DIV); } node = new BinaryOperation(node, token, Factor()); } return(node); }
// <expr> ::= <term> ( ("PLUS" | "MINUS" | "EQUAL" | "LESS" | "AND" | "NOT") <term> )* private AST Expr() { AST node = Term(); while (this.currentToken.type == TokenType.PLUS || this.currentToken.type == TokenType.MINUS || this.currentToken.type == TokenType.EQUAL || this.currentToken.type == TokenType.LESS || this.currentToken.type == TokenType.AND || this.currentToken.type == TokenType.NOT) { Token token = this.currentToken; if (token.type == TokenType.PLUS) { Eat(TokenType.PLUS); } else if (token.type == TokenType.MINUS) { Eat(TokenType.MINUS); } else if (token.type == TokenType.EQUAL) { Eat(TokenType.EQUAL); } else if (token.type == TokenType.LESS) { Eat(TokenType.LESS); } else if (token.type == TokenType.AND) { Eat(TokenType.AND); } else if (token.type == TokenType.NOT) { Eat(TokenType.NOT); } node = new BinaryOperation(node, token, Term()); } return(node); }