Skip to content

Commit

Permalink
Merge pull request #750 from kkirby/yield-inside-for-let
Browse files Browse the repository at this point in the history
Fix for #749: Yield inside for let
  • Loading branch information
gkz committed Jul 8, 2015
2 parents db4404b + 23b1083 commit 7a549b4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
17 changes: 13 additions & 4 deletions lib/ast.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/ast.ls
Original file line number Diff line number Diff line change
Expand Up @@ -924,15 +924,15 @@ class exports.Call extends Node
++index
node <<< back: (args[index] = fun)body

@let = (args, body) ->
@let = (args, body, generator = false) ->
params = for a, i in args
if a.op is \= and not a.logic and a.right
args[i] = that
continue if i is 0 and gotThis = a.left.value is \this
a.left
else Var a.var-name! || a.carp 'invalid "let" argument'
gotThis or args.unshift Literal \this
@block Fun(params, body), args, \.call
@block Fun(params, body, null, null, null, generator), args, \.call

#### List
# An abstract node for a list of comma-separated items.
Expand Down Expand Up @@ -2299,13 +2299,16 @@ class exports.For extends While
show: -> ((@kind || []) ++ @index).join ' '

add-body: (body) ->
has-yield = !!body.traverse-children (child) ->
return true if child instanceof Yield
if @let
@item = Literal \.. if delete @ref
body = Block Call.let do
with []
..push Assign Var(that), Literal \index$$ if @index
..push Assign that, Literal \item$$ if @item
body
has-yield

super body

Expand All @@ -2317,6 +2320,7 @@ class exports.For extends While
if @item and it.value is @item.value
it.value = \item$$
if @let
@body := Block Yield \yieldfrom, body if has-yield
delete @index
delete @item
this
Expand Down
14 changes: 14 additions & 0 deletions test/generators.ls
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,17 @@ f7 = (x) ->*
y
g7 = f7 true
eq 1 g7.next!.value

f8 = ->*
result = for let i in [1,2,3]
yield i
-> i * 2
result
g8 = f8!
eq 1 g8.next!value
eq 2 g8.next!value
eq 3 g8.next!value
g8_result = g8.next!value
eq 2 g8_result[0]()
eq 4 g8_result[1]()
eq 6 g8_result[2]()

0 comments on commit 7a549b4

Please sign in to comment.