Skip to content

Commit 26c8891

Browse files
committed
for let with when fix
1 parent de969c5 commit 26c8891

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

lib/ast.js

Lines changed: 24 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ast.ls

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ class exports.Fun extends Node
16981698
if vr.isEmpty!
16991699
vr = Var scope.temporary \arg
17001700
else if vr.value is \..
1701-
vr = Var o.ref = scope.reference!
1701+
vr = Var o.ref = scope.temporary!
17021702
else if vr not instanceof Var
17031703
unaries = []
17041704
while vr instanceof Unary
@@ -1977,7 +1977,7 @@ class exports.While extends Node
19771977
for node in @body?.lines or [] then return node if node.getJump ctx
19781978

19791979
addBody: (@body) ->
1980-
@body = Block If @guard, body if @guard
1980+
@body = Block If @guard, @body if @guard
19811981
[top] = @body.lines
19821982
@body.lines.length = 0 if top?verb is \continue and not top.label
19831983
this
@@ -2080,11 +2080,24 @@ class exports.For extends While
20802080
@item = Literal \.. if delete @ref
20812081
body = Block Call.let do
20822082
with []
2083-
..push Assign Var(that), Literal \index if delete @index
2084-
..push Assign that, Literal \item if delete @item
2083+
..push Assign Var(that), Literal \index$$ if @index
2084+
..push Assign that, Literal \item$$ if @item
20852085
body
2086+
20862087
super body
20872088

2089+
if @guard and @let and (@index or @item)
2090+
@body.lines[0].if.traverse-children !~>
2091+
if it instanceof Var
2092+
if @index and it.value is @index
2093+
it.value = \index$$
2094+
if @item and it.value is @item.value
2095+
it.value = \item$$
2096+
if @let
2097+
delete @index
2098+
delete @item
2099+
this
2100+
20882101
compileNode: (o) ->
20892102
o.loop = true
20902103
temps = @temps = []
@@ -2141,8 +2154,8 @@ class exports.For extends While
21412154
if @let
21422155
@body.traverseChildren !->
21432156
switch it.value
2144-
| \index => it.value = idx
2145-
| \item => it.value = "#svar[#idx]"
2157+
| \index$$ => it.value = idx
2158+
| \item$$ => it.value = "#svar[#idx]"
21462159
else
21472160
@infuseIIFE!
21482161
o.indent += TAB

test/loop.ls

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,18 @@ for let k, v of {a: \b}
594594
ok true
595595
ok 7 is i is v
596596

597-
fns = for <[foo bar]>
598-
for let i in [7 8]
599-
-> .. + i
600-
eq \foo7 fns.0.0!
601-
eq \bar8 fns.1.1!
597+
fns = for let <[foo bar]>
598+
for let x in [6 7 8 9] when x % 2 == 0
599+
-> .. + x
600+
eq \foo6 fns.0.0!
601+
eq \bar8 fns.1.1!
602+
603+
xs = for let x, i in [1 to 10] by 2 when x % 3 == 0
604+
-> i + x
605+
eq 5, xs[0]!
606+
eq 17, xs[1]!
607+
608+
xs = for own let key, value of {a: 1, b: 2, c: 3, d: 4} when value % 2 == 0
609+
-> key + value
610+
eq 'b2', xs[0]!
611+
eq 'd4', xs[1]!

0 commit comments

Comments
 (0)