Skip to content

Commit df7ca32

Browse files
committed
handing the transformation of decorator with implicit return in all cases
1 parent b404f2d commit df7ca32

File tree

7 files changed

+97
-51
lines changed

7 files changed

+97
-51
lines changed

moonscript/dump.lua

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,9 @@ value = function(op)
2828
return flat_value(op)
2929
end
3030
tree = function(block)
31-
return (function()
32-
local _accum_0 = { }
33-
local _len_0 = 0
34-
local _list_0 = block
35-
for _index_0 = 1, #_list_0 do
36-
value = _list_0[_index_0]
37-
_len_0 = _len_0 + 1
38-
_accum_0[_len_0] = print(flat_value(value))
39-
end
40-
return _accum_0
41-
end)()
31+
local _list_0 = block
32+
for _index_0 = 1, #_list_0 do
33+
value = _list_0[_index_0]
34+
print(flat_value(value))
35+
end
4236
end

moonscript/transform.lua

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -376,32 +376,38 @@ construct_comprehension = function(inner, clauses)
376376
end
377377
Statement = Transformer({
378378
assign = function(self, node)
379-
local _, names, values = unpack(node)
380-
if #values == 1 and types.cascading[ntype(values[1])] then
381-
values[1] = self.transform.statement(values[1], function(stm)
382-
local t = ntype(stm)
383-
if types.is_value(stm) then
384-
return {
385-
"assign",
386-
names,
387-
{
388-
stm
389-
}
390-
}
391-
else
392-
return stm
393-
end
394-
end)
395-
return build.group({
396-
{
397-
"declare",
398-
names
399-
},
400-
values[1]
401-
})
402-
else
403-
return node
379+
local names, values = unpack(node, 2)
380+
local transformed
381+
if #values == 1 then
382+
local value = values[1]
383+
local t = ntype(value)
384+
if t == "decorated" then
385+
value = self.transform.statement(value)
386+
t = ntype(value)
387+
end
388+
if types.cascading[t] then
389+
transformed = build.group({
390+
{
391+
"declare",
392+
names
393+
},
394+
self.transform.statement(value, function(stm)
395+
if types.is_value(stm) then
396+
return {
397+
"assign",
398+
names,
399+
{
400+
stm
401+
}
402+
}
403+
else
404+
return stm
405+
end
406+
end)
407+
})
408+
end
404409
end
410+
return transformed or node
405411
end,
406412
export = function(self, node)
407413
if #node > 2 then
@@ -1258,6 +1264,9 @@ Value = Transformer({
12581264
["for"] = default_accumulator,
12591265
["while"] = default_accumulator,
12601266
foreach = default_accumulator,
1267+
decorated = function(self, node)
1268+
return self.transform.statement(node)
1269+
end,
12611270
string = function(self, node)
12621271
local delim = node[2]
12631272
local convert_part

moonscript/transform.moon

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,28 @@ construct_comprehension = (inner, clauses) ->
160160

161161
Statement = Transformer {
162162
assign: (node) =>
163-
_, names, values = unpack node
163+
names, values = unpack node, 2
164164
-- bubble cascading assigns
165-
if #values == 1 and types.cascading[ntype values[1]]
166-
values[1] = @transform.statement values[1], (stm) ->
167-
t = ntype stm
168-
if types.is_value stm
169-
{"assign", names, {stm}}
170-
else
171-
stm
165+
transformed = if #values == 1
166+
value = values[1]
167+
t = ntype value
168+
169+
if t == "decorated"
170+
value = @transform.statement value
171+
t = ntype value
172+
173+
if types.cascading[t]
174+
build.group {
175+
{"declare", names}
176+
@transform.statement value, (stm) ->
177+
if types.is_value stm
178+
{"assign", names, {stm}}
179+
else
180+
stm
181+
}
182+
183+
transformed or node
172184

173-
build.group {
174-
{"declare", names}
175-
values[1]
176-
}
177-
else
178-
node
179185

180186
export: (node) =>
181187
-- assign values if they are included
@@ -655,6 +661,9 @@ Value = Transformer {
655661
while: default_accumulator
656662
foreach: default_accumulator
657663

664+
decorated: (node) =>
665+
@transform.statement node
666+
658667
string: (node) =>
659668
delim = node[2]
660669

moonscript/types.moon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import insert from table
1111
-- implicit return does not work on these statements
1212
manual_return = data.Set{"foreach", "for", "while", "return"}
1313

14-
-- assigns and returns are bubbled into their bodies
14+
-- Assigns and returns are bubbled into their bodies.
15+
-- All cascading statement transform functions accept a second arugment that
16+
-- is the transformation to apply to the last statement in their body
1517
cascading = data.Set{ "if", "unless", "with", "switch" }
1618

1719
is_value = (stm) ->

tests/inputs/lists.moon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ print thing for thing in *test
7171

7272
-> a = b for row in *rows
7373

74+
-- testing implicit return
75+
-> x for x in *things
76+
-> [x for x in *things]
77+
78+

tests/outputs/lists.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,24 @@ _ = function()
278278
local row = _list_9[_index_0]
279279
a = b
280280
end
281+
end
282+
_ = function()
283+
local _list_9 = things
284+
for _index_0 = 1, #_list_9 do
285+
x = _list_9[_index_0]
286+
_ = x
287+
end
288+
end
289+
_ = function()
290+
return (function()
291+
local _accum_0 = { }
292+
local _len_0 = 0
293+
local _list_9 = things
294+
for _index_0 = 1, #_list_9 do
295+
x = _list_9[_index_0]
296+
_len_0 = _len_0 + 1
297+
_accum_0[_len_0] = x
298+
end
299+
return _accum_0
300+
end)()
281301
end

todo

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@ not working right:
5555

5656
* convert tree transformer to be depth first instead of breadth first (lazy)
5757

58+
59+
-- for searching?
60+
x = for thing in *things
61+
if is_important thing
62+
break thing
63+
64+

0 commit comments

Comments
 (0)