avoid creating function for basic list comprehension assign

This commit is contained in:
leaf corcoran 2013-06-10 19:57:01 -07:00
parent dc9d5507a1
commit f8c2e0b3ab
5 changed files with 223 additions and 252 deletions

View File

@ -13,8 +13,7 @@ local destructure = require("moonscript.transform.destructure")
local NOOP = { local NOOP = {
"noop" "noop"
} }
local implicitly_return local Run, apply_to_last, is_singular, extract_declarations, expand_elseif_assign, constructor_name, with_continue_listener, Transformer, construct_comprehension, Statement, Accumulator, default_accumulator, implicitly_return, Value
local Run
do do
local _parent_0 = nil local _parent_0 = nil
local _base_0 = { local _base_0 = {
@ -55,7 +54,6 @@ do
end end
Run = _class_0 Run = _class_0
end end
local apply_to_last
apply_to_last = function(stms, fn) apply_to_last = function(stms, fn)
local last_exp_id = 0 local last_exp_id = 0
for i = #stms, 1, -1 do for i = #stms, 1, -1 do
@ -83,7 +81,6 @@ apply_to_last = function(stms, fn)
return _accum_0 return _accum_0
end)() end)()
end end
local is_singular
is_singular = function(body) is_singular = function(body)
if #body ~= 1 then if #body ~= 1 then
return false return false
@ -94,7 +91,6 @@ is_singular = function(body)
return body[1] return body[1]
end end
end end
local extract_declarations
extract_declarations = function(self, body, start, out) extract_declarations = function(self, body, start, out)
if body == nil then if body == nil then
body = self.current_stms body = self.current_stms
@ -135,7 +131,6 @@ extract_declarations = function(self, body, start, out)
end end
return out return out
end end
local expand_elseif_assign
expand_elseif_assign = function(ifstm) expand_elseif_assign = function(ifstm)
for i = 4, #ifstm do for i = 4, #ifstm do
local case = ifstm[i] local case = ifstm[i]
@ -159,8 +154,7 @@ expand_elseif_assign = function(ifstm)
end end
return ifstm return ifstm
end end
local constructor_name = "new" constructor_name = "new"
local with_continue_listener
with_continue_listener = function(body) with_continue_listener = function(body)
local continue_name = nil local continue_name = nil
return { return {
@ -223,7 +217,6 @@ with_continue_listener = function(body)
end) end)
} }
end end
local Transformer
do do
local _parent_0 = nil local _parent_0 = nil
local _base_0 = { local _base_0 = {
@ -294,7 +287,6 @@ do
end end
Transformer = _class_0 Transformer = _class_0
end end
local construct_comprehension
construct_comprehension = function(inner, clauses) construct_comprehension = function(inner, clauses)
local current_stms = inner local current_stms = inner
for _, clause in reversed(clauses) do for _, clause in reversed(clauses) do
@ -337,7 +329,7 @@ construct_comprehension = function(inner, clauses)
end end
return current_stms[1] return current_stms[1]
end end
local Statement = Transformer({ Statement = Transformer({
transform = function(self, tuple) transform = function(self, tuple)
local _, node, fn local _, node, fn
_, node, fn = tuple[1], tuple[2], tuple[3] _, node, fn = tuple[1], tuple[2], tuple[3]
@ -379,8 +371,24 @@ local Statement = Transformer({
end, end,
assign = function(self, node) assign = function(self, node)
local names, values = unpack(node, 2) local names, values = unpack(node, 2)
local num_values = #values
local num_names = #values
if num_names == 1 and num_values == 1 then
local first_value = values[1]
local _exp_0 = ntype(first_value)
if "comprehension" == _exp_0 then
local first_name = names[1]
local a = Accumulator(first_name)
node = self.transform.statement(first_value, function(exp)
return a:mutate_body({
exp
})
end)
return a:wrap(node, "group")
end
end
local transformed local transformed
if #values == 1 then if num_values == 1 then
local value = values[1] local value = values[1]
local t = ntype(value) local t = ntype(value)
if t == "decorated" then if t == "decorated" then
@ -1254,7 +1262,6 @@ local Statement = Transformer({
return value return value
end end
}) })
local Accumulator
do do
local _parent_0 = nil local _parent_0 = nil
local _base_0 = { local _base_0 = {
@ -1268,12 +1275,15 @@ do
node[index] = self:mutate_body(node[index]) node[index] = self:mutate_body(node[index])
return self:wrap(node) return self:wrap(node)
end, end,
wrap = function(self, node) wrap = function(self, node, group_type)
return build.block_exp({ if group_type == nil then
group_type = "block_exp"
end
return build[group_type]({
build.assign_one(self.accum_name, build.table()), build.assign_one(self.accum_name, build.table()),
build.assign_one(self.len_name, 1), build.assign_one(self.len_name, 1),
node, node,
self.accum_name group_type == "block_exp" and self.accum_name or NOOP
}) })
end, end,
mutate_body = function(self, body) mutate_body = function(self, body)
@ -1301,7 +1311,7 @@ do
val = self.value_name val = self.value_name
end end
local update = { local update = {
build.assign_one(self.accum_name:index(self.len_name), val), build.assign_one(NameProxy.index(self.accum_name, self.len_name), val),
{ {
"update", "update",
self.len_name, self.len_name,
@ -1318,8 +1328,8 @@ do
setmetatable(_base_0, _parent_0.__base) setmetatable(_base_0, _parent_0.__base)
end end
local _class_0 = setmetatable({ local _class_0 = setmetatable({
__init = function(self) __init = function(self, accum_name)
self.accum_name = NameProxy("accum") self.accum_name = accum_name or NameProxy("accum")
self.value_name = NameProxy("value") self.value_name = NameProxy("value")
self.len_name = NameProxy("len") self.len_name = NameProxy("len")
end, end,
@ -1347,7 +1357,6 @@ do
end end
Accumulator = _class_0 Accumulator = _class_0
end end
local default_accumulator
default_accumulator = function(self, node) default_accumulator = function(self, node)
return Accumulator():convert(node) return Accumulator():convert(node)
end end
@ -1382,7 +1391,7 @@ implicitly_return = function(scope)
end end
return fn return fn
end end
local Value = Transformer({ Value = Transformer({
["for"] = default_accumulator, ["for"] = default_accumulator,
["while"] = default_accumulator, ["while"] = default_accumulator,
foreach = default_accumulator, foreach = default_accumulator,

View File

@ -11,7 +11,7 @@ import NameProxy, LocalName from require "moonscript.transform.names"
destructure = require "moonscript.transform.destructure" destructure = require "moonscript.transform.destructure"
NOOP = {"noop"} NOOP = {"noop"}
local implicitly_return local *
class Run class Run
new: (@fn) => new: (@fn) =>
@ -171,8 +171,23 @@ Statement = Transformer {
assign: (node) => assign: (node) =>
names, values = unpack node, 2 names, values = unpack node, 2
num_values = #values
num_names = #values
if num_names == 1 and num_values == 1
first_value = values[1]
switch ntype first_value
when "comprehension"
first_name = names[1]
a = Accumulator first_name
node = @transform.statement first_value, (exp) ->
a\mutate_body { exp }
return a\wrap node, "group"
-- bubble cascading assigns -- bubble cascading assigns
transformed = if #values == 1 transformed = if num_values == 1
value = values[1] value = values[1]
t = ntype value t = ntype value
@ -194,11 +209,9 @@ Statement = Transformer {
node = transformed or node node = transformed or node
if destructure.has_destructure names if destructure.has_destructure names
return destructure.split_assign @, node return destructure.split_assign @, node
-- print util.dump node
node node
continue: (node) => continue: (node) =>
@ -697,8 +710,8 @@ Statement = Transformer {
class Accumulator class Accumulator
body_idx: { for: 4, while: 3, foreach: 4 } body_idx: { for: 4, while: 3, foreach: 4 }
new: => new: (accum_name) =>
@accum_name = NameProxy "accum" @accum_name = accum_name or NameProxy "accum"
@value_name = NameProxy "value" @value_name = NameProxy "value"
@len_name = NameProxy "len" @len_name = NameProxy "len"
@ -709,12 +722,12 @@ class Accumulator
@wrap node @wrap node
-- wrap the node into a block_exp -- wrap the node into a block_exp
wrap: (node) => wrap: (node, group_type="block_exp") =>
build.block_exp { build[group_type] {
build.assign_one @accum_name, build.table! build.assign_one @accum_name, build.table!
build.assign_one @len_name, 1 build.assign_one @len_name, 1
node node
@accum_name group_type == "block_exp" and @accum_name or NOOP
} }
-- 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
@ -737,7 +750,7 @@ class Accumulator
@value_name @value_name
update = { update = {
build.assign_one @accum_name\index(@len_name), val build.assign_one NameProxy.index(@accum_name, @len_name), val
{"update", @len_name, "+=", 1} {"update", @len_name, "+=", 1}
} }

View File

@ -18,25 +18,22 @@ dont_bubble = function()
return _accum_0 return _accum_0
end)() end)()
end end
local k = (function() local k = { }
local _accum_0 = { } local _len_0 = 1
local _len_0 = 1 for x in (function(...)
for x in (function(...) return print(...)
return print(...) end)("hello") do
end)("hello") do k[_len_0] = x
_accum_0[_len_0] = x _len_0 = _len_0 + 1
_len_0 = _len_0 + 1 end
end
return _accum_0
end)()
local j = (function() local j = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 1 local _len_1 = 1
for i = 1, 10 do for i = 1, 10 do
_accum_0[_len_0] = function(...) _accum_0[_len_1] = function(...)
return print(...) return print(...)
end end
_len_0 = _len_0 + 1 _len_1 = _len_1 + 1
end end
return _accum_0 return _accum_0
end)() end)()
@ -44,15 +41,15 @@ local m
m = function(...) m = function(...)
return (function(...) return (function(...)
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 1 local _len_1 = 1
local _list_0 = { local _list_0 = {
... ...
} }
for _index_0 = 1, #_list_0 do for _index_0 = 1, #_list_0 do
local x = _list_0[_index_0] local x = _list_0[_index_0]
if f(...) > 4 then if f(...) > 4 then
_accum_0[_len_0] = x _accum_0[_len_1] = x
_len_0 = _len_0 + 1 _len_1 = _len_1 + 1
end end
end end
return _accum_0 return _accum_0
@ -60,58 +57,52 @@ m = function(...)
end end
local x = (function(...) local x = (function(...)
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 1 local _len_1 = 1
local _list_0 = { local _list_0 = {
... ...
} }
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]
_accum_0[_len_0] = i _accum_0[_len_1] = i
_len_0 = _len_0 + 1 _len_1 = _len_1 + 1
end end
return _accum_0 return _accum_0
end)(...) end)(...)
local y = (function(...) local y = { }
local _accum_0 = { } local _len_1 = 1
local _len_0 = 1 local _list_0 = {
local _list_0 = { ...
... }
} for _index_0 = 1, #_list_0 do
for _index_0 = 1, #_list_0 do x = _list_0[_index_0]
x = _list_0[_index_0] y[_len_1] = x
_accum_0[_len_0] = x _len_1 = _len_1 + 1
_len_0 = _len_0 + 1 end
local z = { }
local _len_2 = 1
for x in hallo do
if f(...) > 4 then
z[_len_2] = x
_len_2 = _len_2 + 1
end end
return _accum_0 end
end)(...)
local z = (function(...)
local _accum_0 = { }
local _len_0 = 1
for x in hallo do
if f(...) > 4 then
_accum_0[_len_0] = x
_len_0 = _len_0 + 1
end
end
return _accum_0
end)(...)
local a = (function(...) local a = (function(...)
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 1 local _len_3 = 1
for i = 1, 10 do for i = 1, 10 do
_accum_0[_len_0] = ... _accum_0[_len_3] = ...
_len_0 = _len_0 + 1 _len_3 = _len_3 + 1
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_3 = 1
for i = 1, 10 do for i = 1, 10 do
_accum_0[_len_0] = function() _accum_0[_len_3] = function()
return print(...) return print(...)
end end
_len_0 = _len_0 + 1 _len_3 = _len_3 + 1
end end
return _accum_0 return _accum_0
end)() end)()

View File

@ -79,79 +79,61 @@ _ = (function()
end end
return _tbl_0 return _tbl_0
end)() end)()
local n1 = (function() local n1 = { }
local _accum_0 = { } local _len_0 = 1
local _len_0 = 1 for i = 1, 10 do
n1[_len_0] = i
_len_0 = _len_0 + 1
end
local n2 = { }
local _len_1 = 1
for i = 1, 10 do
if i % 2 == 1 then
n2[_len_1] = i
_len_1 = _len_1 + 1
end
end
local aa = { }
local _len_2 = 1
for x = 1, 10 do
for y = 5, 14 do
aa[_len_2] = {
x,
y
}
_len_2 = _len_2 + 1
end
end
local bb = { }
local _len_3 = 1
for thing in y do
for i = 1, 10 do for i = 1, 10 do
_accum_0[_len_0] = i bb[_len_3] = y
_len_0 = _len_0 + 1 _len_3 = _len_3 + 1
end end
return _accum_0 end
end)() local cc = { }
local n2 = (function() local _len_4 = 1
local _accum_0 = { } for i = 1, 10 do
local _len_0 = 1
for i = 1, 10 do
if i % 2 == 1 then
_accum_0[_len_0] = i
_len_0 = _len_0 + 1
end
end
return _accum_0
end)()
local aa = (function()
local _accum_0 = { }
local _len_0 = 1
for x = 1, 10 do
for y = 5, 14 do
_accum_0[_len_0] = {
x,
y
}
_len_0 = _len_0 + 1
end
end
return _accum_0
end)()
local bb = (function()
local _accum_0 = { }
local _len_0 = 1
for thing in y do for thing in y do
for i = 1, 10 do cc[_len_4] = y
_accum_0[_len_0] = y _len_4 = _len_4 + 1
_len_0 = _len_0 + 1
end
end end
return _accum_0 end
end)() local dd = { }
local cc = (function() local _len_5 = 1
local _accum_0 = { } for i = 1, 10 do
local _len_0 = 1 if cool then
for i = 1, 10 do
for thing in y do for thing in y do
_accum_0[_len_0] = y if x > 3 then
_len_0 = _len_0 + 1 if c + 3 then
end dd[_len_5] = y
end _len_5 = _len_5 + 1
return _accum_0
end)()
local dd = (function()
local _accum_0 = { }
local _len_0 = 1
for i = 1, 10 do
if cool then
for thing in y do
if x > 3 then
if c + 3 then
_accum_0[_len_0] = y
_len_0 = _len_0 + 1
end
end end
end end
end end
end end
return _accum_0 end
end)()
_ = (function() _ = (function()
local _tbl_0 = { } local _tbl_0 = { }
for i = 1, 10 do for i = 1, 10 do

View File

@ -1,17 +1,14 @@
local hi = (function() local hi = { }
local _accum_0 = { } local _len_0 = 1
local _len_0 = 1 for _, x in ipairs({
for _, x in ipairs({ 1,
1, 2,
2, 3,
3, 4
4 }) do
}) do hi[_len_0] = x * 2
_accum_0[_len_0] = x * 2 _len_0 = _len_0 + 1
_len_0 = _len_0 + 1 end
end
return _accum_0
end)()
local items = { local items = {
1, 1,
2, 2,
@ -20,40 +17,34 @@ local items = {
5, 5,
6 6
} }
local mm = (function() local mm = { }
local _accum_0 = { } local _len_1 = 1
local _len_0 = 1 for self.x in ipairs(items) do
for self.x in ipairs(items) do mm[_len_1] = self.x
_accum_0[_len_0] = self.x _len_1 = _len_1 + 1
_len_0 = _len_0 + 1 end
end
return _accum_0
end)()
for z in ipairs(items) do for z in ipairs(items) do
if z > 4 then if z > 4 then
local _ = z local _ = z
end end
end end
local rad = (function() local rad = { }
local _accum_0 = { } local _len_2 = 1
local _len_0 = 1 for a in ipairs({
for a in ipairs({ 1,
1, 2,
2, 3,
3, 4,
4, 5,
5, 6
6 }) do
}) do if good_number(a) then
if good_number(a) then rad[_len_2] = {
_accum_0[_len_0] = { a
a }
} _len_2 = _len_2 + 1
_len_0 = _len_0 + 1
end
end end
return _accum_0 end
end)()
for z in items do for z in items do
for j in list do for j in list do
if z > 4 then if z > 4 then
@ -78,44 +69,41 @@ range = function(count)
end end
dump((function() dump((function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 1 local _len_3 = 1
for x in range(10) do for x in range(10) do
_accum_0[_len_0] = x _accum_0[_len_3] = x
_len_0 = _len_0 + 1 _len_3 = _len_3 + 1
end end
return _accum_0 return _accum_0
end)()) end)())
dump((function() dump((function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 1 local _len_3 = 1
for x in range(5) do for x in range(5) do
if x > 2 then if x > 2 then
for y in range(5) do for y in range(5) do
_accum_0[_len_0] = { _accum_0[_len_3] = {
x, x,
y y
} }
_len_0 = _len_0 + 1 _len_3 = _len_3 + 1
end end
end end
end end
return _accum_0 return _accum_0
end)()) end)())
local things = (function() local things = { }
local _accum_0 = { } local _len_3 = 1
local _len_0 = 1 for x in range(10) do
for x in range(10) do if x > 5 then
if x > 5 then for y in range(10) do
for y in range(10) do if y > 7 then
if y > 7 then things[_len_3] = x + y
_accum_0[_len_0] = x + y _len_3 = _len_3 + 1
_len_0 = _len_0 + 1
end
end end
end end
end end
return _accum_0 end
end)()
for x in ipairs({ for x in ipairs({
1, 1,
2, 2,
@ -137,15 +125,12 @@ end
for x in x do for x in x do
local _ = x local _ = x
end end
local x = (function() local x = { }
local _accum_0 = { } local _len_4 = 1
local _len_0 = 1 for x in x do
for x in x do x[_len_4] = x
_accum_0[_len_0] = x _len_4 = _len_4 + 1
_len_0 = _len_0 + 1 end
end
return _accum_0
end)()
for x in ipairs({ for x in ipairs({
1, 1,
2, 2,
@ -161,45 +146,36 @@ for x in ipairs({
end end
end end
end end
local double = (function() local double = { }
local _accum_0 = { } local _len_5 = 1
local _len_0 = 1 for _index_0 = 1, #items do
for _index_0 = 1, #items do x = items[_index_0]
x = items[_index_0] double[_len_5] = x * 2
_accum_0[_len_0] = x * 2 _len_5 = _len_5 + 1
_len_0 = _len_0 + 1 end
end
return _accum_0
end)()
for _index_0 = 1, #double do for _index_0 = 1, #double do
x = double[_index_0] x = double[_index_0]
print(x) print(x)
end end
local cut = (function() local cut = { }
local _accum_0 = { } local _len_6 = 1
local _len_0 = 1 for _index_0 = 1, #items do
for _index_0 = 1, #items do x = items[_index_0]
x = items[_index_0] if x > 3 then
if x > 3 then cut[_len_6] = x
_accum_0[_len_0] = x _len_6 = _len_6 + 1
_len_0 = _len_0 + 1
end
end end
return _accum_0 end
end)() local hello = { }
local hello = (function() local _len_7 = 1
local _accum_0 = { } for _index_0 = 1, #items do
local _len_0 = 1 x = items[_index_0]
for _index_0 = 1, #items do for _index_1 = 1, #items do
x = items[_index_0] local y = items[_index_1]
for _index_1 = 1, #items do hello[_len_7] = x + y
local y = items[_index_1] _len_7 = _len_7 + 1
_accum_0[_len_0] = x + y
_len_0 = _len_0 + 1
end
end end
return _accum_0 end
end)()
for _index_0 = 1, #hello do for _index_0 = 1, #hello do
local z = hello[_index_0] local z = hello[_index_0]
print(z) print(z)
@ -251,10 +227,10 @@ local normal
normal = function(hello) normal = function(hello)
return (function() return (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 1 local _len_8 = 1
for x in yeah do for x in yeah do
_accum_0[_len_0] = x _accum_0[_len_8] = x
_len_0 = _len_0 + 1 _len_8 = _len_8 + 1
end end
return _accum_0 return _accum_0
end)() end)()
@ -281,11 +257,11 @@ end
return function() return function()
return (function() return (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 1 local _len_8 = 1
for _index_0 = 1, #things do for _index_0 = 1, #things do
x = things[_index_0] x = things[_index_0]
_accum_0[_len_0] = x _accum_0[_len_8] = x
_len_0 = _len_0 + 1 _len_8 = _len_8 + 1
end end
return _accum_0 return _accum_0
end)() end)()