Skip to content

Commit f3c7b11

Browse files
committed
cascading assigns done through transformer
1 parent 2219c44 commit f3c7b11

File tree

8 files changed

+108
-160
lines changed

8 files changed

+108
-160
lines changed

moonscript/compile.lua

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -379,47 +379,14 @@ Block_ = (function()
379379
end
380380
return nil
381381
end,
382-
ret_stms = function(self, stms, ret)
383-
if ret == nil then
384-
ret = error("missing return handler")
385-
end
386-
local last_exp_id = 0
387-
for i = #stms, 1, -1 do
388-
local stm = stms[i]
389-
if stm and util.moon.type(stm) ~= transform.Run then
390-
last_exp_id = i
391-
break
392-
end
393-
end
394-
for i, stm in ipairs(stms) do
395-
if i == last_exp_id then
396-
if cascading[ntype(stm)] then
397-
self:stm(stm, ret)
398-
elseif self:is_value(stm) then
399-
local line = ret(stms[i])
400-
if self:is_stm(line) then
401-
self:stm(line)
402-
else
403-
error("got a value from implicit return")
404-
end
405-
else
406-
self:stm(stm)
407-
end
408-
else
409-
self:stm(stm)
410-
end
411-
end
412-
return nil
413-
end,
414382
stms = function(self, stms, ret)
415383
if ret then
416-
self:ret_stms(stms, ret)
417-
else
418-
local _list_0 = stms
419-
for _index_0 = 1, #_list_0 do
420-
local stm = _list_0[_index_0]
421-
self:stm(stm)
422-
end
384+
error("deprecated stms call, use transformer")
385+
end
386+
local _list_0 = stms
387+
for _index_0 = 1, #_list_0 do
388+
local stm = _list_0[_index_0]
389+
self:stm(stm)
423390
end
424391
return nil
425392
end

moonscript/compile.moon

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -258,38 +258,9 @@ class Block_
258258
@add out if out
259259
nil
260260

261-
ret_stms: (stms, ret=error"missing return handler") =>
262-
-- find last exp for explicit return
263-
last_exp_id = 0
264-
for i = #stms, 1, -1
265-
stm = stms[i]
266-
if stm and util.moon.type(stm) != transform.Run
267-
last_exp_id = i
268-
break
269-
270-
for i, stm in ipairs stms
271-
if i == last_exp_id
272-
if cascading[ntype(stm)]
273-
@stm stm, ret
274-
elseif @is_value stm
275-
line = ret stms[i]
276-
if @is_stm line
277-
@stm line
278-
else
279-
error "got a value from implicit return"
280-
else
281-
-- nothing we can do with a statement except show it
282-
@stm stm
283-
else
284-
@stm stm
285-
286-
nil
287-
288261
stms: (stms, ret) =>
289-
if ret
290-
@ret_stms stms, ret
291-
else
292-
@stm stm for stm in *stms
262+
error "deprecated stms call, use transformer" if ret
263+
@stm stm for stm in *stms
293264
nil
294265

295266
class RootBlock extends Block_

moonscript/compile/format.lua

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ user_error = function(...)
1919
...
2020
})
2121
end
22-
local manual_return = Set({
23-
"foreach",
24-
"for",
25-
"while"
26-
})
27-
cascading = Set({
28-
"if",
29-
"with"
30-
})
3122
moonlib = {
3223
bind = function(tbl, name)
3324
return concat({

moonscript/compile/format.moon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ indent_char = " "
1515
user_error = (...) ->
1616
error {"user-error", ...}
1717

18-
manual_return = Set{"foreach", "for", "while"}
19-
cascading = Set{ "if", "with" }
18+
-- manual_return = Set{"foreach", "for", "while"}
19+
-- cascading = Set{ "if", "with" }
2020

2121
-- TODO get RID OF THIAS
2222
moonlib =

moonscript/compile/line.lua

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -39,65 +39,47 @@ line_compile = {
3939
local _, names, values = unpack(node)
4040
local undeclared = self:declare(names)
4141
local declare = "local " .. concat(undeclared, ", ")
42-
if #values == 1 and self:is_stm(values[1]) and cascading[ntype(values[1])] then
43-
local stm = values[1]
44-
if #undeclared > 0 then
45-
self:add(declare)
42+
local has_fndef = false
43+
local i = 1
44+
while i <= #values do
45+
if ntype(values[i]) == "fndef" then
46+
has_fndef = true
4647
end
47-
local decorate
48-
decorate = function(value)
49-
return {
50-
"assign",
51-
names,
52-
{
53-
value
54-
}
55-
}
56-
end
57-
return self:stm(stm, decorate)
58-
else
59-
local has_fndef = false
60-
local i = 1
61-
while i <= #values do
62-
if ntype(values[i]) == "fndef" then
63-
has_fndef = true
64-
end
65-
i = i + 1
66-
end
67-
do
68-
local _with_0 = self:line()
69-
if #undeclared == #names and not has_fndef then
70-
_with_0:append(declare)
71-
else
72-
if #undeclared > 0 then
73-
self:add(declare)
74-
end
75-
_with_0:append_list((function()
76-
local _accum_0 = { }
77-
local _len_0 = 0
78-
local _list_0 = names
79-
for _index_0 = 1, #_list_0 do
80-
local name = _list_0[_index_0]
81-
_len_0 = _len_0 + 1
82-
_accum_0[_len_0] = self:value(name)
83-
end
84-
return _accum_0
85-
end)(), ", ")
48+
i = i + 1
49+
end
50+
do
51+
local _with_0 = self:line()
52+
if #undeclared == #names and not has_fndef then
53+
_with_0:append(declare)
54+
else
55+
if #undeclared > 0 then
56+
self:add(declare)
8657
end
87-
_with_0:append(" = ")
8858
_with_0:append_list((function()
8959
local _accum_0 = { }
9060
local _len_0 = 0
91-
local _list_0 = values
61+
local _list_0 = names
9262
for _index_0 = 1, #_list_0 do
93-
local v = _list_0[_index_0]
63+
local name = _list_0[_index_0]
9464
_len_0 = _len_0 + 1
95-
_accum_0[_len_0] = self:value(v)
65+
_accum_0[_len_0] = self:value(name)
9666
end
9767
return _accum_0
9868
end)(), ", ")
99-
return _with_0
10069
end
70+
_with_0:append(" = ")
71+
_with_0:append_list((function()
72+
local _accum_0 = { }
73+
local _len_0 = 0
74+
local _list_0 = values
75+
for _index_0 = 1, #_list_0 do
76+
local v = _list_0[_index_0]
77+
_len_0 = _len_0 + 1
78+
_accum_0[_len_0] = self:value(v)
79+
end
80+
return _accum_0
81+
end)(), ", ")
82+
return _with_0
10183
end
10284
end,
10385
update = function(self, node)
@@ -196,12 +178,12 @@ line_compile = {
196178
return _with_0
197179
end
198180
end,
199-
["if"] = function(self, node, ret)
181+
["if"] = function(self, node)
200182
local cond, block = node[2], node[3]
201183
local root
202184
do
203185
local _with_0 = self:block(self:line("if ", self:value(cond), " then"))
204-
_with_0:stms(block, ret)
186+
_with_0:stms(block)
205187
root = _with_0
206188
end
207189
local current = root
@@ -216,7 +198,7 @@ line_compile = {
216198
i = i + 1
217199
next = self:block(self:line("elseif ", self:value(clause[2]), " then"))
218200
end
219-
next:stms(clause[i], ret)
201+
next:stms(clause[i])
220202
current.next = next
221203
current = next
222204
end

moonscript/compile/line.moon

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,22 @@ line_compile =
2929
undeclared = @declare names
3030
declare = "local "..concat(undeclared, ", ")
3131

32-
-- todo: tree transformation
33-
if #values == 1 and @is_stm(values[1]) and cascading[ntype(values[1])]
34-
stm = values[1]
35-
@add declare if #undeclared > 0
36-
decorate = (value) ->
37-
{"assign", names, {value}}
38-
39-
@stm stm, decorate
40-
else
41-
has_fndef = false
42-
i = 1
43-
while i <= #values
44-
if ntype(values[i]) == "fndef"
45-
has_fndef = true
46-
i = i +1
47-
48-
with @line!
49-
if #undeclared == #names and not has_fndef
50-
\append declare
51-
else
52-
@add declare if #undeclared > 0
53-
\append_list [@value name for name in *names], ", "
54-
55-
\append " = "
56-
\append_list [@value v for v in *values], ", "
32+
has_fndef = false
33+
i = 1
34+
while i <= #values
35+
if ntype(values[i]) == "fndef"
36+
has_fndef = true
37+
i = i +1
38+
39+
with @line!
40+
if #undeclared == #names and not has_fndef
41+
\append declare
42+
else
43+
@add declare if #undeclared > 0
44+
\append_list [@value name for name in *names], ", "
45+
46+
\append " = "
47+
\append_list [@value v for v in *values], ", "
5748

5849
update: (node) =>
5950
_, name, op, exp = unpack node
@@ -100,10 +91,10 @@ line_compile =
10091
source = \init_free_var "table", source
10192
\stm {"assign", {name}, {get_value name}} for name in *final_names
10293

103-
if: (node, ret) =>
94+
if: (node) =>
10495
cond, block = node[2], node[3]
10596
root = with @block @line "if ", @value(cond), " then"
106-
\stms block, ret
97+
\stms block
10798

10899
current = root
109100
add_clause = (clause)->
@@ -115,7 +106,7 @@ line_compile =
115106
i += 1
116107
@block @line "elseif ", @value(clause[2]), " then"
117108

118-
next\stms clause[i], ret
109+
next\stms clause[i]
119110

120111
current.next = next
121112
current = next

moonscript/transform.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,34 @@ Transformer = function(transformers)
187187
})
188188
end
189189
Statement = Transformer({
190+
assign = function(node)
191+
local _, names, values = unpack(node)
192+
if #values == 1 and types.cascading[ntype(values[1])] then
193+
values[1] = Statement(values[1], function(stm)
194+
local t = ntype(stm)
195+
if is_value(stm) then
196+
return {
197+
"assign",
198+
names,
199+
{
200+
stm
201+
}
202+
}
203+
else
204+
return stm
205+
end
206+
end)
207+
return build.group({
208+
{
209+
"declare",
210+
names
211+
},
212+
values[1]
213+
})
214+
else
215+
return node
216+
end
217+
end,
190218
comprehension = function(node, action)
191219
local _, exp, clauses = unpack(node)
192220
action = action or function(exp)

moonscript/transform.moon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,24 @@ Transformer = (transformers) ->
106106
}
107107

108108
Statement = Transformer {
109+
assign: (node) ->
110+
_, names, values = unpack node
111+
-- bubble cascading assigns
112+
if #values == 1 and types.cascading[ntype values[1]]
113+
values[1] = Statement values[1], (stm) ->
114+
t = ntype stm
115+
if is_value stm
116+
{"assign", names, {stm}}
117+
else
118+
stm
119+
120+
build.group {
121+
{"declare", names}
122+
values[1]
123+
}
124+
else
125+
node
126+
109127
comprehension: (node, action) ->
110128
_, exp, clauses = unpack node
111129

0 commit comments

Comments
 (0)