From 5ff2f8be5aa459f0a7365e63d633e9976c6ed926 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Wed, 11 Nov 2020 21:53:12 -0800 Subject: Support converting operands to strings for the + operator --- src/com/craftinginterpreters/lox/Interpreter.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/com/craftinginterpreters/lox/Interpreter.java b/src/com/craftinginterpreters/lox/Interpreter.java index 24cf1c0..57e697c 100644 --- a/src/com/craftinginterpreters/lox/Interpreter.java +++ b/src/com/craftinginterpreters/lox/Interpreter.java @@ -110,6 +110,14 @@ class Interpreter implements Expr.Visitor { 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."); case SLASH: checkNumberOperands(expr.operator, left, right); -- cgit v1.2.3-54-g00ecf