Issue #621. Added the ability to leave the start and end index off of arrays. list[5..] is now valid CoffeeScript, slicing to the end of the array.

This commit is contained in:
Jeremy Ashkenas
2010-08-18 21:51:44 -04:00
parent bf6bafa3ac
commit 4ddd65a4c4
6 changed files with 145 additions and 104 deletions

View File

@@ -307,6 +307,14 @@
return new RangeNode($2, $5); return new RangeNode($2, $5);
}), o("INDEX_START Expression . . . Expression INDEX_END", function() { }), o("INDEX_START Expression . . . Expression INDEX_END", function() {
return new RangeNode($2, $6, true); 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: [ Array: [

View File

@@ -692,11 +692,14 @@
SliceNode.prototype["class"] = 'SliceNode'; SliceNode.prototype["class"] = 'SliceNode';
SliceNode.prototype.children = ['range']; SliceNode.prototype.children = ['range'];
SliceNode.prototype.compileNode = function(o) { SliceNode.prototype.compileNode = function(o) {
var from, plusPart, to; var from, to;
from = this.range.from.compile(o); from = this.range.from ? this.range.from.compile(o) : '0';
to = this.range.to.compile(o); to = this.range.to ? this.range.to.compile(o) : '';
plusPart = this.range.exclusive ? '' : ' + 1'; to += (!to || this.range.exclusive ? '' : ' + 1');
return ".slice(" + (from) + ", " + (to) + (plusPart) + ")"; if (to) {
to = ', ' + to;
}
return ".slice(" + (from) + (to) + ")";
}; };
return SliceNode; return SliceNode;
})(); })();
@@ -990,8 +993,8 @@
l = this.variable.properties.length; l = this.variable.properties.length;
range = this.variable.properties[l - 1].range; range = this.variable.properties[l - 1].range;
plus = range.exclusive ? '' : ' + 1'; plus = range.exclusive ? '' : ' + 1';
from = range.from.compile(o); from = range.from ? range.from.compile(o) : '0';
to = range.to.compile(o) + ' - ' + from + plus; to = range.to ? range.to.compile(o) + ' - ' + from + plus : ("" + (name) + ".length");
val = this.value.compile(o); val = this.value.compile(o);
return "" + (name) + ".splice.apply(" + (name) + ", [" + (from) + ", " + (to) + "].concat(" + (val) + "))"; return "" + (name) + ".splice.apply(" + (name) + ", [" + (from) + ", " + (to) + "].concat(" + (val) + "))";
}; };

File diff suppressed because one or more lines are too long

View File

@@ -348,6 +348,10 @@ grammar =
Slice: [ Slice: [
o "INDEX_START Expression . . Expression INDEX_END", -> new RangeNode $2, $5 o "INDEX_START Expression . . Expression INDEX_END", -> new RangeNode $2, $5
o "INDEX_START Expression . . . Expression INDEX_END", -> new RangeNode $2, $6, true o "INDEX_START Expression . . . Expression INDEX_END", -> new RangeNode $2, $6, true
o "INDEX_START Expression . . INDEX_END", -> new RangeNode $2, null
o "INDEX_START Expression . . . INDEX_END", -> new RangeNode $2, null, true
o "INDEX_START . . Expression INDEX_END", -> new RangeNode null, $4
o "INDEX_START . . . Expression INDEX_END", -> new RangeNode null, $5, true
] ]
# The array literal. # The array literal.

View File

@@ -621,10 +621,11 @@ exports.SliceNode = class SliceNode extends BaseNode
super() super()
compileNode: (o) -> compileNode: (o) ->
from = @range.from.compile(o) from = if @range.from then @range.from.compile(o) else '0'
to = @range.to.compile(o) to = if @range.to then @range.to.compile(o) else ''
plusPart = if @range.exclusive then '' else ' + 1' to += if not to or @range.exclusive then '' else ' + 1'
".slice(#{from}, #{to}#{plusPart})" to = ', ' + to if to
".slice(#{from}#{to})"
#### ObjectNode #### ObjectNode
@@ -860,8 +861,8 @@ exports.AssignNode = class AssignNode extends BaseNode
l = @variable.properties.length l = @variable.properties.length
range = @variable.properties[l - 1].range range = @variable.properties[l - 1].range
plus = if range.exclusive then '' else ' + 1' plus = if range.exclusive then '' else ' + 1'
from = range.from.compile(o) from = if range.from then range.from.compile(o) else '0'
to = range.to.compile(o) + ' - ' + from + plus to = if range.to then range.to.compile(o) + ' - ' + from + plus else "#{name}.length"
val = @value.compile(o) val = @value.compile(o)
"#{name}.splice.apply(#{name}, [#{from}, #{to}].concat(#{val}))" "#{name}.splice.apply(#{name}, [#{from}, #{to}].concat(#{val}))"

View File

@@ -56,3 +56,20 @@ array[5..10] = [0, 0, 0]
ok array.join(' ') is '0 1 2 3 4 0 0 0' ok array.join(' ') is '0 1 2 3 4 0 0 0'
# Slices and splices that omit their beginning or end.
array = [0..10]
ok array[7..].join(' ') is '7 8 9 10'
ok array[-2..].join(' ') is '9 10'
ok array[...3].join(' ') is '0 1 2'
ok array[..-5].join(' ') is '0 1 2 3 4 5 6'
array[3..] = [9, 8, 7]
ok array.join(' ') is '0 1 2 9 8 7'
array[...3] = [7, 8, 9]
ok array.join(' ') is '7 8 9 9 8 7'