mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
fixing range literals (which had gone untested) oops.
This commit is contained in:
parent
744638ed08
commit
7c426db36a
3 changed files with 23 additions and 10 deletions
12
lib/nodes.js
12
lib/nodes.js
|
@ -597,7 +597,7 @@
|
|||
return "" + vars + "; " + (idx) + " " + op + " " + (this.toVar.compile(o)) + "; " + idx + " += " + step;
|
||||
};
|
||||
RangeNode.prototype.compileArray = function(o) {
|
||||
var body, clause, equals, from, idt, post, pre, to, vars;
|
||||
var body, clause, equals, from, i, idt, post, pre, result, to, vars;
|
||||
idt = this.idt(1);
|
||||
vars = this.compileVariables(merge(o, {
|
||||
indent: idt
|
||||
|
@ -605,11 +605,13 @@
|
|||
equals = this.exclusive ? '' : '=';
|
||||
from = this.fromVar.compile(o);
|
||||
to = this.toVar.compile(o);
|
||||
result = o.scope.freeVariable();
|
||||
i = o.scope.freeVariable();
|
||||
clause = ("" + from + " <= " + to + " ?");
|
||||
pre = ("\n" + (idt) + "a = [];" + (vars));
|
||||
body = ("var i = " + from + "; (" + clause + " i <" + equals + " " + to + " : i >" + equals + " " + to + "); (" + clause + " i += 1 : i -= 1)");
|
||||
post = ("a.push(i);\n" + (idt) + "return a;\n" + o.indent);
|
||||
return "(function(){" + (pre) + "for (" + body + ") " + post + "}).call(this)";
|
||||
pre = ("\n" + (idt) + (result) + " = [];" + (vars));
|
||||
body = ("var " + i + " = " + from + "; " + clause + " " + i + " <" + equals + " " + to + " : " + i + " >" + equals + " " + to + "; " + clause + " " + i + " += 1 : " + i + " -= 1");
|
||||
post = ("{ " + (result) + ".push(" + i + ") };\n" + (idt) + "return " + result + ";\n" + o.indent);
|
||||
return "(function(){" + (pre) + ";\n" + (idt) + "for (" + body + ")" + post + "}).call(this)";
|
||||
};
|
||||
return RangeNode;
|
||||
})();
|
||||
|
|
|
@ -525,11 +525,13 @@ exports.RangeNode: class RangeNode extends BaseNode
|
|||
equals: if @exclusive then '' else '='
|
||||
from: @fromVar.compile o
|
||||
to: @toVar.compile o
|
||||
result: o.scope.freeVariable()
|
||||
i: o.scope.freeVariable()
|
||||
clause: "$from <= $to ?"
|
||||
pre: "\n${idt}a = [];${vars}"
|
||||
body: "var i = $from; ($clause i <$equals $to : i >$equals $to); ($clause i += 1 : i -= 1)"
|
||||
post: "a.push(i);\n${idt}return a;\n$o.indent"
|
||||
"(function(){${pre}for ($body) $post}).call(this)"
|
||||
pre: "\n${idt}${result} = [];${vars}"
|
||||
body: "var $i = $from; $clause $i <$equals $to : $i >$equals $to; $clause $i += 1 : $i -= 1"
|
||||
post: "{ ${result}.push($i) };\n${idt}return $result;\n$o.indent"
|
||||
"(function(){${pre};\n${idt}for ($body)$post}).call(this)"
|
||||
|
||||
#### SliceNode
|
||||
|
||||
|
|
|
@ -12,10 +12,19 @@ a: [0, 1, 2, 3, 4, 5, 6, 7]
|
|||
deepEqual a[2...6], [2, 3, 4, 5]
|
||||
|
||||
|
||||
# Range.
|
||||
# Ranges.
|
||||
countdown: [10..1].join(' ')
|
||||
ok countdown is "10 9 8 7 6 5 4 3 2 1"
|
||||
|
||||
a: 1
|
||||
b: 5
|
||||
nums: [a...b]
|
||||
ok nums.join(' ') is '1 2 3 4'
|
||||
|
||||
b: -5
|
||||
nums: [a..b]
|
||||
ok nums.join(' ') is '1 0 -1 -2 -3 -4 -5'
|
||||
|
||||
|
||||
# Expression-based range.
|
||||
array: [(1+5)..1+9]
|
||||
|
|
Loading…
Add table
Reference in a new issue