recompiled

This commit is contained in:
leaf corcoran 2011-07-14 23:29:06 -07:00
parent 4634b76ad0
commit a57efb3511
4 changed files with 658 additions and 226 deletions

View File

@ -13,10 +13,12 @@ Line = (function(_parent_0)
local _base_0 = { local _base_0 = {
_append_single = function(self, item) _append_single = function(self, item)
if util.moon.type(item) == Line then if util.moon.type(item) == Line then
local _item_0 = item do
for _index_0=1,#_item_0 do local _item_0 = item
local value = _item_0[_index_0] for _index_0 = 1, #_item_0 do
self:_append_single(value) local value = _item_0[_index_0]
self:_append_single(value)
end
end end
else else
insert(self, item) insert(self, item)
@ -32,15 +34,19 @@ Line = (function(_parent_0)
end end
end, end,
append = function(self, ...) append = function(self, ...)
local _item_0 = { ... } do
for _index_0=1,#_item_0 do local _item_0 = {
local item = _item_0[_index_0] ...
self:_append_single(item) }
for _index_0 = 1, #_item_0 do
local item = _item_0[_index_0]
self:_append_single(item)
end
end end
return nil return nil
end, end,
render = function(self) render = function(self)
local buff = { } local buff = { }
for i = 1, #self do for i = 1, #self do
local c = self[i] local c = self[i]
insert(buff, (function() insert(buff, (function()
@ -55,15 +61,23 @@ Line = (function(_parent_0)
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0
local _class_0 = setmetatable({ __init = function(self, ...) if _parent_0 then
setmetatable(_base_0, getmetatable(_parent_0).__index)
end
local _class_0 = setmetatable({
__init = function(self, ...)
if _parent_0 then if _parent_0 then
return _parent_0.__init(self, ...) return _parent_0.__init(self, ...)
end end
end }, { __index = _base_0, __call = function(mt, ...) end
}, {
__index = _base_0,
__call = function(mt, ...)
local self = setmetatable({}, _base_0) local self = setmetatable({}, _base_0)
mt.__init(self, ...) mt.__init(self, ...)
return self return self
end }) end
})
_base_0.__class = _class_0 _base_0.__class = _class_0
return _class_0 return _class_0
end)() end)()
@ -72,36 +86,57 @@ Block_ = (function(_parent_0)
local _base_0 = { local _base_0 = {
header = "do", header = "do",
footer = "end", footer = "end",
line_table = function(self) return self._posmap end, line_table = function(self)
set = function(self, name, value) self._state[name] = value end, return self._posmap
get = function(self, name) return self._state[name] end, end,
set = function(self, name, value)
self._state[name] = value
end,
get = function(self, name)
return self._state[name]
end,
declare = function(self, names) declare = function(self, names)
local undeclared = (function() local undeclared = (function()
local _moon_0 = {} local _accum_0 = { }
local _item_0 = names do
for _index_0=1,#_item_0 do local _item_0 = names
local name = _item_0[_index_0] for _index_0 = 1, #_item_0 do
if type(name) == "string" and not self:has_name(name) then local name = _item_0[_index_0]
table.insert(_moon_0, name) if type(name) == "string" and not self:has_name(name) then
table.insert(_accum_0, name)
end
end end
end end
return _moon_0 return _accum_0
end)() end)()
local _item_0 = undeclared do
for _index_0=1,#_item_0 do local _item_0 = undeclared
local name = _item_0[_index_0] for _index_0 = 1, #_item_0 do
self:put_name(name) local name = _item_0[_index_0]
self:put_name(name)
end
end end
return undeclared return undeclared
end, end,
put_name = function(self, name) self._names[name] = true end, put_name = function(self, name)
has_name = function(self, name) return self._names[name] end, self._names[name] = true
end,
has_name = function(self, name)
return self._names[name]
end,
shadow_name = function(self, name)
self._names[name] = false
end,
free_name = function(self, prefix, dont_put) free_name = function(self, prefix, dont_put)
prefix = prefix or "moon" prefix = prefix or "moon"
local searching = true local searching = true
local name, i = nil, 0 local name, i = nil, 0
while searching do while searching do
name = concat({ "", prefix, i }, "_") name = concat({
"",
prefix,
i
}, "_")
i = i + 1 i = i + 1
searching = self:has_name(name) searching = self:has_name(name)
end end
@ -112,7 +147,15 @@ Block_ = (function(_parent_0)
end, end,
init_free_var = function(self, prefix, value) init_free_var = function(self, prefix, value)
local name = self:free_name(prefix, true) local name = self:free_name(prefix, true)
self:stm({ "assign", { name }, { value } }) self:stm({
"assign",
{
name
},
{
value
}
})
return name return name
end, end,
mark_pos = function(self, node) mark_pos = function(self, node)
@ -137,8 +180,14 @@ Block_ = (function(_parent_0)
return error("Adding unknown item") return error("Adding unknown item")
end end
end, end,
push = function(self) self._names = setmetatable({ }, { __index = self._names }) end, push = function(self)
pop = function(self) self._names = getmetatable(self._names).__index end, self._names = setmetatable({ }, {
__index = self._names
})
end,
pop = function(self)
self._names = getmetatable(self._names).__index
end,
_insert_breaks = function(self) _insert_breaks = function(self)
for i = 1, #self._lines - 1 do for i = 1, #self._lines - 1 do
local left, right = self._lines[i], self._lines[i + 1] local left, right = self._lines[i], self._lines[i + 1]
@ -159,22 +208,31 @@ Block_ = (function(_parent_0)
local header = flatten(self.header) local header = flatten(self.header)
if #self._lines == 0 then if #self._lines == 0 then
local footer = flatten(self.footer) local footer = flatten(self.footer)
return(concat({ header, footer }, " ")) return(concat({
header,
footer
}, " "))
end end
local indent = indent_char:rep(self.indent) local indent = indent_char:rep(self.indent)
if not self.delim then if not self.delim then
self:_insert_breaks() self:_insert_breaks()
end end
local body = indent .. concat(self._lines, (self.delim or "") .. "\n" .. indent) local body = indent .. concat(self._lines, (self.delim or "") .. "\n" .. indent)
return concat({ header, body, indent_char:rep(self.indent - 1) .. (function() return concat({
header,
body,
indent_char:rep(self.indent - 1) .. (function()
if self.next then if self.next then
return self.next:render() return self.next:render()
else else
return flatten(self.footer) return flatten(self.footer)
end end
end)() }, "\n") end)()
}, "\n")
end,
block = function(self, header, footer)
return Block(self, header, footer)
end, end,
block = function(self, header, footer) return Block(self, header, footer) end,
line = function(self, ...) line = function(self, ...)
do do
local _with_0 = Line() local _with_0 = Line()
@ -182,12 +240,16 @@ Block_ = (function(_parent_0)
return _with_0 return _with_0
end end
end, end,
is_stm = function(self, node) return line_compile[ntype(node)] ~= nil end, is_stm = function(self, node)
return line_compile[ntype(node)] ~= nil
end,
is_value = function(self, node) is_value = function(self, node)
local t = ntype(node) local t = ntype(node)
return value_compile[t] ~= nil or t == "value" return value_compile[t] ~= nil or t == "value"
end, end,
name = function(self, node) return self:value(node) end, name = function(self, node)
return self:value(node)
end,
value = function(self, node, ...) value = function(self, node, ...)
if type(node) ~= "table" then if type(node) ~= "table" then
return(tostring(node)) return(tostring(node))
@ -204,13 +266,15 @@ Block_ = (function(_parent_0)
do do
local _with_0 = Line() local _with_0 = Line()
_with_0:append_list((function() _with_0:append_list((function()
local _moon_0 = {} local _accum_0 = { }
local _item_0 = values do
for _index_0=1,#_item_0 do local _item_0 = values
local v = _item_0[_index_0] for _index_0 = 1, #_item_0 do
table.insert(_moon_0, self:value(v)) local v = _item_0[_index_0]
table.insert(_accum_0, self:value(v))
end
end end
return _moon_0 return _accum_0
end)(), delim) end)(), delim)
return _with_0 return _with_0
end end
@ -219,7 +283,15 @@ Block_ = (function(_parent_0)
local fn = line_compile[ntype(node)] local fn = line_compile[ntype(node)]
if not fn then if not fn then
if has_value(node) then if has_value(node) then
return self:stm({ "assign", { "_" }, { node } }) return self:stm({
"assign",
{
"_"
},
{
node
}
})
else else
return self:add(self:value(node)) return self:add(self:value(node))
end end
@ -260,57 +332,78 @@ Block_ = (function(_parent_0)
if ret then if ret then
self:ret_stms(stms, ret) self:ret_stms(stms, ret)
else else
local _item_0 = stms do
for _index_0=1,#_item_0 do local _item_0 = stms
local stm = _item_0[_index_0] for _index_0 = 1, #_item_0 do
self:stm(stm) local stm = _item_0[_index_0]
self:stm(stm)
end
end end
end end
return nil return nil
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0
local _class_0 = setmetatable({ __init = function(self, parent, header, footer) if _parent_0 then
setmetatable(_base_0, getmetatable(_parent_0).__index)
end
local _class_0 = setmetatable({
__init = function(self, parent, header, footer)
self.parent, self.header, self.footer = parent, header, footer self.parent, self.header, self.footer = parent, header, footer
self.line_offset = 1 self.line_offset = 1
self._lines = { } self._lines = { }
self._posmap = { } self._posmap = { }
self._names = { } self._names = { }
self._state = { } self._state = { }
if self.parent then if self.parent then
self.indent = self.parent.indent + 1 self.indent = self.parent.indent + 1
setmetatable(self._state, { __index = self.parent._state }) setmetatable(self._state, {
return setmetatable(self._names, { __index = self.parent._names }) __index = self.parent._state
})
return setmetatable(self._names, {
__index = self.parent._names
})
else else
self.indent = 0 self.indent = 0
end end
end }, { __index = _base_0, __call = function(mt, ...) end
}, {
__index = _base_0,
__call = function(mt, ...)
local self = setmetatable({}, _base_0) local self = setmetatable({}, _base_0)
mt.__init(self, ...) mt.__init(self, ...)
return self return self
end }) end
})
_base_0.__class = _class_0 _base_0.__class = _class_0
return _class_0 return _class_0
end)() end)()
local RootBlock local RootBlock
RootBlock = (function(_parent_0) RootBlock = (function(_parent_0)
local _base_0 = { render = function(self) local _base_0 = {
render = function(self)
self:_insert_breaks() self:_insert_breaks()
return concat(self._lines, "\n") return concat(self._lines, "\n")
end } end
}
_base_0.__index = _base_0 _base_0.__index = _base_0
if _parent_0 then if _parent_0 then
setmetatable(_base_0, getmetatable(_parent_0).__index) setmetatable(_base_0, getmetatable(_parent_0).__index)
end end
local _class_0 = setmetatable({ __init = function(self, ...) local _class_0 = setmetatable({
__init = function(self, ...)
if _parent_0 then if _parent_0 then
return _parent_0.__init(self, ...) return _parent_0.__init(self, ...)
end end
end }, { __index = _base_0, __call = function(mt, ...) end
}, {
__index = _base_0,
__call = function(mt, ...)
local self = setmetatable({}, _base_0) local self = setmetatable({}, _base_0)
mt.__init(self, ...) mt.__init(self, ...)
return self return self
end }) end
})
_base_0.__class = _class_0 _base_0.__class = _class_0
return _class_0 return _class_0
end)(Block_) end)(Block_)
@ -318,15 +411,20 @@ Block = Block_
format_error = function(msg, pos, file_str) format_error = function(msg, pos, file_str)
local line = pos_to_line(file_str, pos) local line = pos_to_line(file_str, pos)
local line_str = get_line(file_str, line) or "" local line_str = get_line(file_str, line) or ""
return concat({ msg, ("On line [%d]:\n\t%s"):format(line, trim(line_str, line)) }, "\n") return concat({
msg,
("On line [%d]:\n\t%s"):format(line, trim(line_str, line))
}, "\n")
end end
tree = function(tree) tree = function(tree)
local scope = RootBlock() local scope = RootBlock()
local runner = coroutine.create(function() local runner = coroutine.create(function()
local _item_0 = tree do
for _index_0=1,#_item_0 do local _item_0 = tree
local line = _item_0[_index_0] for _index_0 = 1, #_item_0 do
scope:stm(line) local line = _item_0[_index_0]
scope:stm(line)
end
end end
return scope:render() return scope:render()
end) end)

View File

@ -7,18 +7,30 @@ local concat, insert = table.concat, table.insert
indent_char = " " indent_char = " "
returner = function(exp) returner = function(exp)
if ntype(exp) == "chain" and exp[2] == "return" then if ntype(exp) == "chain" and exp[2] == "return" then
local items = { "explist" } local items = {
local _item_0 = exp[3][2] "explist"
for _index_0=1,#_item_0 do }
local v = _item_0[_index_0] do
insert(items, v) local _item_0 = exp[3][2]
for _index_0 = 1, #_item_0 do
local v = _item_0[_index_0]
insert(items, v)
end
end end
return { "return", items } return {
"return",
items
}
else else
return { "return", exp } return {
"return",
exp
}
end end
end end
moonlib = { bind = function(tbl, name) return concat({ moonlib = {
bind = function(tbl, name)
return concat({
"moon.bind(", "moon.bind(",
tbl, tbl,
".", ".",
@ -26,9 +38,16 @@ moonlib = { bind = function(tbl, name) return concat({
", ", ", ",
tbl, tbl,
")" ")"
}) end } })
cascading = Set({ "if", "with" }) end
non_atomic = Set({ "update" }) }
cascading = Set({
"if",
"with"
})
non_atomic = Set({
"update"
})
has_value = function(node) has_value = function(node)
if ntype(node) == "chain" then if ntype(node) == "chain" then
local ctype = ntype(node[#node]) local ctype = ntype(node[#node])
@ -37,8 +56,12 @@ has_value = function(node)
return true return true
end end
end end
is_non_atomic = function(node) return non_atomic[ntype(node)] end is_non_atomic = function(node)
is_slice = function(node) return ntype(node) == "chain" and ntype(node[#node]) == "slice" end return non_atomic[ntype(node)]
end
is_slice = function(node)
return ntype(node) == "chain" and ntype(node[#node]) == "slice"
end
count_lines = function(str) count_lines = function(str)
local count = 1 local count = 1
for _ in str:gmatch("\n") do for _ in str:gmatch("\n") do

View File

@ -19,13 +19,15 @@ line_compile = {
do do
local _with_0 = self:line("local ") local _with_0 = self:line("local ")
_with_0:append_list((function() _with_0:append_list((function()
local _moon_0 = {} local _accum_0 = { }
local _item_0 = names do
for _index_0=1,#_item_0 do local _item_0 = names
local name = _item_0[_index_0] for _index_0 = 1, #_item_0 do
table.insert(_moon_0, self:name(name)) local name = _item_0[_index_0]
table.insert(_accum_0, self:name(name))
end
end end
return _moon_0 return _accum_0
end)(), ", ") end)(), ", ")
return _with_0 return _with_0
end end
@ -41,7 +43,15 @@ line_compile = {
end end
if cascading[ntype(values)] then if cascading[ntype(values)] then
local decorate local decorate
decorate = function(value) return { "assign", names, { value } } end decorate = function(value)
return {
"assign",
names,
{
value
}
}
end
return self:stm(values, decorate) return self:stm(values, decorate)
else else
return error("Assigning unsupported statement") return error("Assigning unsupported statement")
@ -64,24 +74,28 @@ line_compile = {
self:add(declare) self:add(declare)
end end
_with_0:append_list((function() _with_0:append_list((function()
local _moon_0 = {} local _accum_0 = { }
local _item_0 = names do
for _index_0=1,#_item_0 do local _item_0 = names
local name = _item_0[_index_0] for _index_0 = 1, #_item_0 do
table.insert(_moon_0, self:value(name)) local name = _item_0[_index_0]
table.insert(_accum_0, self:value(name))
end
end end
return _moon_0 return _accum_0
end)(), ", ") end)(), ", ")
end end
_with_0:append(" = ") _with_0:append(" = ")
_with_0:append_list((function() _with_0:append_list((function()
local _moon_0 = {} local _accum_0 = { }
local _item_0 = values do
for _index_0=1,#_item_0 do local _item_0 = values
local v = _item_0[_index_0] for _index_0 = 1, #_item_0 do
table.insert(_moon_0, self:value(v)) local v = _item_0[_index_0]
table.insert(_accum_0, self:value(v))
end
end end
return _moon_0 return _accum_0
end)(), ", ") end)(), ", ")
return _with_0 return _with_0
end end
@ -93,18 +107,30 @@ line_compile = {
if not op_final then if not op_final then
_ = error("unknown op: ") .. op _ = error("unknown op: ") .. op
end end
return self:stm({ "assign", { name }, { { return self:stm({
"assign",
{
name
},
{
{
"exp", "exp",
name, name,
op_final, op_final,
exp exp
} } }) }
}
})
end,
["return"] = function(self, node)
return self:line("return ", self:value(node[2]))
end,
["break"] = function(self, node)
return "break"
end, end,
["return"] = function(self, node) return self:line("return ", self:value(node[2])) end,
["break"] = function(self, node) return "break" end,
import = function(self, node) import = function(self, node)
local _, names, source = unpack(node) local _, names, source = unpack(node)
local to_bind = { } local to_bind = { }
local get_name local get_name
get_name = function(name) get_name = function(name)
if ntype(name) == ":" then if ntype(name) == ":" then
@ -116,18 +142,22 @@ line_compile = {
end end
end end
local final_names = (function() local final_names = (function()
local _moon_0 = {} local _accum_0 = { }
local _item_0 = names do
for _index_0=1,#_item_0 do local _item_0 = names
local n = _item_0[_index_0] for _index_0 = 1, #_item_0 do
table.insert(_moon_0, get_name(n)) local n = _item_0[_index_0]
table.insert(_accum_0, get_name(n))
end
end end
return _moon_0 return _accum_0
end)() end)()
local _item_0 = final_names do
for _index_0=1,#_item_0 do local _item_0 = final_names
local name = _item_0[_index_0] for _index_0 = 1, #_item_0 do
self:put_name(name) local name = _item_0[_index_0]
self:put_name(name)
end
end end
local get_value local get_value
get_value = function(name) get_value = function(name)
@ -139,13 +169,15 @@ line_compile = {
end end
if type(source) == "string" then if type(source) == "string" then
local values = (function() local values = (function()
local _moon_0 = {} local _accum_0 = { }
local _item_0 = final_names do
for _index_0=1,#_item_0 do local _item_0 = final_names
local name = _item_0[_index_0] for _index_0 = 1, #_item_0 do
table.insert(_moon_0, get_value(name)) local name = _item_0[_index_0]
table.insert(_accum_0, get_value(name))
end
end end
return _moon_0 return _accum_0
end)() end)()
local line local line
do do
@ -159,10 +191,20 @@ line_compile = {
do do
local _with_0 = self:block("do") local _with_0 = self:block("do")
source = _with_0:init_free_var("table", source) source = _with_0:init_free_var("table", source)
local _item_0 = final_names do
for _index_0=1,#_item_0 do local _item_0 = final_names
local name = _item_0[_index_0] for _index_0 = 1, #_item_0 do
_with_0:stm({ "assign", { name }, { get_value(name) } }) local name = _item_0[_index_0]
_with_0:stm({
"assign",
{
name
},
{
get_value(name)
}
})
end
end end
return _with_0 return _with_0
end end
@ -191,10 +233,12 @@ line_compile = {
current.next = next current.next = next
current = next current = next
end end
local _item_0 = node do
for _index_0=4,#_item_0 do local _item_0 = node
local cond = _item_0[_index_0] for _index_0 = 4, #_item_0 do
add_clause(cond) local cond = _item_0[_index_0]
add_clause(cond)
end
end end
return root return root
end, end,
@ -204,7 +248,18 @@ line_compile = {
if is_non_atomic(cond) then if is_non_atomic(cond) then
do do
local _with_0 = self:block("while true do") local _with_0 = self:block("while true do")
_with_0:stm({ "if", { "not", cond }, { { "break" } } }) _with_0:stm({
"if",
{
"not",
cond
},
{
{
"break"
}
}
})
out = _with_0 out = _with_0
end end
else else
@ -215,7 +270,10 @@ line_compile = {
end, end,
["for"] = function(self, node) ["for"] = function(self, node)
local _, name, bounds, block = unpack(node) local _, name, bounds, block = unpack(node)
local loop = self:line("for ", self:name(name), " = ", self:value({ "explist", unpack(bounds) }), " do") local loop = self:line("for ", self:name(name), " = ", self:value({
"explist",
unpack(bounds)
}), " do")
do do
local _with_0 = self:block(loop) local _with_0 = self:block(loop)
_with_0:stms(block) _with_0:stms(block)
@ -243,31 +301,73 @@ line_compile = {
"<", "<",
0, 0,
"and", "and",
{ "length", items_tmp }, {
"length",
items_tmp
},
"+", "+",
max_tmp, max_tmp,
"or", "or",
max_tmp max_tmp
} }
else else
slice[2] = { "length", items_tmp } slice[2] = {
"length",
items_tmp
}
end end
bounds = slice bounds = slice
else else
bounds = { 1, { "length", items_tmp } } bounds = {
1,
{
"length",
items_tmp
}
}
end end
local index_tmp = _with_0:free_name("index") local index_tmp = _with_0:free_name("index")
_with_0:stm({ "assign", { items_tmp }, { iter } }) _with_0:stm({
"assign",
{
items_tmp
},
{
iter
}
})
block = (function() block = (function()
local _moon_0 = {} local _accum_0 = { }
local _item_0 = block do
for _index_0=1,#_item_0 do local _item_0 = block
local s = _item_0[_index_0] for _index_0 = 1, #_item_0 do
table.insert(_moon_0, s) local s = _item_0[_index_0]
table.insert(_accum_0, s)
end
end end
return _moon_0 return _accum_0
end)() end)()
insert(block, 1, { "assign", names, { { "chain", items_tmp, { "index", index_tmp } } } }) do
local _item_0 = names
for _index_0 = 1, #_item_0 do
local name = _item_0[_index_0]
_with_0:shadow_name(name)
end
end
insert(block, 1, {
"assign",
names,
{
{
"chain",
items_tmp,
{
"index",
index_tmp
}
}
}
})
_with_0:stm({ _with_0:stm({
"for", "for",
index_tmp, index_tmp,
@ -283,13 +383,15 @@ line_compile = {
local _with_0 = self:line() local _with_0 = self:line()
_with_0:append("for ") _with_0:append("for ")
_with_0:append_list((function() _with_0:append_list((function()
local _moon_0 = {} local _accum_0 = { }
local _item_0 = names do
for _index_0=1,#_item_0 do local _item_0 = names
local name = _item_0[_index_0] for _index_0 = 1, #_item_0 do
table.insert(_moon_0, self:name(name)) local name = _item_0[_index_0]
table.insert(_accum_0, self:name(name))
end
end end
return _moon_0 return _accum_0
end)(), ", ") end)(), ", ")
_with_0:append(" in ", self:value(exp), " do") _with_0:append(" in ", self:value(exp), " do")
loop = _with_0 loop = _with_0
@ -308,31 +410,55 @@ line_compile = {
class = function(self, node) class = function(self, node)
local _, name, parent_val, tbl = unpack(node) local _, name, parent_val, tbl = unpack(node)
local constructor = nil local constructor = nil
local final_properties = { } local final_properties = { }
local find_special local find_special
find_special = function(name, value) find_special = function(name, value)
if name == constructor_name then if name == constructor_name then
constructor = value constructor = value
else else
return insert(final_properties, { name, value }) return insert(final_properties, {
name,
value
})
end end
end end
local _item_0 = tbl[2] do
for _index_0=1,#_item_0 do local _item_0 = tbl[2]
local entry = _item_0[_index_0] for _index_0 = 1, #_item_0 do
find_special(unpack(entry)) local entry = _item_0[_index_0]
find_special(unpack(entry))
end
end end
tbl[2] = final_properties tbl[2] = final_properties
local parent_loc = self:free_name("parent", true) local parent_loc = self:free_name("parent", true)
if not constructor then if not constructor then
constructor = { constructor = {
"fndef", "fndef",
{ "..." }, {
"..."
},
"fat", "fat",
{ { "if", parent_loc, { { "chain", "super", { "call", { "..." } } } } } } {
{
"if",
parent_loc,
{
{
"chain",
"super",
{
"call",
{
"..."
}
}
}
}
}
}
} }
end end
local self_args = { } local self_args = { }
local get_initializers local get_initializers
get_initializers = function(arg) get_initializers = function(arg)
if ntype(arg) == "self" then if ntype(arg) == "self" then
@ -342,27 +468,38 @@ line_compile = {
return arg return arg
end end
constructor[2] = (function() constructor[2] = (function()
local _moon_0 = {} local _accum_0 = { }
local _item_0 = constructor[2] do
for _index_0=1,#_item_0 do local _item_0 = constructor[2]
local arg = _item_0[_index_0] for _index_0 = 1, #_item_0 do
table.insert(_moon_0, get_initializers(arg)) local arg = _item_0[_index_0]
table.insert(_accum_0, get_initializers(arg))
end
end end
return _moon_0 return _accum_0
end)() end)()
constructor[3] = "fat" constructor[3] = "fat"
local body = constructor[4] local body = constructor[4]
local dests = (function() local dests = (function()
local _moon_0 = {} local _accum_0 = { }
local _item_0 = self_args do
for _index_0=1,#_item_0 do local _item_0 = self_args
local name = _item_0[_index_0] for _index_0 = 1, #_item_0 do
table.insert(_moon_0, { "self", name }) local name = _item_0[_index_0]
table.insert(_accum_0, {
"self",
name
})
end
end end
return _moon_0 return _accum_0
end)() end)()
if #self_args > 0 then if #self_args > 0 then
insert(body, 1, { "assign", dests, self_args }) insert(body, 1, {
"assign",
dests,
self_args
})
end end
local def_scope local def_scope
do do
@ -376,15 +513,23 @@ line_compile = {
_with_0:set("super", function(block, chain) _with_0:set("super", function(block, chain)
local calling_name = block:get("current_block") local calling_name = block:get("current_block")
local slice = (function() local slice = (function()
local _moon_0 = {} local _accum_0 = { }
local _item_0 = chain do
for _index_0=3,#_item_0 do local _item_0 = chain
local item = _item_0[_index_0] for _index_0 = 3, #_item_0 do
table.insert(_moon_0, item) local item = _item_0[_index_0]
table.insert(_accum_0, item)
end
end end
return _moon_0 return _accum_0
end)() end)()
slice[1] = { "call", { "self", unpack(slice[1][2]) } } slice[1] = {
"call",
{
"self",
unpack(slice[1][2])
}
}
local act local act
if ntype(calling_name) ~= "value" then if ntype(calling_name) ~= "value" then
act = "index" act = "index"
@ -394,37 +539,154 @@ line_compile = {
return { return {
"chain", "chain",
parent_loc, parent_loc,
{ act, calling_name }, {
act,
calling_name
},
unpack(slice) unpack(slice)
} }
end) end)
local base_name = _with_0:init_free_var("base", tbl) local base_name = _with_0:init_free_var("base", tbl)
_with_0:stm({ "assign", { { "chain", base_name, { "dot", "__index" } } }, { base_name } }) _with_0:stm({
_with_0:stm({ "if", parent_loc, { { "chain", "setmetatable", { "call", { base_name, { "assign",
{
{
"chain",
base_name,
{
"dot",
"__index"
}
}
},
{
base_name
}
})
_with_0:stm({
"if",
parent_loc,
{
{
"chain",
"setmetatable",
{
"call",
{
base_name,
{
"chain", "chain",
"getmetatable", "getmetatable",
{ "call", { parent_loc } }, {
{ "dot", "__index" } "call",
} } } } } }) {
local cls = { "table", { { "__init", constructor } } } parent_loc
local cls_mt = { "table", { { "__index", base_name }, { "__call", { }
},
{
"dot",
"__index"
}
}
}
}
}
}
})
local cls = {
"table",
{
{
"__init",
constructor
}
}
}
local cls_mt = {
"table",
{
{
"__index",
base_name
},
{
"__call",
{
"fndef", "fndef",
{ "mt", "..." }, {
"mt",
"..."
},
"slim", "slim",
{ { "raw", ("local self = setmetatable({}, %s)"):format(base_name) }, { "chain", "mt.__init", { "call", { "self", "..." } } }, "self" } {
} } } } {
local cls_name = _with_0:init_free_var("class", { "chain", "setmetatable", { "call", { cls, cls_mt } } }) "raw",
_with_0:stm({ "assign", { { "chain", base_name, { "dot", "__class" } } }, { cls_name } }) ("local self = setmetatable({}, %s)"):format(base_name)
_with_0:stm({ "return", cls_name }) },
{
"chain",
"mt.__init",
{
"call",
{
"self",
"..."
}
}
},
"self"
}
}
}
}
}
local cls_name = _with_0:init_free_var("class", {
"chain",
"setmetatable",
{
"call",
{
cls,
cls_mt
}
}
})
_with_0:stm({
"assign",
{
{
"chain",
base_name,
{
"dot",
"__class"
}
}
},
{
cls_name
}
})
_with_0:stm({
"return",
cls_name
})
def_scope = _with_0 def_scope = _with_0
end end
self:stm({ "declare", { name } }) self:stm({
"declare",
{
name
}
})
return self:line(name, " = ", def_scope) return self:line(name, " = ", def_scope)
end, end,
comprehension = function(self, node, action) comprehension = function(self, node, action)
local _, exp, clauses = unpack(node) local _, exp, clauses = unpack(node)
if not action then if not action then
action = function(exp) return exp end action = function(exp)
return exp
end
end end
local statement = action(exp) local statement = action(exp)
local build_clause local build_clause
@ -437,12 +699,20 @@ line_compile = {
"foreach", "foreach",
names, names,
iter, iter,
{ statement } {
statement
}
} }
elseif t == "when" then elseif t == "when" then
local cond local cond
_, cond = unpack(clause) _, cond = unpack(clause)
statement = { "if", cond, { statement } } statement = {
"if",
cond,
{
statement
}
}
else else
statement = error("Unknown comprehension clause: " .. t) statement = error("Unknown comprehension clause: " .. t)
end end

View File

@ -17,13 +17,13 @@ value_compile = {
do do
local _with_0 = self:line() local _with_0 = self:line()
_with_0:append_list((function() _with_0:append_list((function()
local _moon_0 = {} local _accum_0 = { }
for i, v in ipairs(node) do for i, v in ipairs(node) do
if i > 1 then if i > 1 then
table.insert(_moon_0, _comp(i, v)) table.insert(_accum_0, _comp(i, v))
end end
end end
return _moon_0 return _accum_0
end)(), " ") end)(), " ")
return _with_0 return _with_0
end end
@ -37,18 +37,22 @@ value_compile = {
do do
local _with_0 = self:line() local _with_0 = self:line()
_with_0:append_list((function() _with_0:append_list((function()
local _moon_0 = {} local _accum_0 = { }
local _item_0 = node do
for _index_0=2,#_item_0 do local _item_0 = node
local v = _item_0[_index_0] for _index_0 = 2, #_item_0 do
table.insert(_moon_0, self:value(v)) local v = _item_0[_index_0]
table.insert(_accum_0, self:value(v))
end
end end
return _moon_0 return _accum_0
end)(), ", ") end)(), ", ")
return _with_0 return _with_0
end end
end, end,
parens = function(self, node) return self:line("(", self:value(node[2]), ")") end, parens = function(self, node)
return self:line("(", self:value(node[2]), ")")
end,
string = function(self, node) string = function(self, node)
local _, delim, inner, delim_end = unpack(node) local _, delim, inner, delim_end = unpack(node)
return delim .. inner .. (delim_end or delim) return delim .. inner .. (delim_end or delim)
@ -64,11 +68,28 @@ value_compile = {
local exp = node[2] local exp = node[2]
do do
local _with_0 = self:block("(function()", "end)()") local _with_0 = self:block("(function()", "end)()")
local tmp_name = _with_0:init_free_var("accum", { "table" }) local tmp_name = _with_0:init_free_var("accum", {
"table"
})
local action local action
action = function(value) return { "chain", "table.insert", { "call", { tmp_name, value } } } end action = function(value)
return {
"chain",
"table.insert",
{
"call",
{
tmp_name,
value
}
}
}
end
_with_0:stm(node, action) _with_0:stm(node, action)
_with_0:stm({ "return", tmp_name }) _with_0:stm({
"return",
tmp_name
})
return _with_0 return _with_0
end end
end, end,
@ -104,10 +125,12 @@ value_compile = {
local actions local actions
do do
local _with_0 = self:line() local _with_0 = self:line()
local _item_0 = node do
for _index_0=3,#_item_0 do local _item_0 = node
local action = _item_0[_index_0] for _index_0 = 3, #_item_0 do
_with_0:append(chain_item(action)) local action = _item_0[_index_0]
_with_0:append(chain_item(action))
end
end end
actions = _with_0 actions = _with_0
end end
@ -127,10 +150,12 @@ value_compile = {
end end
do do
local _with_0 = self:block("function(" .. concat(args, ", ") .. ")") local _with_0 = self:block("function(" .. concat(args, ", ") .. ")")
local _item_0 = args do
for _index_0=1,#_item_0 do local _item_0 = args
local name = _item_0[_index_0] for _index_0 = 1, #_item_0 do
_with_0:put_name(name) local name = _item_0[_index_0]
_with_0:put_name(name)
end
end end
_with_0:ret_stms(block) _with_0:ret_stms(block)
return _with_0 return _with_0
@ -146,7 +171,11 @@ value_compile = {
if #tuple == 2 then if #tuple == 2 then
local key, value = unpack(tuple) local key, value = unpack(tuple)
if type(key) == "string" and data.lua_keywords[key] then if type(key) == "string" and data.lua_keywords[key] then
key = { "string", '"', key } key = {
"string",
'"',
key
}
end end
local assign local assign
if type(key) ~= "string" then if type(key) ~= "string" then
@ -163,18 +192,30 @@ value_compile = {
end end
end end
if items then if items then
local _item_0 = items do
for _index_0=1,#_item_0 do local _item_0 = items
local line = _item_0[_index_0] for _index_0 = 1, #_item_0 do
_with_0:add(format_line(line)) local line = _item_0[_index_0]
_with_0:add(format_line(line))
end
end end
end end
return _with_0 return _with_0
end end
end, end,
minus = function(self, node) return self:line("-", self:value(node[2])) end, minus = function(self, node)
length = function(self, node) return self:line("#", self:value(node[2])) end, return self:line("-", self:value(node[2]))
["not"] = function(self, node) return self:line("not ", self:value(node[2])) end, end,
self = function(self, node) return "self." .. self:value(node[2]) end, length = function(self, node)
self_colon = function(self, node) return "self:" .. self:value(node[2]) end return self:line("#", self:value(node[2]))
end,
["not"] = function(self, node)
return self:line("not ", self:value(node[2]))
end,
self = function(self, node)
return "self." .. self:value(node[2])
end,
self_colon = function(self, node)
return "self:" .. self:value(node[2])
end
} }