mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
String concatenation restricted to + and += signs
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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";
|
||||
Reference in New Issue
Block a user