Skip to content

Commit 45ba49f

Browse files
committed
with moved to transformer
1 parent fd32503 commit 45ba49f

File tree

6 files changed

+41
-48
lines changed

6 files changed

+41
-48
lines changed

moonscript/compile/line.lua

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,6 @@ line_compile = {
305305
end
306306
return nil
307307
end,
308-
with = function(self, node, ret)
309-
local _, exp, block = unpack(node)
310-
do
311-
local _with_0 = self:block()
312-
local var = _with_0:init_free_var("with", exp)
313-
self:set("scope_var", var)
314-
_with_0:stms(block)
315-
if ret then
316-
_with_0:stm(ret(var))
317-
end
318-
return _with_0
319-
end
320-
end,
321308
run = function(self, code)
322309
code:call(self)
323310
return nil

moonscript/compile/line.moon

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,6 @@ line_compile =
167167
@declare names
168168
nil
169169

170-
with: (node, ret) =>
171-
_, exp, block = unpack node
172-
173-
with @block!
174-
var = \init_free_var "with", exp
175-
@set "scope_var", var
176-
\stms block
177-
\stm ret var if ret
178-
179170
run: (code) =>
180171
code\call self
181172
nil

moonscript/compile/value.lua

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,6 @@ value_compile = {
8989
local _, delim, inner, delim_end = unpack(node)
9090
return delim .. inner .. (delim_end or delim)
9191
end,
92-
with = function(self, node)
93-
do
94-
local _with_0 = self:block("(function()", "end)()")
95-
_with_0:stm(node, default_return)
96-
return _with_0
97-
end
98-
end,
99-
["if"] = function(self, node)
100-
do
101-
local _with_0 = self:block("(function()", "end)()")
102-
_with_0:stm(node, default_return)
103-
return _with_0
104-
end
105-
end,
10692
chain = function(self, node)
10793
local callee = node[2]
10894
if callee == -1 then

moonscript/compile/value.moon

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@ value_compile =
4444
_, delim, inner, delim_end = unpack node
4545
delim..inner..(delim_end or delim)
4646

47-
with: (node) =>
48-
with @block "(function()", "end)()"
49-
\stm node, default_return
50-
51-
if: (node) =>
52-
with @block "(function()", "end)()"
53-
\stm node, default_return
54-
5547
chain: (node) =>
5648
callee = node[2]
5749

moonscript/transform.lua

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,6 @@ Statement = Transformer({
224224
return current_stms[1]
225225
end,
226226
["if"] = function(node, ret)
227-
print("node:", node, "ret:", ret)
228-
print(util.dump(node))
229227
if ret then
230228
smart_node(node)
231229
node['then'] = apply_to_last(node['then'], ret)
@@ -237,6 +235,22 @@ Statement = Transformer({
237235
end
238236
return node
239237
end,
238+
with = function(node, ret)
239+
local _, exp, block = unpack(node)
240+
local scope_name = NameProxy("with")
241+
return build.group({
242+
build.assign_one(scope_name, exp),
243+
Run(function(self)
244+
return self:set("scope_var", scope_name)
245+
end),
246+
build.group(block),
247+
(function()
248+
if ret then
249+
return ret(scope_name)
250+
end
251+
end)()
252+
})
253+
end,
240254
foreach = function(node)
241255
smart_node(node)
242256
if ntype(node.iter) == "unpack" then
@@ -634,6 +648,16 @@ Value = Transformer({
634648
node.body = apply_to_last(node.body, implicitly_return)
635649
return node
636650
end,
651+
["if"] = function(node)
652+
return build.block_exp({
653+
node
654+
})
655+
end,
656+
with = function(node)
657+
return build.block_exp({
658+
node
659+
})
660+
end,
637661
chain = function(node)
638662
local stub = node[#node]
639663
if type(stub) == "table" and stub[1] == "colon_stub" then

moonscript/transform.moon

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ Statement = Transformer {
128128

129129
-- handle cascading return decorator
130130
if: (node, ret) ->
131-
print "node:", node, "ret:", ret
132-
print util.dump node
133131
if ret
134132
smart_node node
135133
-- mutate all the bodies
@@ -140,6 +138,17 @@ Statement = Transformer {
140138
case[body_idx] = apply_to_last case[body_idx], ret
141139
node
142140

141+
with: (node, ret) ->
142+
_, exp, block = unpack node
143+
scope_name = NameProxy "with"
144+
build.group {
145+
build.assign_one scope_name, exp
146+
Run => @set "scope_var", scope_name
147+
build.group block
148+
if ret
149+
ret scope_name
150+
}
151+
143152
foreach: (node) ->
144153
smart_node node
145154
if ntype(node.iter) == "unpack"
@@ -366,6 +375,10 @@ Value = Transformer {
366375
smart_node node
367376
node.body = apply_to_last node.body, implicitly_return
368377
node
378+
379+
if: (node) -> build.block_exp { node }
380+
with: (node) -> build.block_exp { node }
381+
369382
-- pull out colon chain
370383
chain: (node) ->
371384
stub = node[#node]

0 commit comments

Comments
 (0)