Skip to content

Commit 9813071

Browse files
committed
implicit return done in transform
1 parent 096b407 commit 9813071

File tree

8 files changed

+97
-31
lines changed

8 files changed

+97
-31
lines changed

moonscript/compile/format.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ manual_return = Set{"foreach", "for", "while"}
1919

2020
default_return = (exp) ->
2121
t = ntype exp
22-
if t == "chain" and exp[2] == "return"
22+
if t == "chain" and exp[2] == "return" -- return is a first class citizen now
2323
-- extract the return
2424
items = {"explist"}
2525
insert items, v for v in *exp[3][2]

moonscript/compile/value.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ value_compile = {
240240
self_arg_values
241241
})
242242
end
243-
_with_0:ret_stms(block)
243+
_with_0:stms(block)
244244
if #args > #arg_names then
245245
arg_names = (function()
246246
local _accum_0 = { }

moonscript/compile/value.moon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ value_compile =
127127
self_arg_values = [arg[2] for arg in *self_args]
128128
\stm {"assign", self_args, self_arg_values} if #self_args > 0
129129

130-
\ret_stms block
130+
\stms block
131+
132+
-- inject more args if the block manipulated arguments
131133
if #args > #arg_names -- will only work for simple adjustments
132134
arg_names = for arg in *args
133135
arg[1]

moonscript/transform.lua

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ 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
812
NameProxy = (function()
913
local _parent_0 = nil
1014
local _base_0 = {
@@ -152,25 +156,35 @@ local constructor_name = "new"
152156
local Transformer
153157
Transformer = function(transformers)
154158
local seen_nodes = { }
155-
return function(n, ...)
156-
if seen_nodes[n] then
157-
return n
158-
end
159-
seen_nodes[n] = true
160-
while true do
161-
local transformer = transformers[ntype(n)]
162-
local res
163-
if transformer then
164-
res = transformer(n, ...) or n
165-
else
166-
res = n
167-
end
168-
if res == n then
159+
local tf = {
160+
transform = function(n, ...)
161+
if seen_nodes[n] then
169162
return n
170163
end
171-
n = res
164+
seen_nodes[n] = true
165+
while true do
166+
local transformer = transformers[ntype(n)]
167+
local res
168+
if transformer then
169+
res = transformer(n, ...) or n
170+
else
171+
res = n
172+
end
173+
if res == n then
174+
return n
175+
end
176+
n = res
177+
end
178+
end,
179+
can_transform = function(node)
180+
return transformers[ntype(node)] ~= nil
172181
end
173-
end
182+
}
183+
return setmetatable(tf, {
184+
__call = function(self, ...)
185+
return self.transform(...)
186+
end
187+
})
174188
end
175189
stm = Transformer({
176190
comprehension = function(node, action)
@@ -587,6 +601,21 @@ value = Transformer({
587601
end)
588602
return a:wrap(node)
589603
end,
604+
fndef = function(node)
605+
smart_node(node)
606+
node.body = apply_to_last(node.body, function(stm)
607+
local t = ntype(stm)
608+
if types.manual_return[t] or not is_value(stm) then
609+
return stm
610+
else
611+
return {
612+
"return",
613+
stm
614+
}
615+
end
616+
end)
617+
return node
618+
end,
590619
chain = function(node)
591620
local stub = node[#node]
592621
if type(stub) == "table" and stub[1] == "colon_stub" then

moonscript/transform.moon

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

1212
export stm, value, NameProxy, Run
1313

14+
-- TODO refactor
15+
is_value = (stm) ->
16+
moonscript.compile.Block\is_value(stm) or value.can_transform stm
17+
1418
class NameProxy
1519
new: (@prefix) =>
1620
self[1] = "temp_name"
@@ -81,17 +85,25 @@ constructor_name = "new"
8185
Transformer = (transformers) ->
8286
-- this is bad, instance it for compiler
8387
seen_nodes = {}
84-
(n, ...) ->
85-
return n if seen_nodes[n]
86-
seen_nodes[n] = true
87-
while true
88-
transformer = transformers[ntype n]
89-
res = if transformer
90-
transformer(n, ...) or n
91-
else
92-
n
93-
return n if res == n
94-
n = res
88+
tf = {
89+
transform: (n, ...) ->
90+
return n if seen_nodes[n]
91+
seen_nodes[n] = true
92+
while true
93+
transformer = transformers[ntype n]
94+
res = if transformer
95+
transformer(n, ...) or n
96+
else
97+
n
98+
return n if res == n
99+
n = res
100+
can_transform: (node) ->
101+
transformers[ntype node] != nil
102+
}
103+
104+
setmetatable tf, {
105+
__call: (...) => self.transform ...
106+
}
95107

96108
stm = Transformer {
97109
comprehension: (node, action) ->
@@ -327,6 +339,18 @@ value = Transformer {
327339
a\mutate_body {exp}, false
328340
a\wrap node
329341

342+
fndef: (node) ->
343+
smart_node node
344+
345+
node.body = apply_to_last node.body, (stm) ->
346+
t = ntype stm
347+
-- TODO okay this needs a refactor
348+
if types.manual_return[t] or not is_value stm
349+
stm
350+
else
351+
{"return", stm}
352+
353+
node
330354
-- pull out colon chain
331355
chain: (node) ->
332356
stub = node[#node]

moonscript/types.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ module("moonscript.types", package.seeall)
22
local util = require("moonscript.util")
33
local data = require("moonscript.data")
44
local insert = table.insert
5+
manual_return = data.Set({
6+
"foreach",
7+
"for",
8+
"while",
9+
"return"
10+
})
511
ntype = function(node)
612
if type(node) ~= "table" then
713
return "value"

moonscript/types.moon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ util = require "moonscript.util"
33
data = require "moonscript.data"
44

55
export ntype, smart_node, build
6-
export is_slice
6+
export is_slice, manual_return
7+
78
import insert from table
89

10+
manual_return = data.Set{"foreach", "for", "while", "return"}
911

1012
-- type of node as string
1113
ntype = (node) ->

todo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# TODO
22

3+
- don't reuse _, put a local on it, so we don't keep around trash
4+
5+
36
- or= and=
47

58
- move stuff out of format that should be in types

0 commit comments

Comments
 (0)