Skip to content

Commit e0da63c

Browse files
committed
assignable name list in comprehension/decorator for in loop, fixes #236
1 parent c024c83 commit e0da63c

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed

moonscript/parse.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ local build_grammar = wrap_env(debug_grammar, function(root)
142142
Comprehension = sym("[") * Exp * CompInner * sym("]") / mark("comprehension"),
143143
TblComprehension = sym("{") * Ct(Exp * (sym(",") * Exp) ^ -1) * CompInner * sym("}") / mark("tblcomprehension"),
144144
CompInner = Ct((CompForEach + CompFor) * CompClause ^ 0),
145-
CompForEach = key("for") * Ct(NameList) * key("in") * (sym("*") * Exp / mark("unpack") + Exp) / mark("foreach"),
145+
CompForEach = key("for") * Ct(AssignableNameList) * key("in") * (sym("*") * Exp / mark("unpack") + Exp) / mark("foreach"),
146146
CompFor = key("for" * Name * sym("=") * Ct(Exp * sym(",") * Exp * (sym(",") * Exp) ^ -1) / mark("for")),
147147
CompClause = CompFor + CompForEach + key("when") * Exp / mark("when"),
148148
Assign = sym("=") * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark("assign"),

moonscript/parse.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ build_grammar = wrap_env debug_grammar, (root) ->
171171
TblComprehension: sym"{" * Ct(Exp * (sym"," * Exp)^-1) * CompInner * sym"}" / mark"tblcomprehension"
172172

173173
CompInner: Ct((CompForEach + CompFor) * CompClause^0)
174-
CompForEach: key"for" * Ct(NameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) / mark"foreach"
174+
CompForEach: key"for" * Ct(AssignableNameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) / mark"foreach"
175175
CompFor: key "for" * Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1) / mark"for"
176176
CompClause: CompFor + CompForEach + key"when" * Exp / mark"when"
177177

spec/inputs/comprehension.moon

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,17 @@ dd = [y for i=1,10 when cool for thing in y when x > 3 when c + 3]
3030

3131
{"hello", "world" for i=1,10}
3232

33+
--
34+
35+
j = [a for {a,b,c} in things]
36+
k = [a for {a,b,c} in *things]
37+
i = [hello for {:hello, :world} in *things]
38+
39+
hj = {a,c for {a,b,c} in things}
40+
hk = {a,c for {a,b,c} in *things}
41+
hi = {hello,world for {:hello, :world} in *things}
42+
43+
ok(a,b,c) for {a,b,c} in things
44+
45+
3346
nil

spec/outputs/comprehension.lua

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,83 @@ do
168168
end
169169
_ = _tbl_0
170170
end
171+
local j
172+
do
173+
local _accum_0 = { }
174+
local _len_0 = 1
175+
for _des_0 in things do
176+
local a, b, c
177+
a, b, c = _des_0[1], _des_0[2], _des_0[3]
178+
_accum_0[_len_0] = a
179+
_len_0 = _len_0 + 1
180+
end
181+
j = _accum_0
182+
end
183+
local k
184+
do
185+
local _accum_0 = { }
186+
local _len_0 = 1
187+
local _list_0 = things
188+
for _index_0 = 1, #_list_0 do
189+
local _des_0 = _list_0[_index_0]
190+
local a, b, c
191+
a, b, c = _des_0[1], _des_0[2], _des_0[3]
192+
_accum_0[_len_0] = a
193+
_len_0 = _len_0 + 1
194+
end
195+
k = _accum_0
196+
end
197+
local i
198+
do
199+
local _accum_0 = { }
200+
local _len_0 = 1
201+
local _list_0 = things
202+
for _index_0 = 1, #_list_0 do
203+
local _des_0 = _list_0[_index_0]
204+
local hello, world
205+
hello, world = _des_0.hello, _des_0.world
206+
_accum_0[_len_0] = hello
207+
_len_0 = _len_0 + 1
208+
end
209+
i = _accum_0
210+
end
211+
local hj
212+
do
213+
local _tbl_0 = { }
214+
for _des_0 in things do
215+
local a, b, c
216+
a, b, c = _des_0[1], _des_0[2], _des_0[3]
217+
_tbl_0[a] = c
218+
end
219+
hj = _tbl_0
220+
end
221+
local hk
222+
do
223+
local _tbl_0 = { }
224+
local _list_0 = things
225+
for _index_0 = 1, #_list_0 do
226+
local _des_0 = _list_0[_index_0]
227+
local a, b, c
228+
a, b, c = _des_0[1], _des_0[2], _des_0[3]
229+
_tbl_0[a] = c
230+
end
231+
hk = _tbl_0
232+
end
233+
local hi
234+
do
235+
local _tbl_0 = { }
236+
local _list_0 = things
237+
for _index_0 = 1, #_list_0 do
238+
local _des_0 = _list_0[_index_0]
239+
local hello, world
240+
hello, world = _des_0.hello, _des_0.world
241+
_tbl_0[hello] = world
242+
end
243+
hi = _tbl_0
244+
end
245+
for _des_0 in things do
246+
local a, b, c
247+
a, b, c = _des_0[1], _des_0[2], _des_0[3]
248+
ok(a, b, c)
249+
end
171250
return nil

0 commit comments

Comments
 (0)