puffnfresh / brushtail Goto Github PK
View Code? Open in Web Editor NEWJS AST rewriter for tail call elimination
Home Page: http://brushtail.brianmckenna.org/
License: MIT License
JS AST rewriter for tail call elimination
Home Page: http://brushtail.brianmckenna.org/
License: MIT License
This will make contribution a whole lot easier.
For some reason, all function expressions are ignored, even though NFEs have letrec semantics.
Put it on your website, in your blog, or even on github pages if you must. Just create an example people can play with, without having to pull.
Would be great to be able to just add it as a dependency.
If the function to transform has a label tco
it gets shadowed:
function a(n) {
tco: {
return a(n);
}
}
function a(n) {
var __tcor;
tco:
while (true) {
tco: {
{
var __n = n;
n = __n;
continue tco;
}
}
}
return __tcor;
}
One of the key advantages for tail recursion is often that the compiler can find ways to optimize the loop to increase performance. But unfortunately this performance boost is destroyed by the closure in the mutated version. By removing the closure there is a massive performance increase on my machine around 800%.
http://jsperf.com/tail-recursion-test
Question is, are there efforts to remove the closure?
The following code breaks.
function factorial_cps(n, k) {
if (n === 0) return k(1);
return factorial_cps(
n - 1,
function(result) {
return k(n * result);
});
}
function identity(x) { return x; }
var f = factorial_cps(5, identity); // 120
console.log(f);
The problem is the
__k = function (result) {
{
__tcor = k(n * result);
break tco;
}}
Which simply doesn't work. I guess there is no trivial solution for that but it would be very cool to be able to transform CPS code.
We can no longer be sure these are tail calls; they likely aren't. We can use escope again here. Either way, it should take into account variable declarations, function declarations, and parameter names that may shadow the function name.
Icedcoffeescript would go great with brushtail.
maxtaco/coffee-script#81
Shouldn't be too tricky. Traverse the AST looking for identifiers, store them in a set and try generating a fresh identifier.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.