Scriptor / pharen

Lisp to PHP Compiler
http://pharen.org
BSD 3-Clause "New" or "Revised" License
218 stars 31 forks source link

Compiler sometimes generates "return" inside TCO'ed "while" loops #35

Closed catseye closed 12 years ago

catseye commented 12 years ago

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.)

Scriptor commented 12 years ago

Sorry for the delay in responding. It looks like this error was caused by a typo that closed the let binding before the rest of the code. Otherwise it should be functional now. Coincidentally this brought up another bug since the (exit 0) line should be returned by the resulting PHP but isn't.

For future reference, the issue for this new problem is at https://github.com/Scriptor/pharen/issues/36.