diff --git a/source/toy_interpreter.c b/source/toy_interpreter.c index caa1755..bc3b5f2 100644 --- a/source/toy_interpreter.c +++ b/source/toy_interpreter.c @@ -1229,12 +1229,8 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) { //get the function literal Toy_Literal func = identifier; - - if (!Toy_parseIdentifierToValue(interpreter, &func)) { - Toy_freeLiteralArray(&arguments); - Toy_freeLiteral(stackSize); + if (!TOY_IS_FUNCTION(func) && Toy_parseIdentifierToValue(interpreter, &func)) { Toy_freeLiteral(identifier); - return false; } if (!TOY_IS_FUNCTION(func) && !TOY_IS_FUNCTION_NATIVE(func)) { @@ -1271,7 +1267,6 @@ static bool execFnCall(Toy_Interpreter* interpreter, bool looseFirstArgument) { Toy_freeLiteralArray(&arguments); Toy_freeLiteral(func); Toy_freeLiteral(stackSize); - Toy_freeLiteral(identifier); return ret; } diff --git a/test/scripts/function-within-function-bugfix.toy b/test/scripts/function-within-function-bugfix.toy new file mode 100644 index 0000000..91b5c4a --- /dev/null +++ b/test/scripts/function-within-function-bugfix.toy @@ -0,0 +1,30 @@ +{ + fn a() { + fn b() { + return 42; + } + + return b; + } + + assert a()() == 42, "function within function failed"; +} + +{ + fn a() { + fn b() { + fn c() { + return 42; + } + + return c; + } + + return b; + } + + assert a()()() == 42, "function within function within function failed"; +} + + +print "All good"; diff --git a/test/test_interpreter.c b/test/test_interpreter.c index 739cb84..e8c5a9f 100644 --- a/test/test_interpreter.c +++ b/test/test_interpreter.c @@ -117,6 +117,7 @@ int main() { "dot-chaining.toy", "dot-modulo-bugfix.toy", "dottify-bugfix.toy", + "function-within-function-bugfix.toy", "functions.toy", "index-arrays.toy", "index-assignment-both-bugfix.toy",