String concatenation restricted to + and += signs

This commit is contained in:
2023-02-05 15:15:32 +00:00
parent 7ea249f723
commit 41d274177a
3 changed files with 13 additions and 2 deletions

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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";