checkpoint

This commit is contained in:
Mihai Bazon
2012-09-04 15:36:14 +03:00
parent 376667a818
commit 1b5183dd5e
4 changed files with 45 additions and 10 deletions

View File

@@ -64,6 +64,7 @@ function Compressor(options, false_by_default) {
evaluate : !false_by_default,
booleans : !false_by_default,
dwloops : !false_by_default,
hoist : !false_by_default,
warnings : true
});
@@ -140,7 +141,7 @@ function Compressor(options, false_by_default) {
statements = eliminate_dead_code(statements, compressor);
}
if (compressor.option("sequences")) {
statements = sequencesize(statements);
statements = sequencesize(statements, compressor);
}
return statements;
};
@@ -157,7 +158,7 @@ function Compressor(options, false_by_default) {
}
return a;
}, []);
}
};
function eliminate_dead_code(statements, compressor) {
var has_quit = false;
@@ -194,7 +195,7 @@ function Compressor(options, false_by_default) {
}
return a;
}, []);
}
};
// XXX: this is destructive -- it modifies tree nodes.
function sequencesize(statements) {
@@ -227,7 +228,7 @@ function Compressor(options, false_by_default) {
return a;
}, []);
return statements;
}
};
/* -----[ boolean/negation helpers ]----- */
@@ -479,6 +480,18 @@ function Compressor(options, false_by_default) {
return self;
});
SQUEEZE(AST_Scope, function(self, compressor){
self = self.clone();
self.hoist_declarations(compressor);
self.body = tighten_body(self.body, compressor);
return self;
});
AST_Scope.DEFMETHOD("hoist_declarations", function(compressor){
if (compressor.option("hoist")) {
}
});
SQUEEZE(AST_EmptyStatement, function(self, compressor){
return self;
});
@@ -559,6 +572,7 @@ function Compressor(options, false_by_default) {
// “has no side effects”; also it doesn't work for cases like
// `x && true`, though it probably should.
var cond = self.condition.evaluate(compressor);
self.condition = cond[0];
if (cond.length == 2) {
if (cond[1]) {
AST_Node.warn("Condition always true [{line},{col}]", self.condition.start);
@@ -568,6 +582,13 @@ function Compressor(options, false_by_default) {
return self.alternative || make_node(AST_EmptyStatement, self);
}
}
if (self.condition instanceof AST_UnaryPrefix
&& self.condition.operator == "!") {
self.condition = self.condition.expression;
var tmp = self.body;
self.body = self.alternative || make_node(AST_EmptyStatement, self);
self.alternative = tmp;
}
if (self.body instanceof AST_SimpleStatement
&& self.alternative instanceof AST_SimpleStatement) {
return make_node(AST_SimpleStatement, self, {
@@ -578,7 +599,9 @@ function Compressor(options, false_by_default) {
}).optimize(compressor)
});
}
if ((!self.alternative || self.alternative instanceof AST_EmptyStatement) && self.body instanceof AST_SimpleStatement) {
if ((!self.alternative
|| self.alternative instanceof AST_EmptyStatement)
&& self.body instanceof AST_SimpleStatement) {
return make_node(AST_SimpleStatement, self, {
body: make_node(AST_Binary, self, {
operator : "&&",
@@ -589,7 +612,7 @@ function Compressor(options, false_by_default) {
}
if (self.body instanceof AST_EmptyStatement
&& self.alternative
&& !(self.alternative instanceof AST_EmptyStatement)) {
&& self.alternative instanceof AST_SimpleStatement) {
return make_node(AST_SimpleStatement, self, {
body: make_node(AST_Binary, self, {
operator : "||",
@@ -656,6 +679,7 @@ function Compressor(options, false_by_default) {
SQUEEZE(AST_Lambda, function(self, compressor){
self = self.clone();
self.hoist_declarations(compressor);
if (self.name) self.name = self.name.squeeze(compressor);
self.argnames = do_list(self.argnames, compressor);
self.body = self.body.squeeze(compressor);

View File

@@ -165,6 +165,7 @@ AST_Toplevel.DEFMETHOD("scope_warnings", function(options){
unreferenced : true,
assign_to_global : true,
func_arguments : true,
nested_defuns : true,
eval : true
});
var tw = new TreeWalker(function(node){
@@ -225,6 +226,17 @@ AST_Toplevel.DEFMETHOD("scope_warnings", function(options){
col: node.start.col
});
}
if (options.nested_defuns
&& node instanceof AST_Defun
&& !(tw.parent() instanceof AST_Scope
|| (tw.parent() instanceof AST_BlockStatement
&& tw.parent(1) instanceof AST_Scope))) {
AST_Node.warn("Function {name} declared in nested statement [{line},{col}]", {
name: node.name.name,
line: node.start.line,
col: node.start.col
});
}
});
this.walk(tw);
});

View File

@@ -145,9 +145,8 @@ var MAP = (function(){
// feeling it works well in general for many scripts (well, better
// than alphabetical order). It would be nice if we could adapt it to
// the currently running script.
// var BASE54_DIGITS = "etnrisouaflchpdvmgybwESxTNCkLAOM_DPHBjFIqRUzWXV$JKQGYZ0516372984";
var BASE54_DIGITS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789";
var BASE54_DIGITS = "etnrisouaflchpdvmgybwESxTNCkLAOM_DPHBjFIqRUzWXV$JKQGYZ0516372984";
//var BASE54_DIGITS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789";
function base54(num) {
var ret = "", base = 54;

View File

@@ -22,7 +22,7 @@ time_it("scope", function(){
ast.figure_out_scope();
});
// ast.scope_warnings();
ast.scope_warnings();
time_it("mangle", function(){
ast.mangle_names();