Interpreter adopts inner interpreter panic state on assert failure

This commit is contained in:
2022-08-27 12:07:41 +01:00
parent 90e5a5d08b
commit e523a6f60a
5 changed files with 18 additions and 2 deletions

View File

@@ -22,10 +22,10 @@ DONE: closures are explicitly supported
DONE: functions are first-class citizens DONE: functions are first-class citizens
DONE: functions take a set number of parameters DONE: functions take a set number of parameters
DONE: functions last argument can be a rest parameter 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: functions return a set number of values
TODO: assert needs to kill the whole script, not just functions
TODO: ternary operator TODO: ternary operator
TODO: Nullish types TODO: Nullish types

View File

View File

@@ -29,6 +29,8 @@ void initInterpreter(Interpreter* interpreter) {
setInterpreterPrint(interpreter, stdoutWrapper); setInterpreterPrint(interpreter, stdoutWrapper);
setInterpreterAssert(interpreter, stderrWrapper); setInterpreterAssert(interpreter, stderrWrapper);
interpreter->panic = false;
} }
void freeInterpreter(Interpreter* interpreter) { void freeInterpreter(Interpreter* interpreter) {
@@ -139,6 +141,7 @@ static bool execAssert(Interpreter* interpreter) {
if (IS_NULL(lhs) || !IS_TRUTHY(lhs)) { if (IS_NULL(lhs) || !IS_TRUTHY(lhs)) {
(*interpreter->assertOutput)(AS_STRING(rhs)); (*interpreter->assertOutput)(AS_STRING(rhs));
interpreter->panic = true;
return false; return false;
} }
@@ -858,6 +861,9 @@ static bool execFnCall(Interpreter* interpreter) {
//execute the interpreter //execute the interpreter
execInterpreter(&inner); execInterpreter(&inner);
//adopt the panic state
interpreter->panic = inner.panic;
//accept the stack as the results //accept the stack as the results
LiteralArray returns; LiteralArray returns;
initLiteralArray(&returns); initLiteralArray(&returns);
@@ -936,7 +942,7 @@ static void execInterpreter(Interpreter* interpreter) {
unsigned char opcode = readByte(interpreter->bytecode, &interpreter->count); 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) { switch(opcode) {
case OP_ASSERT: case OP_ASSERT:
if (!execAssert(interpreter)) { if (!execAssert(interpreter)) {

View File

@@ -25,6 +25,8 @@ typedef struct Interpreter {
// LiteralDictionary exports; //TODO: read-write - interface with Toy from C // LiteralDictionary exports; //TODO: read-write - interface with Toy from C
PrintFn printOutput; PrintFn printOutput;
PrintFn assertOutput; PrintFn assertOutput;
bool panic;
} Interpreter; } Interpreter;
void initInterpreter(Interpreter* interpreter); void initInterpreter(Interpreter* interpreter);

View File

@@ -0,0 +1,8 @@
fn panic() {
assert false, "This should only be seen once";
}
panic();
panic();