mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Interpreter adopts inner interpreter panic state on assert failure
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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)) {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
8
test/panic-within-functions.toy
Normal file
8
test/panic-within-functions.toy
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
|
||||||
|
fn panic() {
|
||||||
|
assert false, "This should only be seen once";
|
||||||
|
}
|
||||||
|
|
||||||
|
panic();
|
||||||
|
panic();
|
||||||
Reference in New Issue
Block a user