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

@@ -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)) {

View File

@@ -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);