puffnfresh/roy — pull request #187 by rtfeldman (2013-07-24 06:34:25+00:00)
Title: Tail Call Optimization with Brushtail

This is not ready to be merged yet; I hit a snare and would like to discuss.

Consider the following code, generated from [fixtures/good/tco.roy](https://github.com/puffnfresh/roy/blob/tco/test/fixtures/good/tco.roy)

```javascript
var gcd = function (a, b) {
return function () {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}();
};
```

Brushtail won't optimize this because the anonymous function wrapper means the recursive call to `gcd` is not actually in the tail position.

Naturally, without that wrapper...

```javascript
var gcd = function (a, b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
};
```

...it gets optimized as expected.

I haven't taken a look at where this anonymous function wrapper is coming from, but I assume it is being added for scoping purposes. Obviously there are plenty of cases where wrapping for scoping makes sense, but I can't think of any where wrapping the entirety of a function body like this could impact scoping.

Am I missing something, or is this wrapper just something superfluous we should do away with in order to enable TCO?