Slice and Splice tests are back on master.
This commit is contained in:
parent
9111c2e702
commit
1f12642af2
|
@ -164,7 +164,9 @@
|
|||
return new Accessor($2, 'proto');
|
||||
}), o('::', function() {
|
||||
return new Accessor(new Literal('prototype'));
|
||||
}), o('Index')
|
||||
}), o('Index'), o('Slice', function() {
|
||||
return new Slice($1);
|
||||
})
|
||||
],
|
||||
Index: [
|
||||
o('INDEX_START Expression INDEX_END', function() {
|
||||
|
|
|
@ -1077,6 +1077,9 @@
|
|||
if (this.variable.isArray() || this.variable.isObject()) {
|
||||
return this.compilePatternMatch(o);
|
||||
}
|
||||
if (this.variable.isSplice()) {
|
||||
return this.compileSplice(o);
|
||||
}
|
||||
if ((_ref = this.context) === '||=' || _ref === '&&=' || _ref === '?=') {
|
||||
return this.compileConditional(o);
|
||||
}
|
||||
|
|
232
lib/parser.js
232
lib/parser.js
File diff suppressed because one or more lines are too long
|
@ -245,6 +245,7 @@ grammar =
|
|||
o ':: Identifier', -> new Accessor $2, 'proto'
|
||||
o '::', -> new Accessor new Literal 'prototype'
|
||||
o 'Index'
|
||||
o 'Slice', -> new Slice $1
|
||||
]
|
||||
|
||||
# Indexing into an object or array using bracket notation.
|
||||
|
|
|
@ -871,6 +871,7 @@ exports.Assign = class Assign extends Base
|
|||
compileNode: (o) ->
|
||||
if isValue = @variable instanceof Value
|
||||
return @compilePatternMatch o if @variable.isArray() or @variable.isObject()
|
||||
return @compileSplice o if @variable.isSplice()
|
||||
return @compileConditional o if @context in ['||=', '&&=', '?=']
|
||||
name = @variable.compile o, LEVEL_LIST
|
||||
if @value instanceof Code and match = @METHOD_DEF.exec name
|
||||
|
|
|
@ -16,14 +16,34 @@ ok odds.join(' ') is "one! three!"
|
|||
|
||||
# Basic range comprehensions.
|
||||
nums = (i * 3 for i in [1..3])
|
||||
|
||||
negs = (x for x in [-20..-5*2])
|
||||
eq nums.concat(negs.slice 0, 3).join(' '), '3 6 9 -20 -19 -18'
|
||||
negs = negs[0..2]
|
||||
|
||||
result = nums.concat(negs).join(', ')
|
||||
|
||||
ok result is '3, 6, 9, -20, -19, -18'
|
||||
|
||||
|
||||
# With range comprehensions, you can loop in steps.
|
||||
eq "#{ x for x in [0..9] by 3 }", '0,3,6,9'
|
||||
eq "#{ x for x in [9..0] by -3 }", '9,6,3,0'
|
||||
eq "#{ x for x in [3*3..0*0] by 0-3 }", '9,6,3,0'
|
||||
results = (x for x in [0...15] by 5)
|
||||
ok results.join(' ') is '0 5 10'
|
||||
|
||||
results = (x for x in [0..100] by 10)
|
||||
ok results.join(' ') is '0 10 20 30 40 50 60 70 80 90 100'
|
||||
|
||||
|
||||
# And can loop downwards, with a negative step.
|
||||
results = (x for x in [5..1])
|
||||
|
||||
ok results.join(' ') is '5 4 3 2 1'
|
||||
ok results.join(' ') is [(10-5)..(-2+3)].join(' ')
|
||||
|
||||
results = (x for x in [10..1])
|
||||
ok results.join(' ') is [10..1].join(' ')
|
||||
|
||||
results = (x for x in [10...0] by -2)
|
||||
ok results.join(' ') is [10, 8, 6, 4, 2].join(' ')
|
||||
|
||||
|
||||
# Range comprehension gymnastics.
|
||||
|
@ -110,6 +130,11 @@ results = for i in [1..3]
|
|||
ok results.join(', ') is '3 , 3 6 , 3 6 9'
|
||||
|
||||
|
||||
# Naked ranges are expanded into arrays.
|
||||
array = [0..10]
|
||||
ok(num % 2 is 0 for num in array by 2)
|
||||
|
||||
|
||||
# Nested comprehensions.
|
||||
multiLiner =
|
||||
for x in [3..5]
|
||||
|
@ -153,6 +178,11 @@ ok own.join(' ') is 'Whiskers'
|
|||
ok all.sort().join(' ') is 'Whiskers cream tabby'
|
||||
|
||||
|
||||
# Optimized range comprehensions.
|
||||
exxes = ('x' for [0...10])
|
||||
ok exxes.join(' ') is 'x x x x x x x x x x'
|
||||
|
||||
|
||||
# Comprehensions safely redeclare parameters if they're not present in closest
|
||||
# scope.
|
||||
rule = (x) -> x
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
# # Slice.
|
||||
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
|
||||
a = array[7..9]
|
||||
b = array[2...4]
|
||||
|
||||
result = a.concat(b).join(' ')
|
||||
|
||||
ok result is "7 8 9 2 3"
|
||||
|
||||
a = [0, 1, 2, 3, 4, 5, 6, 7]
|
||||
eq a[2...6].join(' '), '2 3 4 5'
|
||||
|
||||
|
||||
# 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]
|
||||
ok array.join(' ') is "6 7 8 9 10"
|
||||
|
||||
array = [5..1]
|
||||
ok array.join(' ') is '5 4 3 2 1'
|
||||
|
||||
array = [30...0]
|
||||
ok (len = array.length) is 30
|
||||
ok array[len - 1] is 1
|
||||
|
||||
|
||||
|
||||
# String slicing (at least on Node).
|
||||
hello = "Hello World"
|
||||
|
||||
ok hello[1...1] is ""
|
||||
ok hello[1..1] is "e"
|
||||
ok hello[1...5] is "ello"
|
||||
ok hello[0..4] is "Hello"
|
||||
|
||||
|
||||
# Splice literals.
|
||||
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
|
||||
array[5..10] = [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'
|
Loading…
Reference in New Issue