Skip to content

Commit c8d671a

Browse files
committed
no temporary functions when returning block_exp
1 parent 7085304 commit c8d671a

File tree

7 files changed

+106
-73
lines changed

7 files changed

+106
-73
lines changed

moon/init.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,11 @@ extend = function(...)
108108
return tbls[1]
109109
end
110110
copy = function(self)
111-
return (function()
112-
local _tbl_0 = { }
113-
for key, val in pairs(self) do
114-
_tbl_0[key] = val
115-
end
116-
return _tbl_0
117-
end)()
111+
local _tbl_0 = { }
112+
for key, val in pairs(self) do
113+
_tbl_0[key] = val
114+
end
115+
return _tbl_0
118116
end
119117
mixin = function(self, cls, ...)
120118
for key, val in pairs(cls.__base) do

moonscript/compile.lua

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,14 @@ do
119119
local strip
120120
strip = function(t)
121121
if "table" == type(t) then
122-
return (function()
123-
local _accum_0 = { }
124-
local _len_0 = 1
125-
for _index_0 = 1, #t do
126-
local v = t[_index_0]
127-
_accum_0[_len_0] = strip(v)
128-
_len_0 = _len_0 + 1
129-
end
130-
return _accum_0
131-
end)()
122+
local _accum_0 = { }
123+
local _len_0 = 1
124+
for _index_0 = 1, #t do
125+
local v = t[_index_0]
126+
_accum_0[_len_0] = strip(v)
127+
_len_0 = _len_0 + 1
128+
end
129+
return _accum_0
132130
else
133131
return t
134132
end

moonscript/transform.lua

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ with_continue_listener = function(body)
210210
end
211211
do
212212
local _base_0 = {
213+
transform_once = function(self, scope, node, ...)
214+
if self.seen_nodes[node] then
215+
return node
216+
end
217+
self.seen_nodes[node] = true
218+
local transformer = self.transformers[ntype(node)]
219+
if transformer then
220+
return transformer(scope, node, ...) or node
221+
else
222+
return node
223+
end
224+
end,
213225
transform = function(self, scope, node, ...)
214226
if self.seen_nodes[node] then
215227
return node
@@ -314,6 +326,18 @@ Statement = Transformer({
314326
root_stms = function(self, body)
315327
return apply_to_last(body, implicitly_return(self))
316328
end,
329+
["return"] = function(self, node)
330+
node[2] = Value:transform_once(self, node[2])
331+
if "block_exp" == ntype(node[2]) then
332+
local block_exp = node[2]
333+
local block_body = block_exp[2]
334+
local idx = #block_body
335+
node[2] = block_body[idx]
336+
block_body[idx] = node
337+
return build.group(block_body)
338+
end
339+
return node
340+
end,
317341
declare_glob = function(self, node)
318342
local names = extract_declarations(self)
319343
if node[2] == "^" then
@@ -370,7 +394,7 @@ Statement = Transformer({
370394
}
371395
})
372396
elseif "comprehension" == _exp_0 or "tblcomprehension" == _exp_0 or "foreach" == _exp_0 or "for" == _exp_0 or "while" == _exp_0 then
373-
return build.assign_one(first_name, Value.transformers[first_value[1]](self, first_value))
397+
return build.assign_one(first_name, Value:transform_once(self, first_value))
374398
end
375399
end
376400
local transformed

moonscript/transform.moon

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ class Transformer
108108
new: (@transformers) =>
109109
@seen_nodes = setmetatable {}, __mode: "k"
110110

111+
transform_once: (scope, node, ...) =>
112+
return node if @seen_nodes[node]
113+
@seen_nodes[node] = true
114+
115+
transformer = @transformers[ntype node]
116+
if transformer
117+
transformer(scope, node, ...) or node
118+
else
119+
node
120+
111121
transform: (scope, node, ...) =>
112122
return node if @seen_nodes[node]
113123
@seen_nodes[node] = true
@@ -158,6 +168,20 @@ Statement = Transformer {
158168
root_stms: (body) =>
159169
apply_to_last body, implicitly_return @
160170

171+
return: (node) =>
172+
node[2] = Value\transform_once @, node[2]
173+
174+
if "block_exp" == ntype node[2]
175+
block_exp = node[2]
176+
block_body = block_exp[2]
177+
178+
idx = #block_body
179+
node[2] = block_body[idx]
180+
block_body[idx] = node
181+
return build.group block_body
182+
183+
node
184+
161185
declare_glob: (node) =>
162186
names = extract_declarations @
163187

@@ -191,8 +215,7 @@ Statement = Transformer {
191215
}
192216

193217
when "comprehension", "tblcomprehension", "foreach", "for", "while"
194-
return build.assign_one first_name,
195-
Value.transformers[first_value[1]] @, first_value
218+
return build.assign_one first_name, Value\transform_once @, first_value
196219

197220
-- bubble cascading assigns
198221
transformed = if num_values == 1

moonscript/util.lua

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,13 @@ split = function(str, delim)
7777
return { }
7878
end
7979
str = str .. delim
80-
return (function()
81-
local _accum_0 = { }
82-
local _len_0 = 1
83-
for m in str:gmatch("(.-)" .. delim) do
84-
_accum_0[_len_0] = m
85-
_len_0 = _len_0 + 1
86-
end
87-
return _accum_0
88-
end)()
80+
local _accum_0 = { }
81+
local _len_0 = 1
82+
for m in str:gmatch("(.-)" .. delim) do
83+
_accum_0[_len_0] = m
84+
_len_0 = _len_0 + 1
85+
end
86+
return _accum_0
8987
end
9088
local dump
9189
dump = function(what)

spec/outputs/bubbling.lua

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@ f = function(...)
66
end
77
local dont_bubble
88
dont_bubble = function()
9-
return (function()
10-
local _accum_0 = { }
11-
local _len_0 = 1
12-
for x in (function(...)
13-
return print(...)
14-
end)("hello") do
15-
_accum_0[_len_0] = x
16-
_len_0 = _len_0 + 1
17-
end
18-
return _accum_0
19-
end)()
9+
local _accum_0 = { }
10+
local _len_0 = 1
11+
for x in (function(...)
12+
return print(...)
13+
end)("hello") do
14+
_accum_0[_len_0] = x
15+
_len_0 = _len_0 + 1
16+
end
17+
return _accum_0
2018
end
2119
local k
2220
do
@@ -44,21 +42,19 @@ do
4442
end
4543
local m
4644
m = function(...)
47-
return (function(...)
48-
local _accum_0 = { }
49-
local _len_0 = 1
50-
local _list_0 = {
51-
...
52-
}
53-
for _index_0 = 1, #_list_0 do
54-
local x = _list_0[_index_0]
55-
if f(...) > 4 then
56-
_accum_0[_len_0] = x
57-
_len_0 = _len_0 + 1
58-
end
45+
local _accum_0 = { }
46+
local _len_0 = 1
47+
local _list_0 = {
48+
...
49+
}
50+
for _index_0 = 1, #_list_0 do
51+
local x = _list_0[_index_0]
52+
if f(...) > 4 then
53+
_accum_0[_len_0] = x
54+
_len_0 = _len_0 + 1
5955
end
60-
return _accum_0
61-
end)(...)
56+
end
57+
return _accum_0
6258
end
6359
local x
6460
do

spec/outputs/lists.lua

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,13 @@ for _index_0 = a, _max_2 < 0 and #x + _max_2 or _max_2, c do
251251
end
252252
local normal
253253
normal = function(hello)
254-
return (function()
255-
local _accum_0 = { }
256-
local _len_0 = 1
257-
for x in yeah do
258-
_accum_0[_len_0] = x
259-
_len_0 = _len_0 + 1
260-
end
261-
return _accum_0
262-
end)()
254+
local _accum_0 = { }
255+
local _len_0 = 1
256+
for x in yeah do
257+
_accum_0[_len_0] = x
258+
_len_0 = _len_0 + 1
259+
end
260+
return _accum_0
263261
end
264262
local test = x(1, 2, 3, 4, 5)
265263
for _index_0 = 1, #test do
@@ -281,14 +279,12 @@ _ = function()
281279
end
282280
end
283281
return function()
284-
return (function()
285-
local _accum_0 = { }
286-
local _len_0 = 1
287-
for _index_0 = 1, #things do
288-
x = things[_index_0]
289-
_accum_0[_len_0] = x
290-
_len_0 = _len_0 + 1
291-
end
292-
return _accum_0
293-
end)()
282+
local _accum_0 = { }
283+
local _len_0 = 1
284+
for _index_0 = 1, #things do
285+
x = things[_index_0]
286+
_accum_0[_len_0] = x
287+
_len_0 = _len_0 + 1
288+
end
289+
return _accum_0
294290
end

0 commit comments

Comments
 (0)