Skip to content

Commit d244440

Browse files
committed
remove filtering of nil values from loop expressions (use continue instead) #66
1 parent 673e0cf commit d244440

File tree

5 files changed

+43
-99
lines changed

5 files changed

+43
-99
lines changed

moonscript/transform.lua

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ is_singular = function(body)
8888
if "group" == ntype(body) then
8989
return is_singular(body[2])
9090
else
91-
return true
91+
return body[1]
9292
end
9393
end
9494
local find_assigns
@@ -1243,17 +1243,12 @@ do
12431243
self.accum_name
12441244
})
12451245
end,
1246-
mutate_body = function(self, body, skip_nil)
1247-
if skip_nil == nil then
1248-
skip_nil = true
1249-
end
1246+
mutate_body = function(self, body)
1247+
local single_stm = is_singular(body)
12501248
local val
1251-
if not skip_nil and is_singular(body) then
1252-
do
1253-
local _with_0 = body[1]
1254-
body = { }
1255-
val = _with_0
1256-
end
1249+
if single_stm and types.is_value(single_stm) then
1250+
body = { }
1251+
val = single_stm
12571252
else
12581253
body = apply_to_last(body, function(n)
12591254
if types.is_value(n) then
@@ -1281,19 +1276,7 @@ do
12811276
1
12821277
}
12831278
}
1284-
if skip_nil then
1285-
table.insert(body, build["if"]({
1286-
cond = {
1287-
"exp",
1288-
self.value_name,
1289-
"!=",
1290-
"nil"
1291-
},
1292-
["then"] = update
1293-
}))
1294-
else
1295-
table.insert(body, build.group(update))
1296-
end
1279+
insert(body, build.group(update))
12971280
return body
12981281
end
12991282
}
@@ -1427,7 +1410,7 @@ local Value = Transformer({
14271410
node = self.transform.statement(node, function(exp)
14281411
return a:mutate_body({
14291412
exp
1430-
}, false)
1413+
})
14311414
end)
14321415
return a:wrap(node)
14331416
end,

moonscript/transform.moon

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ is_singular = (body) ->
4242
if "group" == ntype body
4343
is_singular body[2]
4444
else
45-
true
45+
body[1]
4646

4747
find_assigns = (body, out={}) ->
4848
for thing in *body
@@ -697,11 +697,12 @@ class Accumulator
697697
}
698698

699699
-- mutates the body of a loop construct to save last value into accumulator
700-
-- can optionally skip nil results
701-
mutate_body: (body, skip_nil=true) =>
702-
val = if not skip_nil and is_singular body
703-
with body[1]
704-
body = {}
700+
mutate_body: (body) =>
701+
-- shortcut to write simpler code if body is a single expression
702+
single_stm = is_singular body
703+
val = if single_stm and types.is_value single_stm
704+
body = {}
705+
single_stm
705706
else
706707
body = apply_to_last body, (n) ->
707708
if types.is_value n
@@ -719,14 +720,7 @@ class Accumulator
719720
{"update", @len_name, "+=", 1}
720721
}
721722

722-
if skip_nil
723-
table.insert body, build["if"] {
724-
cond: {"exp", @value_name, "!=", "nil"}
725-
then: update
726-
}
727-
else
728-
table.insert body, build.group update
729-
723+
insert body, build.group update
730724
body
731725

732726
default_accumulator = (node) =>
@@ -799,7 +793,7 @@ Value = Transformer {
799793
comprehension: (node) =>
800794
a = Accumulator!
801795
node = @transform.statement node, (exp) ->
802-
a\mutate_body {exp}, false
796+
a\mutate_body {exp}
803797
a\wrap node
804798

805799
tblcomprehension: (node) =>

test2.moon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ output_fname = (base) ->
8484

8585
describe "input tests", ->
8686
inputs = for file in lfs.dir options.in_dir
87-
file\match options.input_pattern
87+
with match = file\match options.input_pattern
88+
continue unless match
8889

8990
table.sort inputs
9091

tests/outputs/bubbling.lua

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,10 @@ local j = (function()
3333
local _accum_0 = { }
3434
local _len_0 = 1
3535
for i = 1, 10 do
36-
local _value_0
37-
_value_0 = function(...)
36+
_accum_0[_len_0] = function(...)
3837
return print(...)
3938
end
40-
if _value_0 ~= nil then
41-
_accum_0[_len_0] = _value_0
42-
_len_0 = _len_0 + 1
43-
end
39+
_len_0 = _len_0 + 1
4440
end
4541
return _accum_0
4642
end)()
@@ -70,11 +66,8 @@ local x = (function(...)
7066
}
7167
for _index_0 = 1, #_list_0 do
7268
local i = _list_0[_index_0]
73-
local _value_0 = i
74-
if _value_0 ~= nil then
75-
_accum_0[_len_0] = _value_0
76-
_len_0 = _len_0 + 1
77-
end
69+
_accum_0[_len_0] = i
70+
_len_0 = _len_0 + 1
7871
end
7972
return _accum_0
8073
end)(...)
@@ -106,26 +99,19 @@ local a = (function(...)
10699
local _accum_0 = { }
107100
local _len_0 = 1
108101
for i = 1, 10 do
109-
local _value_0 = ...
110-
if _value_0 ~= nil then
111-
_accum_0[_len_0] = _value_0
112-
_len_0 = _len_0 + 1
113-
end
102+
_accum_0[_len_0] = ...
103+
_len_0 = _len_0 + 1
114104
end
115105
return _accum_0
116106
end)(...)
117107
local b = (function()
118108
local _accum_0 = { }
119109
local _len_0 = 1
120110
for i = 1, 10 do
121-
local _value_0
122-
_value_0 = function()
111+
_accum_0[_len_0] = function()
123112
return print(...)
124113
end
125-
if _value_0 ~= nil then
126-
_accum_0[_len_0] = _value_0
127-
_len_0 = _len_0 + 1
128-
end
114+
_len_0 = _len_0 + 1
129115
end
130116
return _accum_0
131117
end)()

tests/outputs/loops.lua

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,10 @@ x = (function()
5454
local _list_1 = hello
5555
for _index_0 = 1, #_list_1 do
5656
local y = _list_1[_index_0]
57-
local _value_0
5857
if y % 2 == 0 then
59-
_value_0 = y
60-
end
61-
if _value_0 ~= nil then
62-
_accum_0[_len_0] = _value_0
63-
_len_0 = _len_0 + 1
58+
_accum_0[_len_0] = y
6459
end
60+
_len_0 = _len_0 + 1
6561
end
6662
return _accum_0
6763
end)()
@@ -76,11 +72,8 @@ local t = (function()
7672
local _accum_0 = { }
7773
local _len_0 = 1
7874
for i = 10, 20 do
79-
local _value_0 = i * 2
80-
if _value_0 ~= nil then
81-
_accum_0[_len_0] = _value_0
82-
_len_0 = _len_0 + 1
83-
end
75+
_accum_0[_len_0] = i * 2
76+
_len_0 = _len_0 + 1
8477
end
8578
return _accum_0
8679
end)()
@@ -91,10 +84,8 @@ local y = (function()
9184
for j = 3, 30, 8 do
9285
hmm = hmm + 1
9386
local _value_0 = j * hmm
94-
if _value_0 ~= nil then
95-
_accum_0[_len_0] = _value_0
96-
_len_0 = _len_0 + 1
97-
end
87+
_accum_0[_len_0] = _value_0
88+
_len_0 = _len_0 + 1
9889
end
9990
return _accum_0
10091
end)()
@@ -109,11 +100,8 @@ _ = function()
109100
local _accum_0 = { }
110101
local _len_0 = 1
111102
for k = 10, 40 do
112-
local _value_0 = "okay"
113-
if _value_0 ~= nil then
114-
_accum_0[_len_0] = _value_0
115-
_len_0 = _len_0 + 1
116-
end
103+
_accum_0[_len_0] = "okay"
104+
_len_0 = _len_0 + 1
117105
end
118106
return _accum_0
119107
end)()
@@ -136,10 +124,8 @@ x = (function()
136124
while i < 10 do
137125
local _value_0
138126
i = i + 1
139-
if _value_0 ~= nil then
140-
_accum_0[_len_0] = _value_0
141-
_len_0 = _len_0 + 1
142-
end
127+
_accum_0[_len_0] = _value_0
128+
_len_0 = _len_0 + 1
143129
end
144130
return _accum_0
145131
end)()
@@ -151,10 +137,8 @@ x = (function()
151137
local thing = _list_1[_index_0]
152138
local _value_0
153139
y = "hello"
154-
if _value_0 ~= nil then
155-
_accum_0[_len_0] = _value_0
156-
_len_0 = _len_0 + 1
157-
end
140+
_accum_0[_len_0] = _value_0
141+
_len_0 = _len_0 + 1
158142
end
159143
return _accum_0
160144
end)()
@@ -164,10 +148,8 @@ x = (function()
164148
for x = 1, 2 do
165149
local _value_0
166150
y = "hello"
167-
if _value_0 ~= nil then
168-
_accum_0[_len_0] = _value_0
169-
_len_0 = _len_0 + 1
170-
end
151+
_accum_0[_len_0] = _value_0
152+
_len_0 = _len_0 + 1
171153
end
172154
return _accum_0
173155
end)()
@@ -214,10 +196,8 @@ local list = (function()
214196
break
215197
end
216198
local _value_0 = x
217-
if _value_0 ~= nil then
218-
_accum_0[_len_0] = _value_0
219-
_len_0 = _len_0 + 1
220-
end
199+
_accum_0[_len_0] = _value_0
200+
_len_0 = _len_0 + 1
221201
_continue_0 = true
222202
until true
223203
if not _continue_0 then

0 commit comments

Comments
 (0)