ursalang / ursa

A friendly, stable general-purpose programming language
https://ursalang.github.io
3 stars 1 forks source link

Tail recursion elimination #17

Open rrthomas opened 9 months ago

rrthomas commented 9 months ago

Add an exception ArkTailCall that replaces ArkCall for tail calls. Use it whenever we're in tail position, which we'll track with another argument to toAST, which is true when the parent is in tail position. Then if we're in tail position and our parent is and we're calling the current function (another new argument to toAST) then use ArkTailCall instead of ArkCall.

apt1002 commented 8 months ago

Why only if we're calling the current function?

rrthomas commented 5 months ago

The situation has changed. For the reference interpreter it may be a good idea to eliminate tail calls, but it's less urgent. The worst we can do is exhaust memory (that stack will be on the heap).

ES2015 has tail calls in strict mode; we should ensure we're using this where possible.

rrthomas commented 2 weeks ago

See https://www.cs.tufts.edu/~nr/cs257/archive/will-clinger/proper-tail-space.pdf