From f36289786e23f7e7c09dbe4968f0865aa9388b60 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 26 Aug 2022 11:59:09 +0100 Subject: [PATCH] Fixed a repl bug --- source/compiler.c | 1 + source/interpreter.c | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/source/compiler.c b/source/compiler.c index ce925b2..5631832 100644 --- a/source/compiler.c +++ b/source/compiler.c @@ -162,6 +162,7 @@ static int writeNodeCompoundToCache(Compiler* compiler, Node* node) { static int writeNodeCollectionToCache(Compiler* compiler, Node* node) { LiteralArray* store = ALLOCATE(LiteralArray, 1); + initLiteralArray(store); //ensure each literal value is in the cache, individually for (int i = 0; i < node->fnCollection.count; i++) { diff --git a/source/interpreter.c b/source/interpreter.c index 7c4b8e2..c361227 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -807,7 +807,7 @@ static bool execFnCall(Interpreter* interpreter) { pushLiteralArray(&returns, popLiteralArray(&inner.stack)); //NOTE: also reverses the order } - //TODO: remove this when multiple assignment is enabled + //TODO: remove this when multiple assignment is enabled - note the BUGFIX that balances the stack if (returns.count > 1) { printf(ERROR "ERROR: Too many values returned (multiple returns not yet implemented)\n" RESET); @@ -1336,6 +1336,11 @@ void runInterpreter(Interpreter* interpreter, unsigned char* bytecode, int lengt //execute the interpreter execInterpreter(interpreter); + //BUGFIX: clear the stack (for repl - stack must be balanced) + while(interpreter->stack.count > 0) { + popLiteralArray(&interpreter->stack); + } + //free the bytecode immediately after use FREE_ARRAY(unsigned char, interpreter->bytecode, interpreter->length); }