From c4b72fcc794e235da11248a8a0f39624df5cf13a Mon Sep 17 00:00:00 2001 From: Michael Ficarra Date: Wed, 23 Mar 2011 13:49:15 -0400 Subject: [PATCH] improved range compilation --- lib/lexer.js | 2 +- lib/nodes.js | 13 ++++++------- src/nodes.coffee | 15 +++++++-------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/lib/lexer.js b/lib/lexer.js index 945f11d7..8fdee451 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -459,7 +459,7 @@ Lexer.prototype.balancedString = function(str, end) { var i, letter, prev, stack, _ref; stack = [end]; - for (i = 1, _ref = str.length; (1 <= _ref ? i < _ref : i > _ref); (1 <= _ref ? i += 1 : i -= 1)) { + for (i = 1, _ref = str.length; 1 <= _ref ? i < _ref : i > _ref; 1 <= _ref ? i++ : i--) { switch (letter = str.charAt(i)) { case '\\': i++; diff --git a/lib/nodes.js b/lib/nodes.js index 7b2ccd5e..4a50faed 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -782,7 +782,7 @@ } }; Range.prototype.compileNode = function(o) { - var compare, idx, incr, intro, step, stepPart, vars; + var compare, cond, idx, incr, step, vars; this.compileVariables(o); if (!o.index) { return this.compileArray(o); @@ -793,10 +793,9 @@ idx = del(o, 'index'); step = del(o, 'step'); vars = ("" + idx + " = " + this.from) + (this.to !== this.toVar ? ", " + this.to : ''); - intro = "(" + this.fromVar + " <= " + this.toVar + " ? " + idx; - compare = "" + intro + " <" + this.equals + " " + this.toVar + " : " + idx + " >" + this.equals + " " + this.toVar + ")"; - stepPart = step ? step.compile(o) : '1'; - incr = step ? "" + idx + " += " + stepPart : "" + intro + " += " + stepPart + " : " + idx + " -= " + stepPart + ")"; + cond = "" + this.fromVar + " <= " + this.toVar; + compare = "" + cond + " ? " + idx + " <" + this.equals + " " + this.toVar + " : " + idx + " >" + this.equals + " " + this.toVar; + incr = step ? "" + idx + " += " + (step.compile(o)) : "" + cond + " ? " + idx + "++ : " + idx + "--"; return "" + vars + "; " + compare + "; " + incr; }; Range.prototype.compileSimple = function(o) { @@ -816,7 +815,7 @@ if (this.fromNum && this.toNum && Math.abs(this.fromNum - this.toNum) <= 20) { range = (function() { _results = []; - for (var _i = _ref = +this.fromNum, _ref2 = +this.toNum; _ref <= _ref2 ? _i <= _ref2 : _i >= _ref2; _ref <= _ref2 ? _i += 1 : _i -= 1){ _results.push(_i); } + for (var _i = _ref = +this.fromNum, _ref2 = +this.toNum; _ref <= _ref2 ? _i <= _ref2 : _i >= _ref2; _ref <= _ref2 ? _i++ : _i--){ _results.push(_i); } return _results; }).apply(this, arguments); if (this.exclusive) { @@ -834,7 +833,7 @@ } else { vars = ("" + i + " = " + this.from) + (this.to !== this.toVar ? ", " + this.to : ''); clause = "" + this.fromVar + " <= " + this.toVar + " ?"; - body = "var " + vars + "; " + clause + " " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + clause + " " + i + " += 1 : " + i + " -= 1"; + body = "var " + vars + "; " + clause + " " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + clause + " " + i + "++ : " + i + "--"; } post = "{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + o.indent; return "(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).apply(this, arguments)"; diff --git a/src/nodes.coffee b/src/nodes.coffee index 99370609..c139d23c 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -642,10 +642,9 @@ exports.Range = class Range extends Base idx = del o, 'index' step = del o, 'step' vars = "#{idx} = #{@from}" + if @to isnt @toVar then ", #{@to}" else '' - intro = "(#{@fromVar} <= #{@toVar} ? #{idx}" - compare = "#{intro} <#{@equals} #{@toVar} : #{idx} >#{@equals} #{@toVar})" - stepPart = if step then step.compile(o) else '1' - incr = if step then "#{idx} += #{stepPart}" else "#{intro} += #{stepPart} : #{idx} -= #{stepPart})" + cond = "#{@fromVar} <= #{@toVar}" + compare = "#{cond} ? #{idx} <#{@equals} #{@toVar} : #{idx} >#{@equals} #{@toVar}" + incr = if step then "#{idx} += #{step.compile(o)}" else "#{cond} ? #{idx}++ : #{idx}--" "#{vars}; #{compare}; #{incr}" # Compile a simple range comprehension, with integers. @@ -671,11 +670,11 @@ exports.Range = class Range extends Base pre = "\n#{idt}#{result} = [];" if @fromNum and @toNum o.index = i - body = @compileSimple o + body = @compileSimple o else - vars = "#{i} = #{@from}" + if @to isnt @toVar then ", #{@to}" else '' - clause = "#{@fromVar} <= #{@toVar} ?" - body = "var #{vars}; #{clause} #{i} <#{@equals} #{@toVar} : #{i} >#{@equals} #{@toVar}; #{clause} #{i} += 1 : #{i} -= 1" + vars = "#{i} = #{@from}" + if @to isnt @toVar then ", #{@to}" else '' + cond = "#{@fromVar} <= #{@toVar}" + body = "var #{vars}; #{cond} ? #{i} <#{@equals} #{@toVar} : #{i} >#{@equals} #{@toVar}; #{cond} ? #{i}++ : #{i}--" post = "{ #{result}.push(#{i}); }\n#{idt}return #{result};\n#{o.indent}" "(function() {#{pre}\n#{idt}for (#{body})#{post}}).apply(this, arguments)"