remove filtering of nil values from loop expressions (use continue instead) #66

This commit is contained in:
leaf corcoran 2013-01-12 13:29:38 -08:00
parent 673e0cf61a
commit d2444409fe
5 changed files with 43 additions and 99 deletions

View File

@ -88,7 +88,7 @@ is_singular = function(body)
if "group" == ntype(body) then if "group" == ntype(body) then
return is_singular(body[2]) return is_singular(body[2])
else else
return true return body[1]
end end
end end
local find_assigns local find_assigns
@ -1243,17 +1243,12 @@ do
self.accum_name self.accum_name
}) })
end, end,
mutate_body = function(self, body, skip_nil) mutate_body = function(self, body)
if skip_nil == nil then local single_stm = is_singular(body)
skip_nil = true
end
local val local val
if not skip_nil and is_singular(body) then if single_stm and types.is_value(single_stm) then
do
local _with_0 = body[1]
body = { } body = { }
val = _with_0 val = single_stm
end
else else
body = apply_to_last(body, function(n) body = apply_to_last(body, function(n)
if types.is_value(n) then if types.is_value(n) then
@ -1281,19 +1276,7 @@ do
1 1
} }
} }
if skip_nil then insert(body, build.group(update))
table.insert(body, build["if"]({
cond = {
"exp",
self.value_name,
"!=",
"nil"
},
["then"] = update
}))
else
table.insert(body, build.group(update))
end
return body return body
end end
} }
@ -1427,7 +1410,7 @@ local Value = Transformer({
node = self.transform.statement(node, function(exp) node = self.transform.statement(node, function(exp)
return a:mutate_body({ return a:mutate_body({
exp exp
}, false) })
end) end)
return a:wrap(node) return a:wrap(node)
end, end,

View File

@ -42,7 +42,7 @@ is_singular = (body) ->
if "group" == ntype body if "group" == ntype body
is_singular body[2] is_singular body[2]
else else
true body[1]
find_assigns = (body, out={}) -> find_assigns = (body, out={}) ->
for thing in *body for thing in *body
@ -697,11 +697,12 @@ class Accumulator
} }
-- mutates the body of a loop construct to save last value into accumulator -- mutates the body of a loop construct to save last value into accumulator
-- can optionally skip nil results mutate_body: (body) =>
mutate_body: (body, skip_nil=true) => -- shortcut to write simpler code if body is a single expression
val = if not skip_nil and is_singular body single_stm = is_singular body
with body[1] val = if single_stm and types.is_value single_stm
body = {} body = {}
single_stm
else else
body = apply_to_last body, (n) -> body = apply_to_last body, (n) ->
if types.is_value n if types.is_value n
@ -719,14 +720,7 @@ class Accumulator
{"update", @len_name, "+=", 1} {"update", @len_name, "+=", 1}
} }
if skip_nil insert body, build.group update
table.insert body, build["if"] {
cond: {"exp", @value_name, "!=", "nil"}
then: update
}
else
table.insert body, build.group update
body body
default_accumulator = (node) => default_accumulator = (node) =>
@ -799,7 +793,7 @@ Value = Transformer {
comprehension: (node) => comprehension: (node) =>
a = Accumulator! a = Accumulator!
node = @transform.statement node, (exp) -> node = @transform.statement node, (exp) ->
a\mutate_body {exp}, false a\mutate_body {exp}
a\wrap node a\wrap node
tblcomprehension: (node) => tblcomprehension: (node) =>

View File

@ -84,7 +84,8 @@ output_fname = (base) ->
describe "input tests", -> describe "input tests", ->
inputs = for file in lfs.dir options.in_dir 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 table.sort inputs

View File

@ -33,15 +33,11 @@ local j = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 1 local _len_0 = 1
for i = 1, 10 do for i = 1, 10 do
local _value_0 _accum_0[_len_0] = function(...)
_value_0 = function(...)
return print(...) return print(...)
end end
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1 _len_0 = _len_0 + 1
end end
end
return _accum_0 return _accum_0
end)() end)()
local m local m
@ -70,12 +66,9 @@ local x = (function(...)
} }
for _index_0 = 1, #_list_0 do for _index_0 = 1, #_list_0 do
local i = _list_0[_index_0] local i = _list_0[_index_0]
local _value_0 = i _accum_0[_len_0] = i
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1 _len_0 = _len_0 + 1
end end
end
return _accum_0 return _accum_0
end)(...) end)(...)
local y = (function(...) local y = (function(...)
@ -106,26 +99,19 @@ local a = (function(...)
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 1 local _len_0 = 1
for i = 1, 10 do for i = 1, 10 do
local _value_0 = ... _accum_0[_len_0] = ...
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1 _len_0 = _len_0 + 1
end end
end
return _accum_0 return _accum_0
end)(...) end)(...)
local b = (function() local b = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 1 local _len_0 = 1
for i = 1, 10 do for i = 1, 10 do
local _value_0 _accum_0[_len_0] = function()
_value_0 = function()
return print(...) return print(...)
end end
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1 _len_0 = _len_0 + 1
end end
end
return _accum_0 return _accum_0
end)() end)()

View File

@ -54,15 +54,11 @@ x = (function()
local _list_1 = hello local _list_1 = hello
for _index_0 = 1, #_list_1 do for _index_0 = 1, #_list_1 do
local y = _list_1[_index_0] local y = _list_1[_index_0]
local _value_0
if y % 2 == 0 then if y % 2 == 0 then
_value_0 = y _accum_0[_len_0] = y
end end
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1 _len_0 = _len_0 + 1
end end
end
return _accum_0 return _accum_0
end)() end)()
x = function() x = function()
@ -76,12 +72,9 @@ local t = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 1 local _len_0 = 1
for i = 10, 20 do for i = 10, 20 do
local _value_0 = i * 2 _accum_0[_len_0] = i * 2
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1 _len_0 = _len_0 + 1
end end
end
return _accum_0 return _accum_0
end)() end)()
local hmm = 0 local hmm = 0
@ -91,11 +84,9 @@ local y = (function()
for j = 3, 30, 8 do for j = 3, 30, 8 do
hmm = hmm + 1 hmm = hmm + 1
local _value_0 = j * hmm local _value_0 = j * hmm
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0 _accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1 _len_0 = _len_0 + 1
end end
end
return _accum_0 return _accum_0
end)() end)()
local _ local _
@ -109,12 +100,9 @@ _ = function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 1 local _len_0 = 1
for k = 10, 40 do for k = 10, 40 do
local _value_0 = "okay" _accum_0[_len_0] = "okay"
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1 _len_0 = _len_0 + 1
end end
end
return _accum_0 return _accum_0
end)() end)()
end end
@ -136,11 +124,9 @@ x = (function()
while i < 10 do while i < 10 do
local _value_0 local _value_0
i = i + 1 i = i + 1
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0 _accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1 _len_0 = _len_0 + 1
end end
end
return _accum_0 return _accum_0
end)() end)()
x = (function() x = (function()
@ -151,11 +137,9 @@ x = (function()
local thing = _list_1[_index_0] local thing = _list_1[_index_0]
local _value_0 local _value_0
y = "hello" y = "hello"
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0 _accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1 _len_0 = _len_0 + 1
end end
end
return _accum_0 return _accum_0
end)() end)()
x = (function() x = (function()
@ -164,11 +148,9 @@ x = (function()
for x = 1, 2 do for x = 1, 2 do
local _value_0 local _value_0
y = "hello" y = "hello"
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0 _accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1 _len_0 = _len_0 + 1
end end
end
return _accum_0 return _accum_0
end)() end)()
while true do while true do
@ -214,10 +196,8 @@ local list = (function()
break break
end end
local _value_0 = x local _value_0 = x
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0 _accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1 _len_0 = _len_0 + 1
end
_continue_0 = true _continue_0 = true
until true until true
if not _continue_0 then if not _continue_0 then