From d12ad4f60d425e673da856e7b3e7fbb525f157c3 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 4 Sep 2022 08:51:19 +0100 Subject: [PATCH] Fixed codeStart issue --- scripts/small.toy | 9 +++------ source/interpreter.c | 6 +++++- source/interpreter.h | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/scripts/small.toy b/scripts/small.toy index 0c369df..6c156e2 100644 --- a/scripts/small.toy +++ b/scripts/small.toy @@ -1,8 +1,5 @@ - -fn panic() { - assert false, "This should only be seen once"; +for (var i = 0; i < 40; i++) { + print i; + print (string)i + " printed"; } - -panic(); -panic(); diff --git a/source/interpreter.c b/source/interpreter.c index 11727b8..cc8770f 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -441,6 +441,7 @@ void initInterpreter(Interpreter* interpreter) { interpreter->bytecode = NULL; interpreter->length = 0; interpreter->count = 0; + interpreter->codeStart = -1; initLiteralArray(&interpreter->stack); @@ -1405,6 +1406,7 @@ static bool execFnCall(Interpreter* interpreter) { inner.bytecode = AS_FUNCTION(func).bytecode; inner.length = func.as.function.length; inner.count = 0; + inner.codeStart = -1; inner.panic = false; initLiteralArray(&inner.stack); setInterpreterPrint(&inner, interpreter->printOutput); @@ -1614,7 +1616,9 @@ static bool execFnReturn(Interpreter* interpreter) { //the heart of toy static void execInterpreter(Interpreter* interpreter) { //set the starting point for the interpreter - interpreter->codeStart = interpreter->count; + if (interpreter->codeStart == -1) { + interpreter->codeStart = interpreter->count; + } unsigned char opcode = readByte(interpreter->bytecode, &interpreter->count); diff --git a/source/interpreter.h b/source/interpreter.h index 0eb82f5..e59e3af 100644 --- a/source/interpreter.h +++ b/source/interpreter.h @@ -15,7 +15,7 @@ typedef struct Interpreter { unsigned char* bytecode; int length; int count; - int codeStart; //for jumps + int codeStart; //BUGFIX: for jumps, must be initialized to -1 LiteralArray literalCache; //read-only - built from the bytecode, refreshed each time new bytecode is provided //operation