diff --git a/source/toy_interpreter.c b/source/toy_interpreter.c index e428c07..cb74446 100644 --- a/source/toy_interpreter.c +++ b/source/toy_interpreter.c @@ -224,19 +224,28 @@ static void consumeShort(Toy_Interpreter* interpreter, unsigned short bytes, uns static bool execAssert(Toy_Interpreter* interpreter) { Toy_Literal rhs = Toy_popLiteralArray(&interpreter->stack); Toy_Literal lhs = Toy_popLiteralArray(&interpreter->stack); - Toy_parseIdentifierToValue(interpreter, &lhs); + + Toy_Literal lhsIdn = lhs; + if (TOY_IS_IDENTIFIER(lhs) && Toy_parseIdentifierToValue(interpreter, &lhs)) { + Toy_freeLiteral(lhsIdn); + } if (!TOY_IS_STRING(rhs)) { interpreter->errorOutput("The assert keyword needs a string as the second argument, received: "); Toy_printLiteralCustom(rhs, interpreter->errorOutput); interpreter->errorOutput("\n"); + + Toy_freeLiteral(rhs); + Toy_freeLiteral(lhs); return false; } if (TOY_IS_NULL(lhs) || !TOY_IS_TRUTHY(lhs)) { (*interpreter->assertOutput)(Toy_toCString(TOY_AS_STRING(rhs))); - Toy_freeLiteral(rhs); interpreter->panic = true; + + Toy_freeLiteral(rhs); + Toy_freeLiteral(lhs); return false; } @@ -1086,16 +1095,14 @@ static bool execFalseJump(Toy_Interpreter* interpreter) { //actually jump Toy_Literal lit = Toy_popLiteralArray(&interpreter->stack); - bool freeLit = false; - if (TOY_IS_IDENTIFIER(lit)) { - Toy_Literal idn = lit; - Toy_parseIdentifierToValue(interpreter, &lit); - Toy_freeLiteral(idn); - freeLit = true; + Toy_Literal litIdn = lit; + if (TOY_IS_IDENTIFIER(lit) && Toy_parseIdentifierToValue(interpreter, &lit)) { + Toy_freeLiteral(litIdn); } if (TOY_IS_NULL(lit)) { interpreter->errorOutput("Null detected in comparison\n"); + Toy_freeLiteral(lit); return false; } @@ -1103,9 +1110,7 @@ static bool execFalseJump(Toy_Interpreter* interpreter) { interpreter->count = target + interpreter->codeStart; } - if (freeLit) { - Toy_freeLiteral(lit); - } + Toy_freeLiteral(lit); return true; } @@ -1995,7 +2000,6 @@ static void execInterpreter(Toy_Interpreter* interpreter) { } break; - case TOY_OP_FN_RETURN: if (!execFnReturn(interpreter)) { return; diff --git a/source/toy_refstring.c b/source/toy_refstring.c index 08ccca0..716d291 100644 --- a/source/toy_refstring.c +++ b/source/toy_refstring.c @@ -96,4 +96,4 @@ bool Toy_equalsRefStringCString(Toy_RefString* lhs, char* cstring) { //same string return strncmp(lhs->data, cstring, lhs->length) == 0; -} \ No newline at end of file +} diff --git a/test/scripts/ternary-expressions.toy b/test/scripts/ternary-expressions.toy index be8fda6..1e8d72b 100644 --- a/test/scripts/ternary-expressions.toy +++ b/test/scripts/ternary-expressions.toy @@ -23,13 +23,13 @@ //test ambiguous syntax { - var a = 1; - var b = 2; - var c = 3; - var d = 4; - var e = 5; + var aa = 1; + var bbb = 2; + var cccc = 3; + var ddddd = 4; + var eeeeee = 5; - assert a ? b ? c : d : e == 3, "Ambiguous syntax failed"; + assert aa ? bbb ? cccc : ddddd : eeeeee, "Ambiguous syntax failed"; }