diff --git a/docs/TODO.txt b/docs/TODO.txt index ec13803..57093d2 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -33,18 +33,18 @@ DONE: Assertion-based test scripts DONE: Import/export keywords DONE: A way to check the type of a variable (typeOf keyword) DONE: slice and dot notation around the builtin _index and _dot functions +DONE: maximum recursion/function depth +TODO: nested compound assignment +TODO: better sugar for _push and _pop TODO: ternary operator TODO: Nullish types? TODO: hooks on the external libraries, triggered on import TODO: standard library TODO: external script runner library TODO: document how it all works - book? -TODO: maximum recursion/function depth TODO: better API -TODO: better sugar for _push and _pop -TODO: nested compound assignment TODO: packaging for release? NOPE: a = b = c = 1; diff --git a/scripts/small.toy b/scripts/small.toy index 1a2a795..f7cc170 100644 --- a/scripts/small.toy +++ b/scripts/small.toy @@ -1,25 +1,10 @@ +fn loop(count) { + print count++; -fn makeCounter() { - var total: int = 0; - - fn counter(): int { - return ++total; - } - - return counter; + loop(count); } -var tally = makeCounter(); - -print tally(); //1 -print tally(); //2 -print tally(); //3 - -export tally; - -//heck yeah! -import tally as tally2; -print tally2(); //4 +loop(0); \ No newline at end of file diff --git a/source/interpreter.c b/source/interpreter.c index 81095e0..9c3b171 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -991,6 +991,13 @@ static void execInterpreter(Interpreter*); static void readInterpreterSections(Interpreter* interpreter); static bool execFnCall(Interpreter* interpreter) { + //BUGFIX: depth check - don't drown! + if (interpreter->depth >= 1000) { + interpreter->errorOutput("Depth check failed\n"); + interpreter->panic = true; + return false; + } + LiteralArray arguments; initLiteralArray(&arguments); @@ -1057,6 +1064,7 @@ static bool execFnCall(Interpreter* interpreter) { inner.length = func.as.function.length; inner.count = 0; inner.codeStart = -1; + inner.depth = interpreter->depth + 1; inner.panic = false; initLiteralArray(&inner.stack); inner.exports = interpreter->exports; @@ -2368,6 +2376,7 @@ void runInterpreter(Interpreter* interpreter, unsigned char* bytecode, int lengt initLiteralArray(&interpreter->stack); + interpreter->depth = 0; interpreter->panic = false; //prep the bytecode diff --git a/source/interpreter.h b/source/interpreter.h index edce019..8ebe8e1 100644 --- a/source/interpreter.h +++ b/source/interpreter.h @@ -27,6 +27,7 @@ typedef struct Interpreter { PrintFn assertOutput; PrintFn errorOutput; + int depth; //don't overflow bool panic; } Interpreter;