diff options
author | Tom Willemse | 2020-11-11 21:53:12 -0800 |
---|---|---|
committer | Tom Willemse | 2020-11-11 21:53:12 -0800 |
commit | 5ff2f8be5aa459f0a7365e63d633e9976c6ed926 (patch) | |
tree | c6c7589fc334a83d9856b0816133a9374c4053f8 /src/com/craftinginterpreters | |
parent | b31528d593fd4bbeade34f1fc9ae7b0672d7c1fa (diff) | |
download | crafting-interpreters-5ff2f8be5aa459f0a7365e63d633e9976c6ed926.tar.gz crafting-interpreters-5ff2f8be5aa459f0a7365e63d633e9976c6ed926.zip |
Support converting operands to strings for the + operator
Diffstat (limited to 'src/com/craftinginterpreters')
-rw-r--r-- | src/com/craftinginterpreters/lox/Interpreter.java | 8 |
1 files changed, 8 insertions, 0 deletions
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<Object> { 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); |