From 527af3b69f3809b054f72f1f34d2aa9516efdfb6 Mon Sep 17 00:00:00 2001 From: Tom Date: Sun, 25 Jul 2010 00:49:15 +0200 Subject: [PATCH] simplfy generated code for static ranges --- lib/nodes.js | 19 ++++++++++++++++--- src/nodes.coffee | 8 ++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index 203f7793..1d5730f7 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -618,7 +618,7 @@ return from <= to ? ("" + idx + " = " + from + "; " + idx + " <" + this.equals + " " + to + "; " + (step || ("" + idx + "++"))) : ("" + idx + " = " + from + "; " + idx + " >" + this.equals + " " + to + "; " + (step || ("" + idx + "--"))); }; RangeNode.prototype.compileArray = function(o) { - var body, clause, i, idt, post, pre, result, vars; + var _b, _c, _d, _e, body, clause, i, idt, post, pre, range, result, vars; idt = this.idt(1); vars = this.compileVariables(merge(o, { indent: idt @@ -627,8 +627,21 @@ i = o.scope.freeVariable(); pre = ("\n" + (idt) + (result) + " = []; " + (vars)); if (this.fromNum && this.toNum) { - o.index = i; - body = this.compileSimple(o); + if (Math.abs(+this.fromNum - +this.toNum) >= 20) { + o.index = i; + body = this.compileSimple(o); + } else { + range = this.exclusive ? (function() { + _b = []; + for (var _c = +this.fromNum; +this.fromNum <= +this.toNum ? _c < +this.toNum : _c > +this.toNum; +this.fromNum <= +this.toNum ? _c += 1 : _c -= 1){ _b.push(_c) }; + return _b; + }).call(this) : (function() { + _d = []; + for (var _e = +this.fromNum; +this.fromNum <= +this.toNum ? _e <= +this.toNum : _e >= +this.toNum; +this.fromNum <= +this.toNum ? _e += 1 : _e -= 1){ _d.push(_e) }; + return _d; + }).call(this); + return ("[" + (range.join(', ')) + "]"); + } } else { clause = ("" + this.fromVar + " <= " + this.toVar + " ?"); body = ("var " + i + " = " + this.fromVar + "; " + clause + " " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + clause + " " + i + " += 1 : " + i + " -= 1"); diff --git a/src/nodes.coffee b/src/nodes.coffee index 1fbc808c..3d087ae5 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -578,8 +578,12 @@ exports.RangeNode: class RangeNode extends BaseNode i: o.scope.freeVariable() pre: "\n${idt}${result} = []; ${vars}" if @fromNum and @toNum - o.index: i - body: @compileSimple o + if Math.abs(+@fromNum - +@toNum) >= 20 + o.index: i + body: @compileSimple o + else + range: if @exclusive then [+@fromNum...+@toNum] else [+@fromNum..+@toNum] + return "[${range.join(', ')}]" else clause: "$@fromVar <= $@toVar ?" body: "var $i = $@fromVar; $clause $i <$@equals $@toVar : $i >$@equals $@toVar; $clause $i += 1 : $i -= 1"