moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

Compile-time tail-call avoidance flag?

Open aleclarson opened this issue 7 years ago • 4 comments

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.

aleclarson avatar Apr 16 '18 00:04 aleclarson

How do you think it should modify the code?

leafo avatar Apr 17 '18 03:04 leafo

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

refi64 avatar Apr 17 '18 03:04 refi64

That won't work for functions that return multiple values, so a table would be needed

leafo avatar Apr 17 '18 05:04 leafo

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.

aleclarson avatar Apr 17 '18 16:04 aleclarson