Compare commits

...

2 commits

View file

@ -110,10 +110,23 @@ class Interpreter implements Expr.Visitor<Object> {
return (String) left + (String) right; return (String) left + (String) right;
} }
if (left instanceof String) {
return (String) left + stringify(right);
}
if (right instanceof String) {
return stringify(left) + (String) right;
}
throw new RuntimeError(expr.operator, "Operands must be two numbers or two strings."); throw new RuntimeError(expr.operator, "Operands must be two numbers or two strings.");
case SLASH: case SLASH: {
checkNumberOperands(expr.operator, left, right); checkNumberOperands(expr.operator, left, right);
return (double) left / (double) right; double rhs = (double) right;
if (rhs == 0) {
throw new RuntimeError(expr.operator, "Division by zero.");
}
return (double) left / rhs;
}
case STAR: case STAR:
checkNumberOperands(expr.operator, left, right); checkNumberOperands(expr.operator, left, right);
return (double) left * (double) right; return (double) left * (double) right;