Skip to content

Commit

Permalink
remove filtering of nil values from loop expressions (use continue in…
Browse files Browse the repository at this point in the history
…stead) #66
  • Loading branch information
leafo committed Jan 12, 2013
1 parent 673e0cf commit d244440
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 99 deletions.
33 changes: 8 additions & 25 deletions moonscript/transform.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ is_singular = function(body)
if "group" == ntype(body) then
return is_singular(body[2])
else
return true
return body[1]
end
end
local find_assigns
Expand Down Expand Up @@ -1243,17 +1243,12 @@ do
self.accum_name
})
end,
mutate_body = function(self, body, skip_nil)
if skip_nil == nil then
skip_nil = true
end
mutate_body = function(self, body)
local single_stm = is_singular(body)
local val
if not skip_nil and is_singular(body) then
do
local _with_0 = body[1]
body = { }
val = _with_0
end
if single_stm and types.is_value(single_stm) then
body = { }
val = single_stm
else
body = apply_to_last(body, function(n)
if types.is_value(n) then
Expand Down Expand Up @@ -1281,19 +1276,7 @@ do
1
}
}
if skip_nil then
table.insert(body, build["if"]({
cond = {
"exp",
self.value_name,
"!=",
"nil"
},
["then"] = update
}))
else
table.insert(body, build.group(update))
end
insert(body, build.group(update))
return body
end
}
Expand Down Expand Up @@ -1427,7 +1410,7 @@ local Value = Transformer({
node = self.transform.statement(node, function(exp)
return a:mutate_body({
exp
}, false)
})
end)
return a:wrap(node)
end,
Expand Down
24 changes: 9 additions & 15 deletions moonscript/transform.moon
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ is_singular = (body) ->
if "group" == ntype body
is_singular body[2]
else
true
body[1]

find_assigns = (body, out={}) ->
for thing in *body
Expand Down Expand Up @@ -697,11 +697,12 @@ class Accumulator
}

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

if skip_nil
table.insert body, build["if"] {
cond: {"exp", @value_name, "!=", "nil"}
then: update
}
else
table.insert body, build.group update

insert body, build.group update
body

default_accumulator = (node) =>
Expand Down Expand Up @@ -799,7 +793,7 @@ Value = Transformer {
comprehension: (node) =>
a = Accumulator!
node = @transform.statement node, (exp) ->
a\mutate_body {exp}, false
a\mutate_body {exp}
a\wrap node

tblcomprehension: (node) =>
Expand Down
3 changes: 2 additions & 1 deletion test2.moon
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ output_fname = (base) ->

describe "input tests", ->
inputs = for file in lfs.dir options.in_dir
file\match options.input_pattern
with match = file\match options.input_pattern
continue unless match

table.sort inputs

Expand Down
30 changes: 8 additions & 22 deletions tests/outputs/bubbling.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ local j = (function()
local _accum_0 = { }
local _len_0 = 1
for i = 1, 10 do
local _value_0
_value_0 = function(...)
_accum_0[_len_0] = function(...)
return print(...)
end
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
_len_0 = _len_0 + 1
end
return _accum_0
end)()
Expand Down Expand Up @@ -70,11 +66,8 @@ local x = (function(...)
}
for _index_0 = 1, #_list_0 do
local i = _list_0[_index_0]
local _value_0 = i
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
_accum_0[_len_0] = i
_len_0 = _len_0 + 1
end
return _accum_0
end)(...)
Expand Down Expand Up @@ -106,26 +99,19 @@ local a = (function(...)
local _accum_0 = { }
local _len_0 = 1
for i = 1, 10 do
local _value_0 = ...
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
_accum_0[_len_0] = ...
_len_0 = _len_0 + 1
end
return _accum_0
end)(...)
local b = (function()
local _accum_0 = { }
local _len_0 = 1
for i = 1, 10 do
local _value_0
_value_0 = function()
_accum_0[_len_0] = function()
return print(...)
end
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
_len_0 = _len_0 + 1
end
return _accum_0
end)()
52 changes: 16 additions & 36 deletions tests/outputs/loops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,10 @@ x = (function()
local _list_1 = hello
for _index_0 = 1, #_list_1 do
local y = _list_1[_index_0]
local _value_0
if y % 2 == 0 then
_value_0 = y
end
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
_accum_0[_len_0] = y
end
_len_0 = _len_0 + 1
end
return _accum_0
end)()
Expand All @@ -76,11 +72,8 @@ local t = (function()
local _accum_0 = { }
local _len_0 = 1
for i = 10, 20 do
local _value_0 = i * 2
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
_accum_0[_len_0] = i * 2
_len_0 = _len_0 + 1
end
return _accum_0
end)()
Expand All @@ -91,10 +84,8 @@ local y = (function()
for j = 3, 30, 8 do
hmm = hmm + 1
local _value_0 = j * hmm
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
return _accum_0
end)()
Expand All @@ -109,11 +100,8 @@ _ = function()
local _accum_0 = { }
local _len_0 = 1
for k = 10, 40 do
local _value_0 = "okay"
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
_accum_0[_len_0] = "okay"
_len_0 = _len_0 + 1
end
return _accum_0
end)()
Expand All @@ -136,10 +124,8 @@ x = (function()
while i < 10 do
local _value_0
i = i + 1
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
return _accum_0
end)()
Expand All @@ -151,10 +137,8 @@ x = (function()
local thing = _list_1[_index_0]
local _value_0
y = "hello"
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
return _accum_0
end)()
Expand All @@ -164,10 +148,8 @@ x = (function()
for x = 1, 2 do
local _value_0
y = "hello"
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
return _accum_0
end)()
Expand Down Expand Up @@ -214,10 +196,8 @@ local list = (function()
break
end
local _value_0 = x
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
_continue_0 = true
until true
if not _continue_0 then
Expand Down

0 comments on commit d244440

Please sign in to comment.