Skip to content

Commit 063b50a

Browse files
committed
assignment declaration is pulled out of line decorators #44
1 parent e1f31d9 commit 063b50a

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

moonscript/transform.lua

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ Statement = Transformer({
551551
end,
552552
decorated = function(self, node)
553553
local stm, dec = unpack(node, 2)
554+
local wrapped
554555
local _exp_0 = dec[1]
555556
if "if" == _exp_0 then
556557
local cond, fail = unpack(dec, 2)
@@ -562,7 +563,7 @@ Statement = Transformer({
562563
}
563564
}
564565
end
565-
return {
566+
wrapped = {
566567
"if",
567568
cond,
568569
{
@@ -571,22 +572,43 @@ Statement = Transformer({
571572
fail
572573
}
573574
elseif "unless" == _exp_0 then
574-
return {
575+
wrapped = {
575576
"unless",
576577
dec[2],
577578
{
578579
stm
579580
}
580581
}
581582
elseif "comprehension" == _exp_0 then
582-
return {
583+
wrapped = {
583584
"comprehension",
584585
stm,
585586
dec[2]
586587
}
587588
else
588-
return error("Unknown decorator " .. dec[1])
589+
wrapped = error("Unknown decorator " .. dec[1])
590+
end
591+
if ntype(stm) == "assign" then
592+
wrapped = build.group({
593+
build.declare({
594+
names = (function()
595+
local _accum_0 = { }
596+
local _len_0 = 0
597+
local _list_0 = stm[2]
598+
for _index_0 = 1, #_list_0 do
599+
local name = _list_0[_index_0]
600+
if type(name) == "string" then
601+
_len_0 = _len_0 + 1
602+
_accum_0[_len_0] = name
603+
end
604+
end
605+
return _accum_0
606+
end)()
607+
}),
608+
wrapped
609+
})
589610
end
611+
return wrapped
590612
end,
591613
unless = function(self, node)
592614
return {

moonscript/transform.moon

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ Statement = Transformer {
247247
decorated: (node) =>
248248
stm, dec = unpack node, 2
249249

250-
switch dec[1]
250+
wrapped = switch dec[1]
251251
when "if"
252252
cond, fail = unpack dec, 2
253253
fail = { "else", { fail } } if fail
@@ -259,6 +259,14 @@ Statement = Transformer {
259259
else
260260
error "Unknown decorator " .. dec[1]
261261

262+
if ntype(stm) == "assign"
263+
wrapped = build.group {
264+
build.declare names: [name for name in *stm[2] when type(name) == "string"]
265+
wrapped
266+
}
267+
268+
wrapped
269+
262270
unless: (node) =>
263271
{ "if", {"not", {"parens", node[2]}}, unpack node, 3 }
264272

tests/inputs/cond.moon

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ x = unless true
113113
x = unless true and false
114114
print "cool!"
115115

116-
x = unless false then print "cool!"
117-
x = unless false then print "cool!" else print "no way!"
116+
y = unless false then print "cool!"
117+
y = unless false then print "cool!" else print "no way!"
118118

119-
x = unless nil
119+
z = unless nil
120120
print "hello"
121121
else
122122
print "world"
@@ -141,4 +141,10 @@ print "hello" unless value
141141

142142
dddd = {1,2,3} unless value
143143

144+
----------------
145+
146+
a = 12
147+
a,c,b = "cool" if something
148+
149+
144150

tests/outputs/cond.lua

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,19 @@ end
176176
if not (true and false) then
177177
x = print("cool!")
178178
end
179+
local y
179180
if not (false) then
180-
x = print("cool!")
181+
y = print("cool!")
181182
end
182183
if not (false) then
183-
x = print("cool!")
184+
y = print("cool!")
184185
else
185-
x = print("no way!")
186+
y = print("no way!")
186187
end
187188
if not (nil) then
188-
x = print("hello")
189+
z = print("hello")
189190
else
190-
x = print("world")
191+
z = print("world")
191192
end
192193
print((function()
193194
if not (true) then
@@ -221,10 +222,16 @@ end)())
221222
if not (value) then
222223
print("hello")
223224
end
225+
local dddd
224226
if not (value) then
225-
local dddd = {
227+
dddd = {
226228
1,
227229
2,
228230
3
229231
}
232+
end
233+
local a = 12
234+
local c, b
235+
if something then
236+
a, c, b = "cool"
230237
end

0 commit comments

Comments
 (0)