Skip to content

Commit 02a5dbc

Browse files
committed
fix compile failure when comprehension exp has no value
1 parent f7a9324 commit 02a5dbc

File tree

6 files changed

+42
-18
lines changed

6 files changed

+42
-18
lines changed

moonscript/transform.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ local data = require("moonscript.data")
55
local reversed = util.reversed
66
local ntype, build, smart_node, is_slice = types.ntype, types.build, types.smart_node, types.is_slice
77
local insert = table.insert
8-
local is_value
9-
is_value = function(stm)
10-
return moonscript.compile.Block:is_value(stm) or Value:can_transform(stm)
11-
end
128
NameProxy = (function()
139
local _parent_0 = nil
1410
local _base_0 = {
@@ -241,7 +237,7 @@ Statement = Transformer({
241237
if #values == 1 and types.cascading[ntype(values[1])] then
242238
values[1] = self.transform.statement(values[1], function(stm)
243239
local t = ntype(stm)
244-
if is_value(stm) then
240+
if types.is_value(stm) then
245241
return {
246242
"assign",
247243
names,
@@ -843,15 +839,19 @@ implicitly_return = function(scope)
843839
local fn
844840
fn = function(stm)
845841
local t = ntype(stm)
846-
if types.manual_return[t] or not is_value(stm) then
842+
if types.manual_return[t] or not types.is_value(stm) then
847843
return stm
848844
elseif types.cascading[t] then
849845
return scope.transform.statement(stm, fn)
850846
else
851-
return {
852-
"return",
853-
stm
854-
}
847+
if t == "comprehension" and not types.comprehension_has_value(stm) then
848+
return stm
849+
else
850+
return {
851+
"return",
852+
stm
853+
}
854+
end
855855
end
856856
end
857857
return fn

moonscript/transform.moon

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ import insert from table
1111

1212
export Statement, Value, NameProxy, Run
1313

14-
-- TODO refactor
15-
is_value = (stm) ->
16-
moonscript.compile.Block\is_value(stm) or Value\can_transform stm
17-
1814
class NameProxy
1915
new: (@prefix) =>
2016
self[1] = "temp_name"
@@ -131,7 +127,7 @@ Statement = Transformer {
131127
if #values == 1 and types.cascading[ntype values[1]]
132128
values[1] = @transform.statement values[1], (stm) ->
133129
t = ntype stm
134-
if is_value stm
130+
if types.is_value stm
135131
{"assign", names, {stm}}
136132
else
137133
stm
@@ -201,6 +197,7 @@ Statement = Transformer {
201197

202198
comprehension: (node, action) =>
203199
_, exp, clauses = unpack node
200+
204201
action = action or (exp) -> {exp}
205202
construct_comprehension action(exp), clauses
206203

@@ -470,12 +467,15 @@ default_accumulator = (node) =>
470467
implicitly_return = (scope) ->
471468
fn = (stm) ->
472469
t = ntype stm
473-
if types.manual_return[t] or not is_value stm
470+
if types.manual_return[t] or not types.is_value stm
474471
stm
475472
elseif types.cascading[t]
476473
scope.transform.statement stm, fn
477474
else
478-
{"return", stm}
475+
if t == "comprehension" and not types.comprehension_has_value stm
476+
stm
477+
else
478+
{"return", stm}
479479

480480
fn
481481

moonscript/types.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ cascading = data.Set({
1313
"with",
1414
"switch"
1515
})
16+
is_value = function(stm)
17+
local compile, transform = moonscript.compile, moonscript.transform
18+
return compile.Block:is_value(stm) or transform.Value:can_transform(stm)
19+
end
20+
comprehension_has_value = function(comp)
21+
return is_value(comp[2])
22+
end
1623
ntype = function(node)
1724
if type(node) ~= "table" then
1825
return "value"

moonscript/types.moon

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ module "moonscript.types", package.seeall
22
util = require "moonscript.util"
33
data = require "moonscript.data"
44

5-
export ntype, smart_node, build
5+
export ntype, smart_node, build, is_value
66
export is_slice, manual_return, cascading
7+
export comprehension_has_value
78

89
import insert from table
910

@@ -13,6 +14,13 @@ manual_return = data.Set{"foreach", "for", "while", "return"}
1314
-- assigns and returns are bubbled into their bodies
1415
cascading = data.Set{ "if", "with", "switch" }
1516

17+
is_value = (stm) ->
18+
import compile, transform from moonscript
19+
compile.Block\is_value(stm) or transform.Value\can_transform stm
20+
21+
comprehension_has_value = (comp) ->
22+
is_value comp[2]
23+
1624
-- type of node as string
1725
ntype = (node) ->
1826
if type(node) != "table"

tests/inputs/lists.moon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ normal = (hello) ->
6969
test = x 1,2,3,4,5
7070
print thing for thing in *test
7171

72+
-> a = b for row in *rows
7273

tests/outputs/lists.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,12 @@ local _list_8 = test
270270
for _index_0 = 1, #_list_8 do
271271
local thing = _list_8[_index_0]
272272
print(thing)
273+
end
274+
local _
275+
_ = function()
276+
local _list_9 = rows
277+
for _index_0 = 1, #_list_9 do
278+
local row = _list_9[_index_0]
279+
a = b
280+
end
273281
end

0 commit comments

Comments
 (0)