@@ -5,6 +5,10 @@ local data = require("moonscript.data")
55local reversed = util .reversed
66local ntype , build , smart_node , is_slice = types .ntype , types .build , types .smart_node , types .is_slice
77local 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
812NameProxy = (function ()
913 local _parent_0 = nil
1014 local _base_0 = {
@@ -152,25 +156,35 @@ local constructor_name = "new"
152156local Transformer
153157Transformer = 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+ })
174188end
175189stm = 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
0 commit comments