Scriptor/pharen — Issue #35: Compiler sometimes generates "return" inside TCO'ed "while" loops
Author: catseye
Created: 2011-12-15 22:19:23+00:00
State: open

I found this while trying to play with `repl.phn` -- it doesn't actually loop (so I guess it's just a `REP`, not a `REPL`...) Looking at the generated code, the problem is evident: in the middle of the `while` loop that was generated, the compiler also generated the line

return fwrite(STDOUT, (evaluate($code) . "\n"));

which breaks out of the loop.

Conceptually, if an expression is only being evaluated for its side-effects, it shouldn't try to `return`, at least not inside code that is subject to tail-call optimization. I'm not entirely sure of a good way to address this yet, but one idea would be to assign the result of evaluation to a local variable, and not generate `return` until the end of the function (at which point you just return that local variable.)