Fixed codeStart issue

This commit is contained in:
2022-09-04 08:51:19 +01:00
parent 86061cb74e
commit d12ad4f60d
3 changed files with 9 additions and 8 deletions

View File

@@ -1,8 +1,5 @@
for (var i = 0; i < 40; i++) {
fn panic() { print i;
assert false, "This should only be seen once"; print (string)i + " printed";
} }
panic();
panic();

View File

@@ -441,6 +441,7 @@ void initInterpreter(Interpreter* interpreter) {
interpreter->bytecode = NULL; interpreter->bytecode = NULL;
interpreter->length = 0; interpreter->length = 0;
interpreter->count = 0; interpreter->count = 0;
interpreter->codeStart = -1;
initLiteralArray(&interpreter->stack); initLiteralArray(&interpreter->stack);
@@ -1405,6 +1406,7 @@ static bool execFnCall(Interpreter* interpreter) {
inner.bytecode = AS_FUNCTION(func).bytecode; inner.bytecode = AS_FUNCTION(func).bytecode;
inner.length = func.as.function.length; inner.length = func.as.function.length;
inner.count = 0; inner.count = 0;
inner.codeStart = -1;
inner.panic = false; inner.panic = false;
initLiteralArray(&inner.stack); initLiteralArray(&inner.stack);
setInterpreterPrint(&inner, interpreter->printOutput); setInterpreterPrint(&inner, interpreter->printOutput);
@@ -1614,7 +1616,9 @@ static bool execFnReturn(Interpreter* interpreter) {
//the heart of toy //the heart of toy
static void execInterpreter(Interpreter* interpreter) { static void execInterpreter(Interpreter* interpreter) {
//set the starting point for the 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); unsigned char opcode = readByte(interpreter->bytecode, &interpreter->count);

View File

@@ -15,7 +15,7 @@ typedef struct Interpreter {
unsigned char* bytecode; unsigned char* bytecode;
int length; int length;
int count; 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 LiteralArray literalCache; //read-only - built from the bytecode, refreshed each time new bytecode is provided
//operation //operation