moonscript
moonscript copied to clipboard
Compile-time tail-call avoidance flag?
It would be cool if there was a CLI flag that compiled Moonscript with an avoidance of tail calls. The use case is debugging, of course.
This is probably hard to accomplish, but I figured I would make the suggestion anyway.
How do you think it should modify the code?
Would something like this work?
return f!
To:
tmp = f()
return tmp
LuaJIT may outsmart this, though. In that case, creating a 1-element table could also work...
That won't work for functions that return multiple values, so a table would be needed
Just wrap tail calls like so:
function tail(...)
return ...
end
function bar(i)
print(debug.traceback())
return i + 1, i + 2
end
function foo(i)
-- The tail call
return tail(bar(i))
end
print(foo(1))
Works in both Lua 5.1 and LuaJIT.