ajkachnic / bliss

An elegant, dynamically typed programming language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add Tail Call Elimination (TCE)

ajkachnic opened this issue · comments

What?

bliss should support Tail call elimination. This basically allows infinite calling of functions at the end of another function. For example, here is a tail-call recursive fibonnaci sequence in bliss:

fib = fn (n, a, b) -> {
  if n == 0 {
    return a
  } else {
    if n == 1 {
      return b
    }
    else {
      return fib(n - 1, b, a + b)
    }
  }
}

This isn't the most important thing in the world, and will probably be implemented after #1

Why?

Since the language doesn't support traditional loop constructs, tail calls basically need to be optimized. There are couple of methods to doing this, and they're outline here (excellent article)

How?

Out of the methods outlined in that article above, stack replacement seems to be the simplest. This is partially why I'm putting this off until after #1, because we need more control over the stack than a tree walking interpreter gives us.

For a summary, instead of creating a new stack frame for the call, we just reuse the current one. This gives us the same efficiency as a loop (probably), and is arguably the cleanest way to do this