diff --git a/source/toy_interpreter.c b/source/toy_interpreter.c index 1706000..f3f1280 100644 --- a/source/toy_interpreter.c +++ b/source/toy_interpreter.c @@ -376,7 +376,7 @@ static bool execArithmetic(Toy_Interpreter* interpreter, Toy_Opcode opcode) { } //special case for string concatenation ONLY - if (TOY_IS_STRING(lhs) && TOY_IS_STRING(rhs)) { + if (TOY_IS_STRING(lhs) && TOY_IS_STRING(rhs) && (opcode == TOY_OP_ADDITION || opcode == TOY_OP_VAR_ADDITION_ASSIGN)) { //check for overflow int totalLength = TOY_AS_STRING(lhs)->length + TOY_AS_STRING(rhs)->length; if (totalLength > TOY_MAX_STRING_LENGTH) { diff --git a/source/toy_parser.c b/source/toy_parser.c index 974c9ff..a559da1 100644 --- a/source/toy_parser.c +++ b/source/toy_parser.c @@ -983,7 +983,7 @@ static bool calcStaticBinaryArithmetic(Toy_Parser* parser, Toy_ASTNode** nodeHan Toy_Literal result = TOY_TO_NULL_LITERAL; //special case for string concatenation ONLY - if (TOY_IS_STRING(lhs) && TOY_IS_STRING(rhs)) { + if (TOY_IS_STRING(lhs) && TOY_IS_STRING(rhs) && (*nodeHandle)->binary.opcode == TOY_OP_ADDITION) { //check for overflow int totalLength = TOY_AS_STRING(lhs)->length + TOY_AS_STRING(rhs)->length; if (totalLength > TOY_MAX_STRING_LENGTH) { diff --git a/test/scripts/arithmetic.toy b/test/scripts/arithmetic.toy index a2ed656..27d0c4f 100644 --- a/test/scripts/arithmetic.toy +++ b/test/scripts/arithmetic.toy @@ -28,4 +28,15 @@ a %= 8; assert a == 4, "%= failed"; +//strings as special cases +var s = "foo"; + +assert s + "bar" == "foobar", "string addition failed"; +assert s == "foo", "string addition failed (was too sticky)"; + +s += "bar"; + +assert s == "foobar", "string addition failed (wasn't sticky enough)"; + + print "All good"; \ No newline at end of file