mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
simplfy generated code for static ranges
This commit is contained in:
19
lib/nodes.js
19
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 + "--")));
|
return from <= to ? ("" + idx + " = " + from + "; " + idx + " <" + this.equals + " " + to + "; " + (step || ("" + idx + "++"))) : ("" + idx + " = " + from + "; " + idx + " >" + this.equals + " " + to + "; " + (step || ("" + idx + "--")));
|
||||||
};
|
};
|
||||||
RangeNode.prototype.compileArray = function(o) {
|
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);
|
idt = this.idt(1);
|
||||||
vars = this.compileVariables(merge(o, {
|
vars = this.compileVariables(merge(o, {
|
||||||
indent: idt
|
indent: idt
|
||||||
@@ -627,8 +627,21 @@
|
|||||||
i = o.scope.freeVariable();
|
i = o.scope.freeVariable();
|
||||||
pre = ("\n" + (idt) + (result) + " = []; " + (vars));
|
pre = ("\n" + (idt) + (result) + " = []; " + (vars));
|
||||||
if (this.fromNum && this.toNum) {
|
if (this.fromNum && this.toNum) {
|
||||||
o.index = i;
|
if (Math.abs(+this.fromNum - +this.toNum) >= 20) {
|
||||||
body = this.compileSimple(o);
|
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 {
|
} else {
|
||||||
clause = ("" + this.fromVar + " <= " + this.toVar + " ?");
|
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");
|
body = ("var " + i + " = " + this.fromVar + "; " + clause + " " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + clause + " " + i + " += 1 : " + i + " -= 1");
|
||||||
|
|||||||
@@ -578,8 +578,12 @@ exports.RangeNode: class RangeNode extends BaseNode
|
|||||||
i: o.scope.freeVariable()
|
i: o.scope.freeVariable()
|
||||||
pre: "\n${idt}${result} = []; ${vars}"
|
pre: "\n${idt}${result} = []; ${vars}"
|
||||||
if @fromNum and @toNum
|
if @fromNum and @toNum
|
||||||
o.index: i
|
if Math.abs(+@fromNum - +@toNum) >= 20
|
||||||
body: @compileSimple o
|
o.index: i
|
||||||
|
body: @compileSimple o
|
||||||
|
else
|
||||||
|
range: if @exclusive then [+@fromNum...+@toNum] else [+@fromNum..+@toNum]
|
||||||
|
return "[${range.join(', ')}]"
|
||||||
else
|
else
|
||||||
clause: "$@fromVar <= $@toVar ?"
|
clause: "$@fromVar <= $@toVar ?"
|
||||||
body: "var $i = $@fromVar; $clause $i <$@equals $@toVar : $i >$@equals $@toVar; $clause $i += 1 : $i -= 1"
|
body: "var $i = $@fromVar; $clause $i <$@equals $@toVar : $i >$@equals $@toVar; $clause $i += 1 : $i -= 1"
|
||||||
|
|||||||
Reference in New Issue
Block a user