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); }
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); }