Skip to content

Commit

Permalink
Issue #621. Added the ability to leave the start and end index off of…
Browse files Browse the repository at this point in the history
… arrays. list[5..] is now valid CoffeeScript, slicing to the end of the array.
  • Loading branch information
jashkenas committed Aug 19, 2010
1 parent bf6bafa commit 4ddd65a
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 101 deletions.
8 changes: 8 additions & 0 deletions lib/grammar.js
Expand Up @@ -307,6 +307,14 @@
return new RangeNode($2, $5);
}), o("INDEX_START Expression . . . Expression INDEX_END", function() {
return new RangeNode($2, $6, true);
}), o("INDEX_START Expression . . INDEX_END", function() {
return new RangeNode($2, null);
}), o("INDEX_START Expression . . . INDEX_END", function() {
return new RangeNode($2, null, true);
}), o("INDEX_START . . Expression INDEX_END", function() {
return new RangeNode(null, $4);
}), o("INDEX_START . . . Expression INDEX_END", function() {
return new RangeNode(null, $5, true);
})
],
Array: [
Expand Down
17 changes: 10 additions & 7 deletions lib/nodes.js
Expand Up @@ -692,11 +692,14 @@
SliceNode.prototype["class"] = 'SliceNode';
SliceNode.prototype.children = ['range'];
SliceNode.prototype.compileNode = function(o) {
var from, plusPart, to;
from = this.range.from.compile(o);
to = this.range.to.compile(o);
plusPart = this.range.exclusive ? '' : ' + 1';
return ".slice(" + (from) + ", " + (to) + (plusPart) + ")";
var from, to;
from = this.range.from ? this.range.from.compile(o) : '0';
to = this.range.to ? this.range.to.compile(o) : '';
to += (!to || this.range.exclusive ? '' : ' + 1');
if (to) {
to = ', ' + to;
}
return ".slice(" + (from) + (to) + ")";
};
return SliceNode;
})();
Expand Down Expand Up @@ -990,8 +993,8 @@
l = this.variable.properties.length;
range = this.variable.properties[l - 1].range;
plus = range.exclusive ? '' : ' + 1';
from = range.from.compile(o);
to = range.to.compile(o) + ' - ' + from + plus;
from = range.from ? range.from.compile(o) : '0';
to = range.to ? range.to.compile(o) + ' - ' + from + plus : ("" + (name) + ".length");
val = this.value.compile(o);
return "" + (name) + ".splice.apply(" + (name) + ", [" + (from) + ", " + (to) + "].concat(" + (val) + "))";
};
Expand Down

0 comments on commit 4ddd65a

Please sign in to comment.