checkpoint (refactoring, WIP)
This commit is contained in:
36
lib/ast.js
36
lib/ast.js
@@ -709,5 +709,39 @@ TreeWalker.prototype = {
|
|||||||
},
|
},
|
||||||
parent: function(n) {
|
parent: function(n) {
|
||||||
return this.stack[this.stack.length - 2 - (n || 0)];
|
return this.stack[this.stack.length - 2 - (n || 0)];
|
||||||
}
|
},
|
||||||
|
push: function (node) {
|
||||||
|
this.stack.push(node);
|
||||||
|
},
|
||||||
|
pop: function() {
|
||||||
|
return this.stack.pop();
|
||||||
|
},
|
||||||
|
self: function() {
|
||||||
|
return this.stack[this.stack.length - 1];
|
||||||
|
},
|
||||||
|
find_parent: function(type) {
|
||||||
|
var stack = this.stack;
|
||||||
|
for (var i = stack.length; --i >= 0;) {
|
||||||
|
var x = stack[i];
|
||||||
|
if (x instanceof type) return x;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
in_boolean_context: function() {
|
||||||
|
var stack = this.stack;
|
||||||
|
var i = stack.length, self = stack[--i];
|
||||||
|
while (i > 0) {
|
||||||
|
var p = stack[--i];
|
||||||
|
if ((p instanceof AST_If && p.condition === self) ||
|
||||||
|
(p instanceof AST_Conditional && p.condition === self) ||
|
||||||
|
(p instanceof AST_DWLoop && p.condition === self) ||
|
||||||
|
(p instanceof AST_For && p.condition === self) ||
|
||||||
|
(p instanceof AST_UnaryPrefix && p.operator == "!" && p.expression === self))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!(p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||")))
|
||||||
|
return false;
|
||||||
|
self = p;
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -53,7 +53,10 @@
|
|||||||
// squeezing nodes.
|
// squeezing nodes.
|
||||||
|
|
||||||
function Compressor(options, false_by_default) {
|
function Compressor(options, false_by_default) {
|
||||||
options = defaults(options, {
|
if (!(this instanceof Compressor))
|
||||||
|
return new Compressor(options, false_by_default);
|
||||||
|
TreeTransformer.call(this, this.before, this.after);
|
||||||
|
this.options = defaults(options, {
|
||||||
sequences : !false_by_default,
|
sequences : !false_by_default,
|
||||||
properties : !false_by_default,
|
properties : !false_by_default,
|
||||||
dead_code : !false_by_default,
|
dead_code : !false_by_default,
|
||||||
@@ -73,48 +76,17 @@ function Compressor(options, false_by_default) {
|
|||||||
|
|
||||||
warnings : true
|
warnings : true
|
||||||
});
|
});
|
||||||
var stack = [];
|
|
||||||
function in_boolean_context() {
|
|
||||||
var i = stack.length, self = stack[--i];
|
|
||||||
while (i > 0) {
|
|
||||||
var p = stack[--i];
|
|
||||||
if ((p instanceof AST_If && p.condition === self) ||
|
|
||||||
(p instanceof AST_Conditional && p.condition === self) ||
|
|
||||||
(p instanceof AST_DWLoop && p.condition === self) ||
|
|
||||||
(p instanceof AST_For && p.condition === self) ||
|
|
||||||
(p instanceof AST_UnaryPrefix && p.operator == "!" && p.expression === self))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (!(p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||")))
|
|
||||||
return false;
|
|
||||||
self = p;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
function find_parent(type) {
|
|
||||||
for (var i = stack.length; --i >= 0;) {
|
|
||||||
var x = stack[i];
|
|
||||||
if (x instanceof type) return x;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
option : function(key) { return options[key] },
|
|
||||||
push_node : function(node) { stack.push(node) },
|
|
||||||
pop_node : function() { return stack.pop() },
|
|
||||||
stack : function() { return stack },
|
|
||||||
self : function() { return stack[stack.length - 1] },
|
|
||||||
parent : function(n) {
|
|
||||||
return stack[stack.length - 2 - (n || 0)];
|
|
||||||
},
|
|
||||||
warn : function() {
|
|
||||||
if (options.warnings)
|
|
||||||
AST_Node.warn.apply(AST_Node, arguments);
|
|
||||||
},
|
|
||||||
in_boolean_context: in_boolean_context,
|
|
||||||
find_parent: find_parent,
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Compressor.prototype = new TreeTransformer;
|
||||||
|
defaults(Compressor.prototype, {
|
||||||
|
option: function(key) { return this.options[key] },
|
||||||
|
warn: function() {
|
||||||
|
if (this.options.warnings)
|
||||||
|
AST_Node.warn.apply(AST_Node, arguments);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
(function(undefined){
|
(function(undefined){
|
||||||
|
|
||||||
AST_Node.DEFMETHOD("optimize", function(){
|
AST_Node.DEFMETHOD("optimize", function(){
|
||||||
@@ -1551,13 +1523,13 @@ function Compressor(options, false_by_default) {
|
|||||||
|
|
||||||
function SQUEEZE(nodetype, squeeze) {
|
function SQUEEZE(nodetype, squeeze) {
|
||||||
nodetype.DEFMETHOD("squeeze", function(compressor){
|
nodetype.DEFMETHOD("squeeze", function(compressor){
|
||||||
compressor.push_node(this);
|
compressor.push(this);
|
||||||
var self = this.clone(), opt;
|
var self = this.clone(), opt;
|
||||||
opt = squeeze(self, compressor);
|
opt = squeeze(self, compressor);
|
||||||
if (opt !== undefined) self = opt;
|
if (opt !== undefined) self = opt;
|
||||||
opt = self.optimize(compressor);
|
opt = self.optimize(compressor);
|
||||||
if (opt !== undefined) self = opt;
|
if (opt !== undefined) self = opt;
|
||||||
compressor.pop_node();
|
compressor.pop();
|
||||||
return self;
|
return self;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -1689,10 +1661,6 @@ function Compressor(options, false_by_default) {
|
|||||||
self.expression = self.expression.squeeze(compressor);
|
self.expression = self.expression.squeeze(compressor);
|
||||||
});
|
});
|
||||||
|
|
||||||
SQUEEZE(AST_UnaryPrefix, function(self, compressor){
|
|
||||||
self.expression = self.expression.squeeze(compressor);
|
|
||||||
});
|
|
||||||
|
|
||||||
SQUEEZE(AST_Binary, function(self, compressor){
|
SQUEEZE(AST_Binary, function(self, compressor){
|
||||||
self.left = self.left.squeeze(compressor);
|
self.left = self.left.squeeze(compressor);
|
||||||
self.right = self.right.squeeze(compressor);
|
self.right = self.right.squeeze(compressor);
|
||||||
|
|||||||
@@ -45,22 +45,11 @@
|
|||||||
// XXX: eventually I should refactor the compressor to use this infrastructure.
|
// XXX: eventually I should refactor the compressor to use this infrastructure.
|
||||||
|
|
||||||
function TreeTransformer(before, after) {
|
function TreeTransformer(before, after) {
|
||||||
|
TreeWalker.call(this);
|
||||||
this.before = before;
|
this.before = before;
|
||||||
this.after = after;
|
this.after = after;
|
||||||
this.stack = [];
|
|
||||||
}
|
}
|
||||||
|
TreeTransformer.prototype = new TreeWalker;
|
||||||
TreeTransformer.prototype = {
|
|
||||||
push: function (node) {
|
|
||||||
this.stack.push(node);
|
|
||||||
},
|
|
||||||
pop: function() {
|
|
||||||
return this.stack.pop();
|
|
||||||
},
|
|
||||||
parent: function (n) {
|
|
||||||
return this.stack[this.stack.length - 2 - (n || 0)];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
(function(undefined){
|
(function(undefined){
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user