Skip to content

Commit

Permalink
convert AST_Seq from binary tree to array
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Feb 2, 2017
1 parent 7f8d72d commit a5416f9
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 174 deletions.
2 changes: 1 addition & 1 deletion bin/extract-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function getProps(filename) {
(function walk(node){
node.walk(new U2.TreeWalker(function(node){
if (node instanceof U2.AST_Seq) {
walk(node.cdr);
walk(node.expressions[node.expressions.length - 1]);
return true;
}
if (node instanceof U2.AST_String) {
Expand Down
2 changes: 1 addition & 1 deletion bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ function getOptions(flag, constants) {
ret[name] = true;
return true; // no descend
}
print_error(node.TYPE)
print_error(node.TYPE);
print_error("Error parsing arguments for flag `" + flag + "': " + x);
process.exit(1);
}));
Expand Down
65 changes: 10 additions & 55 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,68 +582,23 @@ 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", "car cdr", {
$documentation: "A sequence expression (two comma-separated expressions)",
$propdoc: {
car: "[AST_Node] first element in sequence",
cdr: "[AST_Node] second element in sequence"
},
$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;
},
to_array: function() {
var p = this, a = [];
while (p) {
a.push(p.car);
if (p.cdr && !(p.cdr instanceof AST_Seq)) {
a.push(p.cdr);
break;
}
p = p.cdr;
}
return a;
var AST_Seq = DEFNODE("Seq", "expressions", {
$documentation: "A sequence expression (comma-separated expressions)",
$propdoc: {
expressions: "[AST_Node*] array of expressions (at least two)"
},
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;
}
},
len: function() {
if (this.cdr instanceof AST_Seq) {
return this.cdr.len() + 1;
if (node instanceof AST_Seq) {
[].push.apply(this.expressions, node.expressions);
} else {
return 2;
this.expressions.push(node);
}
},
_walk: function(visitor) {
return visitor._visit(this, function(){
this.car._walk(visitor);
if (this.cdr) this.cdr._walk(visitor);
this.expressions.forEach(function(expression) {
expression._walk(visitor);
});
});
}
});
Expand Down
Loading

0 comments on commit a5416f9

Please sign in to comment.