Support converting operands to strings for the + operator

This commit is contained in:
Tom Willemse 2020-11-11 21:53:12 -08:00
parent b31528d593
commit 5ff2f8be5a

View file

@ -110,6 +110,14 @@ 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);