more sequencesizing (WIP)
authorMihai Bazon <mihai@bazon.net>
Sun, 16 Sep 2012 13:29:17 +0000 (16:29 +0300)
committerMihai Bazon <mihai@bazon.net>
Sun, 16 Sep 2012 13:29:17 +0000 (16:29 +0300)
lib/compress.js

index 7440a5b..bd14529 100644 (file)
@@ -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){