From 32d6b7124cd48027926f5ed3dadea00480c3c918 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 3 Sep 2022 19:16:13 +1000 Subject: [PATCH] All leaks plugged for the time being --- scripts/small.toy | 12 ++++-------- source/interpreter.c | 28 ++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/scripts/small.toy b/scripts/small.toy index febace3..0c369df 100644 --- a/scripts/small.toy +++ b/scripts/small.toy @@ -1,12 +1,8 @@ -fn capture(count: int) { - print count; - print capture; - if (count > 5) { - return count; - } - return capture(count + 1); +fn panic() { + assert false, "This should only be seen once"; } -print capture(0); +panic(); +panic(); diff --git a/source/interpreter.c b/source/interpreter.c index cae4b1a..0e59365 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -550,6 +550,7 @@ static bool execAssert(Interpreter* interpreter) { if (IS_NULL(lhs) || !IS_TRUTHY(lhs)) { (*interpreter->assertOutput)(AS_STRING(rhs)); + freeLiteral(rhs); interpreter->panic = true; return false; } @@ -569,6 +570,7 @@ static bool execPrint(Interpreter* interpreter) { if (!parseIdentifierToValue(interpreter, &lit)) { return false; } + freeLiteral(idn); } printLiteralCustom(lit, interpreter->printOutput); @@ -657,7 +659,7 @@ static bool execInvert(Interpreter* interpreter) { if (!parseIdentifierToValue(interpreter, &lit)) { return false; } - freeLitreral(idn); + freeLiteral(idn); } if (IS_BOOLEAN(lit)) { @@ -849,7 +851,12 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) { } Literal val = popLiteralArray(&interpreter->stack); - parseIdentifierToValue(interpreter, &val); + + if (IS_IDENTIFIER(val)) { + Literal idn = val; + parseIdentifierToValue(interpreter, &val); + freeLiteral(idn); + } if (!IS_NULL(val) && !setScopeVariable(interpreter->scope, identifier, val, false)) { printf(ERROR "ERROR: Incorrect type assigned to variable \""); @@ -919,7 +926,11 @@ static bool execVarAssign(Interpreter* interpreter) { Literal rhs = popLiteralArray(&interpreter->stack); Literal lhs = popLiteralArray(&interpreter->stack); - parseIdentifierToValue(interpreter, &rhs); + if (IS_IDENTIFIER(rhs)) { + Literal idn = rhs; + parseIdentifierToValue(interpreter, &rhs); + freeLiteral(idn); + } if (!IS_IDENTIFIER(lhs)) { printf(ERROR "ERROR: Can't assign to a non-variable \""); @@ -932,6 +943,9 @@ static bool execVarAssign(Interpreter* interpreter) { printf(ERROR "ERROR: Undeclared variable \""); printLiteral(lhs); printf("\"\n" RESET); + + freeLiteral(lhs); + freeLiteral(rhs); return false; } @@ -939,6 +953,9 @@ static bool execVarAssign(Interpreter* interpreter) { printf(ERROR "ERROR Incorrect type assigned to variable \""); printLiteral(lhs); printf("\"\n" RESET); + + freeLiteral(lhs); + freeLiteral(rhs); return false; } @@ -1352,7 +1369,9 @@ static bool execFnCall(Interpreter* interpreter) { initLiteralArray(&correct); while(arguments.count) { - pushLiteralArray(&correct, popLiteralArray(&arguments)); + Literal lit = popLiteralArray(&arguments); + pushLiteralArray(&correct, lit); + freeLiteral(lit); } freeLiteralArray(&arguments); @@ -1361,6 +1380,7 @@ static bool execFnCall(Interpreter* interpreter) { ((NativeFn) AS_FUNCTION(func).bytecode )(interpreter, &correct); freeLiteralArray(&correct); + freeLiteral(identifier); return true; }