Fix #3671: Allow step in optimized range comprehensions

Allow the `by c` part in `for [a..b] by c then`.

Continue disallowing a `when d` part, since it makes no sense having a guard
that isn't given access to anything that changes on every iteration.
This commit is contained in:
Simon Lydell 2015-01-10 02:31:56 +01:00
parent e769423d52
commit a63009fccb
4 changed files with 82 additions and 61 deletions

View File

@ -421,6 +421,11 @@
return {
source: LOC(2)(new Value($2))
};
}), o('FOR Range BY Expression', function() {
return {
source: LOC(2)(new Value($2)),
step: $4
};
}), o('ForStart ForSource', function() {
$2.own = $1.own;
$2.name = $1[0];

File diff suppressed because one or more lines are too long

View File

@ -451,7 +451,8 @@ grammar =
]
ForBody: [
o 'FOR Range', -> source: LOC(2) new Value($2)
o 'FOR Range', -> source: (LOC(2) new Value($2))
o 'FOR Range BY Expression', -> source: (LOC(2) new Value($2)), step: $4
o 'ForStart ForSource', -> $2.own = $1.own; $2.name = $1[0]; $2.index = $1[1]; $2
]

View File

@ -246,6 +246,17 @@ test "Optimized range comprehensions.", ->
ok exxes.join(' ') is 'x x x x x x x x x x'
test "#3671: Allow step in optimized range comprehensions.", ->
exxes = ('x' for [0...10] by 2)
eq exxes.join(' ') , 'x x x x x'
test "#3671: Disallow guard in optimized range comprehensions.", ->
throws -> CoffeeScript.compile "exxes = ('x' for [0...10] when a)"
test "Loop variables should be able to reference outer variables", ->
outer = 1
do ->