Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile-time tail-call avoidance flag? #364

Open
aleclarson opened this issue Apr 16, 2018 · 4 comments
Open

Compile-time tail-call avoidance flag? #364

aleclarson opened this issue Apr 16, 2018 · 4 comments

Comments

@aleclarson
Copy link

aleclarson commented Apr 16, 2018

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.

@leafo
Copy link
Owner

leafo commented Apr 17, 2018

How do you think it should modify the code?

@refi64
Copy link

refi64 commented Apr 17, 2018

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

@leafo
Copy link
Owner

leafo commented Apr 17, 2018

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

@aleclarson
Copy link
Author

aleclarson commented Apr 17, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants