munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.99k stars 1.05k forks source link

[discussion] test case for nested call with argument(s)? #888

Closed drewbanas closed 3 years ago

drewbanas commented 3 years ago

The code below works fine with Lox, but a bug has eluded my modded implementation for months. It might be interesting to have a similar case in the test scripts?

fun returnArg(arg){
    return arg;
}

fun returnFunCallWithArg(func, arg){
    return returnArg(func)(arg);
}

fun printArg(arg){
    print arg;
}

returnFunCallWithArg(printArg, "hello world");

(used javascript for highlighting :-))

I happen to be experimenting with tail calls (reclaiming the call frame before calling if what is returned is a call) when I noticed that my port previously missed this. My bug had to do with the compilation of arguments relative to emitting OP_CALL.

Btw, I think it's really cool how the code above works in Lox to begin with. Thanks!

nocturn9x commented 3 years ago

Nice snippet! I'll add it to my test suite, thanks :D (it runs on my implementation btw)

munificent commented 3 years ago

Added, thanks!