aboutsummaryrefslogtreecommitdiff
path: root/lib/compress.js
diff options
context:
space:
mode:
authorMihai Bazon <mihai@bazon.net>2012-09-16 18:05:15 +0300
committerMihai Bazon <mihai@bazon.net>2012-09-16 18:10:54 +0300
commit5d781ec6f8689f102db1c0666c725bd64b463df2 (patch)
treec078a5bab59645a823fffb821e355db54e047191 /lib/compress.js
parent0f418d654e035b8547340c4d4c03802a01a532b8 (diff)
downloadtracifyjs-5d781ec6f8689f102db1c0666c725bd64b463df2.tar.gz
tracifyjs-5d781ec6f8689f102db1c0666c725bd64b463df2.zip
some cleanup
Diffstat (limited to 'lib/compress.js')
-rw-r--r--lib/compress.js69
1 files changed, 23 insertions, 46 deletions
diff --git a/lib/compress.js b/lib/compress.js
index bd14529b..67f37269 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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;
};