object IVisitor <object> .visit(BinOpNode node) { Node left = node.getLeft(); Node right = node.getRight(); // decorate left.type = Convert.ToString(left.accept(this)); right.type = Convert.ToString(right.accept(this)); if (left.type == Token.INT && right.type == Token.INT) { node.type = Token.INT; } else if (left.type == Token.STRING || right.type == Token.STRING) { node.type = Token.STRING; } else if (left.type == Token.BOOL || right.type == Token.BOOL) { node.type = Token.BOOL; } checkOperationIsAllowed(node, left, right); return(node.type); }
object IVisitor <object> .visit(BinOpNode node) { object left = evaluate(node.getLeft()); object right = evaluate(node.getRight()); switch (node.value) { case "+": // concatenation if (node.getLeft().type.Equals(Token.STRING) & node.getRight().type.Equals(Token.STRING)) { return(left.ToString() + right.ToString()); } else { return(Convert.ToInt32(left) + Convert.ToInt32(right)); } case "-": return(Convert.ToInt32(left) - Convert.ToInt32(right)); case "*": return(Convert.ToInt32(left) * Convert.ToInt32(right)); case "/": return(Convert.ToInt32(left) / Convert.ToInt32(right)); case "=": if (node.getLeft().type.Equals(Token.STRING) & node.getRight().type.Equals(Token.STRING)) { return(left.ToString().Equals(right.ToString())); } else { return(Convert.ToInt32(left) == Convert.ToInt32(right)); } case "<": if (node.getLeft().type.Equals(Token.STRING) & node.getRight().type.Equals(Token.STRING)) { return(left.ToString().Length < right.ToString().Length); } else { return(Convert.ToInt32(left) < Convert.ToInt32(right)); } case "&": return(Convert.ToBoolean(left) & Convert.ToBoolean(right)); default: break; } return(null); }
private Node expr() { Node left = term(); while (currentToken.type == Token.ADD | currentToken.type == Token.SUB) { Node node = new BinOpNode(currentToken); nextToken(); node.addChild(left); node.addChild(term()); left = node; } return(left); }
private Node term() { Node left = factor(); while (currentToken.type == Token.MUL | currentToken.type == Token.DIV | currentToken.type == Token.LT | currentToken.type == Token.AND | currentToken.type == Token.EQ) { Node node = new BinOpNode(currentToken); nextToken(); node.addChild(left); node.addChild(factor()); left = node; } return(left); }
object IVisitor <object> .visit(BinOpNode node) { return(null); }