From 0f418d654e035b8547340c4d4c03802a01a532b8 Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Sun, 16 Sep 2012 16:29:17 +0300 Subject: more sequencesizing (WIP) --- lib/compress.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'lib/compress.js') diff --git a/lib/compress.js b/lib/compress.js index 7440a5bd..bd14529b 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -189,6 +189,7 @@ 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); @@ -371,6 +372,52 @@ function Compressor(options, false_by_default) { return ret; }; + function sequencesize_2(statements, 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(); + } + 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(); + } + 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(); + } + } + ret.push(stat); + prev = stat instanceof AST_SimpleStatement ? stat : null; + }); + CHANGED = ret.length != statements.length; + return ret; + }; + function join_consecutive_vars(statements, compressor) { var prev = null; return statements.reduce(function(a, stat){ -- cgit v1.2.3