From d2444409fef6362f5ef80cdd5fc8312ca17b4fcc Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 12 Jan 2013 13:29:38 -0800 Subject: [PATCH] remove filtering of nil values from loop expressions (use continue instead) #66 --- moonscript/transform.lua | 33 ++++++------------------ moonscript/transform.moon | 24 +++++++----------- test2.moon | 3 ++- tests/outputs/bubbling.lua | 30 ++++++---------------- tests/outputs/loops.lua | 52 ++++++++++++-------------------------- 5 files changed, 43 insertions(+), 99 deletions(-) diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 20425f3..2868498 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -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] - 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 @@ -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, diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 5705744..e2703c2 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -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] - 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 @@ -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) => diff --git a/test2.moon b/test2.moon index d2a6408..516c307 100644 --- a/test2.moon +++ b/test2.moon @@ -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 diff --git a/tests/outputs/bubbling.lua b/tests/outputs/bubbling.lua index f054c1c..651b0e2 100644 --- a/tests/outputs/bubbling.lua +++ b/tests/outputs/bubbling.lua @@ -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)() @@ -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)(...) @@ -106,11 +99,8 @@ 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)(...) @@ -118,14 +108,10 @@ 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)() \ No newline at end of file diff --git a/tests/outputs/loops.lua b/tests/outputs/loops.lua index 37c5fb6..87824e8 100644 --- a/tests/outputs/loops.lua +++ b/tests/outputs/loops.lua @@ -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)() @@ -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)() @@ -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)() @@ -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)() @@ -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)() @@ -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)() @@ -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)() @@ -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