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
return is_singular(body[2])
else
return true
return body[1]
end
end
local find_assigns
@ -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]
if single_stm and types.is_value(single_stm) then
body = { }
val = _with_0
end
val = single_stm
else
body = apply_to_last(body, function(n)
if types.is_value(n) then
@ -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
}
@ -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,

View File

@ -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
@ -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]
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
@ -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) =>
@ -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) =>

View File

@ -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

View File

@ -33,15 +33,11 @@ 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
end
return _accum_0
end)()
local m
@ -70,12 +66,9 @@ 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
_accum_0[_len_0] = i
_len_0 = _len_0 + 1
end
end
return _accum_0
end)(...)
local y = (function(...)
@ -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
_accum_0[_len_0] = ...
_len_0 = _len_0 + 1
end
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
end
return _accum_0
end)()

View File

@ -54,15 +54,11 @@ 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
_accum_0[_len_0] = y
end
if _value_0 ~= nil then
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
end
return _accum_0
end)()
x = function()
@ -76,12 +72,9 @@ 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
_accum_0[_len_0] = i * 2
_len_0 = _len_0 + 1
end
end
return _accum_0
end)()
local hmm = 0
@ -91,11 +84,9 @@ 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
end
return _accum_0
end)()
local _
@ -109,12 +100,9 @@ _ = 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
_accum_0[_len_0] = "okay"
_len_0 = _len_0 + 1
end
end
return _accum_0
end)()
end
@ -136,11 +124,9 @@ 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
end
return _accum_0
end)()
x = (function()
@ -151,11 +137,9 @@ 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
end
return _accum_0
end)()
x = (function()
@ -164,11 +148,9 @@ 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
end
return _accum_0
end)()
while true do
@ -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
_continue_0 = true
until true
if not _continue_0 then