some cleanup

This commit is contained in:
Mihai Bazon
2012-09-16 18:05:15 +03:00
parent 0f418d654e
commit 5d781ec6f8

View File

@@ -189,7 +189,6 @@ function Compressor(options, false_by_default) {
}
if (compressor.option("sequences")) {
statements = sequencesize(statements, compressor);
statements = sequencesize_2(statements, compressor);
}
if (compressor.option("if_return")) {
statements = handle_if_return(statements, compressor);
@@ -348,73 +347,51 @@ function Compressor(options, false_by_default) {
else push_seq(), ret.push(stat);
});
push_seq();
// if the last node is return or throw, we can mix in the
// previous sequence which might help reducing the list to
// a single statement.
var exit = ret[ret.length - 1], prev = ret[ret.length - 2];
if (prev instanceof AST_SimpleStatement
&& exit instanceof AST_Exit && exit.value) {
ret.pop();
ret.pop();
if (prev.body instanceof AST_Seq) {
prev.body.add(exit.value);
prev.body = prev.body.optimize(compressor);
exit.value = prev.body;
}
else {
exit.value = AST_Seq.cons(prev.body, exit.value).optimize(compressor);
}
ret.push(exit);
}
ret = sequencesize_2(ret, compressor);
CHANGED = ret.length != statements.length;
return ret;
};
function sequencesize_2(statements, compressor) {
function cons_seq(right) {
ret.pop();
var left = prev.body;
if (left instanceof AST_Seq) {
left.add(right);
} else {
left = AST_Seq.cons(left, right);
}
return left.optimize(compressor);
};
var ret = [], prev = null;
statements.forEach(function(stat){
if (prev) {
if (stat instanceof AST_For && stat.init && !(stat.init instanceof AST_Definitions)) {
if (prev.body instanceof AST_Seq) {
prev.body.add(stat.init);
prev.body = prev.body.optimize(compressor);
stat.init = prev.body;
} else {
stat.init = AST_Seq.cons(prev.body, stat.init).optimize(compressor);
}
ret.pop();
stat.init = cons_seq(stat.init);
}
else if (stat instanceof AST_For && !stat.init) {
stat.init = prev.body;
ret.pop();
}
else if (stat instanceof AST_If) {
if (prev.body instanceof AST_Seq) {
prev.body.add(stat.condition);
prev.body = prev.body.optimize(compressor);
stat.condition = prev.body;
} else {
stat.condition = AST_Seq.cons(prev.body, stat.condition).optimize(compressor);
}
ret.pop();
stat.condition = cons_seq(stat.condition);
}
else if (stat instanceof AST_With) {
if (prev.body instanceof AST_Seq) {
prev.body.add(stat.expression);
prev.body = prev.body.optimize(compressor);
stat.expression = prev.body;
} else {
stat.expression = AST_Seq.cons(prev.body, stat.expression).optimize(compressor);
}
ret.pop();
stat.expression = cons_seq(stat.expression);
}
else if (stat instanceof AST_Exit && stat.value) {
stat.value = cons_seq(stat.value);
}
else if (stat instanceof AST_Exit) {
stat.value = cons_seq(make_node(AST_Undefined, stat));
}
else if (stat instanceof AST_Switch) {
stat.expression = cons_seq(stat.expression);
}
}
ret.push(stat);
prev = stat instanceof AST_SimpleStatement ? stat : null;
});
CHANGED = ret.length != statements.length;
return ret;
};