diff options
author | Mihai Bazon <mihai@bazon.net> | 2012-09-14 15:36:38 +0300 |
---|---|---|
committer | Mihai Bazon <mihai@bazon.net> | 2012-09-14 15:36:38 +0300 |
commit | 924aa580602a4ad94f1079dcd157286314066553 (patch) | |
tree | acd65dd576ded856e5e7cc3b15ee473539b645ac /lib/ast.js | |
parent | 93b973c99dd25aeff66ff31f2881e9ce252eaac6 (diff) | |
download | tracifyjs-924aa580602a4ad94f1079dcd157286314066553.tar.gz tracifyjs-924aa580602a4ad94f1079dcd157286314066553.zip |
more optimizations that v1 does and some cleanups
- a = a + x ==> a+=x
- joining consecutive var statements (hoisting is not always desirable)
- x == false ==> x == 0, x != true ==> x != 1
- x, x ==> x; x = exp(), x ==> x = exp()
- discarding useless break-s
Diffstat (limited to 'lib/ast.js')
-rw-r--r-- | lib/ast.js | 53 |
1 files changed, 47 insertions, 6 deletions
@@ -70,7 +70,11 @@ function DEFNODE(type, props, methods, base) { ctor.prototype.TYPE = ctor.TYPE = type; } if (methods) for (i in methods) if (HOP(methods, i)) { - ctor.prototype[i] = methods[i]; + if (/^\$/.test(i)) { + ctor[i.substr(1)] = methods[i]; + } else { + ctor.prototype[i] = methods[i]; + } } ctor.DEFMETHOD = function(name, method) { this.prototype[name] = method; @@ -429,12 +433,45 @@ var AST_New = DEFNODE("New", null, { $documentation: "An object instantiation. Derives from a function call since it has exactly the same properties." }, AST_Call); -var AST_Seq = DEFNODE("Seq", "first second", { +var AST_Seq = DEFNODE("Seq", "car cdr", { $documentation: "A sequence expression (two comma-separated expressions)", + $cons: function(x, y) { + var seq = new AST_Seq(x); + seq.car = x; + seq.cdr = y; + return seq; + }, + $from_array: function(array) { + if (array.length == 0) return null; + if (array.length == 1) return array[0].clone(); + var list = null; + for (var i = array.length; --i >= 0;) { + list = AST_Seq.cons(array[i], list); + } + var p = list; + while (p) { + if (p.cdr && !p.cdr.cdr) { + p.cdr = p.cdr.car; + break; + } + p = p.cdr; + } + return list; + }, + add: function(node) { + var p = this; + while (p) { + if (!(p.cdr instanceof AST_Seq)) { + var cell = AST_Seq.cons(p.cdr, node); + return p.cdr = cell; + } + p = p.cdr; + } + }, _walk: function(visitor) { return visitor._visit(this, function(){ - this.first._walk(visitor); - this.second._walk(visitor); + this.car._walk(visitor); + if (this.cdr) this.cdr._walk(visitor); }); } }); @@ -629,15 +666,19 @@ var AST_Undefined = DEFNODE("Undefined", null, { value: (function(){}()) }, AST_Atom); +var AST_Boolean = DEFNODE("Boolean", null, { + $documentation: "Base class for booleans", +}, AST_Atom); + var AST_False = DEFNODE("False", null, { $documentation: "The `false` atom", value: false -}, AST_Atom); +}, AST_Boolean); var AST_True = DEFNODE("True", null, { $documentation: "The `true` atom", value: true -}, AST_Atom); +}, AST_Boolean); /* -----[ TreeWalker ]----- */ |