diff --git a/docs/TODO.txt b/docs/TODO.txt index 409ceb3..1be5078 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -22,10 +22,10 @@ DONE: closures are explicitly supported DONE: functions are first-class citizens DONE: functions take a set number of parameters DONE: functions last argument can be a rest parameter +DONE: assert needs to kill the whole script, not just functions TODO: functions return a set number of values -TODO: assert needs to kill the whole script, not just functions TODO: ternary operator TODO: Nullish types diff --git a/scripts/error-test.toy b/scripts/error-test.toy deleted file mode 100644 index e69de29..0000000 diff --git a/source/interpreter.c b/source/interpreter.c index 0166725..cc9ec78 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -29,6 +29,8 @@ void initInterpreter(Interpreter* interpreter) { setInterpreterPrint(interpreter, stdoutWrapper); setInterpreterAssert(interpreter, stderrWrapper); + + interpreter->panic = false; } void freeInterpreter(Interpreter* interpreter) { @@ -139,6 +141,7 @@ static bool execAssert(Interpreter* interpreter) { if (IS_NULL(lhs) || !IS_TRUTHY(lhs)) { (*interpreter->assertOutput)(AS_STRING(rhs)); + interpreter->panic = true; return false; } @@ -858,6 +861,9 @@ static bool execFnCall(Interpreter* interpreter) { //execute the interpreter execInterpreter(&inner); + //adopt the panic state + interpreter->panic = inner.panic; + //accept the stack as the results LiteralArray returns; initLiteralArray(&returns); @@ -936,7 +942,7 @@ static void execInterpreter(Interpreter* interpreter) { unsigned char opcode = readByte(interpreter->bytecode, &interpreter->count); - while(opcode != OP_EOF && opcode != OP_SECTION_END) { + while(opcode != OP_EOF && opcode != OP_SECTION_END && !interpreter->panic) { switch(opcode) { case OP_ASSERT: if (!execAssert(interpreter)) { diff --git a/source/interpreter.h b/source/interpreter.h index ebdc6cd..020e55b 100644 --- a/source/interpreter.h +++ b/source/interpreter.h @@ -25,6 +25,8 @@ typedef struct Interpreter { // LiteralDictionary exports; //TODO: read-write - interface with Toy from C PrintFn printOutput; PrintFn assertOutput; + + bool panic; } Interpreter; void initInterpreter(Interpreter* interpreter); diff --git a/test/panic-within-functions.toy b/test/panic-within-functions.toy new file mode 100644 index 0000000..0c369df --- /dev/null +++ b/test/panic-within-functions.toy @@ -0,0 +1,8 @@ + + +fn panic() { + assert false, "This should only be seen once"; +} + +panic(); +panic();