checkpoint (refactoring, WIP)

This commit is contained in:
Mihai Bazon
2012-09-26 12:16:16 +03:00
parent e979d01f04
commit a24e7ee976
3 changed files with 52 additions and 61 deletions

View File

@@ -709,5 +709,39 @@ TreeWalker.prototype = {
},
parent: function(n) {
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;
}
},
};

View File

@@ -53,7 +53,10 @@
// squeezing nodes.
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,
properties : !false_by_default,
dead_code : !false_by_default,
@@ -73,47 +76,16 @@ function Compressor(options, false_by_default) {
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)];
},
Compressor.prototype = new TreeTransformer;
defaults(Compressor.prototype, {
option: function(key) { return this.options[key] },
warn: function() {
if (options.warnings)
if (this.options.warnings)
AST_Node.warn.apply(AST_Node, arguments);
},
in_boolean_context: in_boolean_context,
find_parent: find_parent,
};
};
}
});
(function(undefined){
@@ -1551,13 +1523,13 @@ function Compressor(options, false_by_default) {
function SQUEEZE(nodetype, squeeze) {
nodetype.DEFMETHOD("squeeze", function(compressor){
compressor.push_node(this);
compressor.push(this);
var self = this.clone(), opt;
opt = squeeze(self, compressor);
if (opt !== undefined) self = opt;
opt = self.optimize(compressor);
if (opt !== undefined) self = opt;
compressor.pop_node();
compressor.pop();
return self;
});
};
@@ -1689,10 +1661,6 @@ function Compressor(options, false_by_default) {
self.expression = self.expression.squeeze(compressor);
});
SQUEEZE(AST_UnaryPrefix, function(self, compressor){
self.expression = self.expression.squeeze(compressor);
});
SQUEEZE(AST_Binary, function(self, compressor){
self.left = self.left.squeeze(compressor);
self.right = self.right.squeeze(compressor);

View File

@@ -45,22 +45,11 @@
// XXX: eventually I should refactor the compressor to use this infrastructure.
function TreeTransformer(before, after) {
TreeWalker.call(this);
this.before = before;
this.after = after;
this.stack = [];
}
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)];
}
};
TreeTransformer.prototype = new TreeWalker;
(function(undefined){