import done through transform, binding import also works now

This commit is contained in:
leaf corcoran 2011-11-06 22:06:39 -08:00
parent 4dd7e7dcc0
commit de3372d96e
6 changed files with 139 additions and 106 deletions

View File

@ -92,71 +92,6 @@ line_compile = {
["break"] = function(self, node)
return "break"
end,
import = function(self, node)
local _, names, source = unpack(node)
local final_names, to_bind = { }, { }
local _list_0 = names
for _index_0 = 1, #_list_0 do
local name = _list_0[_index_0]
local final
if ntype(name) == ":" then
local tmp = self:name(name[2])
to_bind[tmp] = true
final = tmp
else
final = self:name(name)
end
self:put_name(final)
insert(final_names, final)
end
local get_value
get_value = function(name)
if to_bind[name] then
return moonlib.bind(source, name)
else
return source .. "." .. name
end
end
if type(source) == "string" then
local values = (function()
local _accum_0 = { }
local _len_0 = 0
local _list_1 = final_names
for _index_0 = 1, #_list_1 do
local name = _list_1[_index_0]
_len_0 = _len_0 + 1
_accum_0[_len_0] = get_value(name)
end
return _accum_0
end)()
local line
do
local _with_0 = self:line("local ", concat(final_names, ", "), " = ")
_with_0:append_list(values, ", ")
line = _with_0
end
return line
end
self:add(self:line("local ", concat(final_names, ", ")))
do
local _with_0 = self:block("do")
source = _with_0:init_free_var("table", source)
local _list_1 = final_names
for _index_0 = 1, #_list_1 do
local name = _list_1[_index_0]
_with_0:stm({
"assign",
{
name
},
{
get_value(name)
}
})
end
return _with_0
end
end,
["if"] = function(self, node)
local cond, block = node[2], node[3]
local root

View File

@ -52,39 +52,6 @@ line_compile =
break: (node) =>
"break"
import: (node) =>
_, names, source = unpack node
final_names, to_bind = {}, {}
for name in *names
final = if ntype(name) == ":"
tmp = @name name[2]
to_bind[tmp] = true
tmp
else
@name name
@put_name final
insert final_names, final
get_value = (name) ->
if to_bind[name]
moonlib.bind source, name
else
source.."."..name
-- from constant expression, put it on one line
if type(source) == "string"
values = [get_value name for name in *final_names]
line = with @line "local ", concat(final_names, ", "), " = "
\append_list values, ", "
return line
@add @line "local ", concat(final_names, ", ")
with @block "do"
source = \init_free_var "table", source
\stm {"assign", {name}, {get_value name}} for name in *final_names
if: (node) =>
cond, block = node[2], node[3]
root = with @block @line "if ", @value(cond), " then"

View File

@ -284,7 +284,7 @@ local build_grammar = wrap(function()
InBlock = Advance * Block * PopIndent,
Import = key"import" * Ct(ImportNameList) * key"from" * Exp / mark"import",
ImportName = (sym"\\" * Ct(Cc":" * Name) + Name),
ImportName = (sym"\\" * Ct(Cc"colon_stub" * Name) + Name),
ImportNameList = ImportName * (sym"," * ImportName)^0,
NameList = Name * (sym"," * Name)^0,

View File

@ -228,6 +228,92 @@ Statement = Transformer({
exp
})
end,
import = function(node)
local _, names, source = unpack(node)
local stubs = (function()
local _accum_0 = { }
local _len_0 = 0
local _list_0 = names
for _index_0 = 1, #_list_0 do
local name = _list_0[_index_0]
local _value_0
if type(name) == "table" then
_value_0 = name
else
_value_0 = {
"dot",
name
}
end
if _value_0 ~= nil then
_len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end
end
return _accum_0
end)()
local real_names = (function()
local _accum_0 = { }
local _len_0 = 0
local _list_0 = names
for _index_0 = 1, #_list_0 do
local name = _list_0[_index_0]
local _value_0 = type(name) == "table" and name[2] or name
if _value_0 ~= nil then
_len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end
end
return _accum_0
end)()
if type(source) == "string" then
return build.assign({
names = real_names,
values = (function()
local _accum_0 = { }
local _len_0 = 0
local _list_0 = stubs
for _index_0 = 1, #_list_0 do
local stub = _list_0[_index_0]
_len_0 = _len_0 + 1
_accum_0[_len_0] = build.chain({
base = source,
stub
})
end
return _accum_0
end)()
})
else
local source_name = NameProxy("table")
return build.group({
{
"declare",
real_names
},
build["do"]({
build.assign_one(source_name, source),
build.assign({
names = real_names,
values = (function()
local _accum_0 = { }
local _len_0 = 0
local _list_0 = stubs
for _index_0 = 1, #_list_0 do
local stub = _list_0[_index_0]
_len_0 = _len_0 + 1
_accum_0[_len_0] = build.chain({
base = source_name,
stub
})
end
return _accum_0
end)()
})
})
})
end
end,
comprehension = function(node, action)
local _, exp, clauses = unpack(node)
action = action or function(exp)

View File

@ -130,6 +130,36 @@ Statement = Transformer {
error "Unknown op: "..op if not op_final
build.assign_one name, {"exp", name, op_final, exp}
import: (node) ->
_, names, source = unpack node
stubs = for name in *names
if type(name) == "table"
name
else
{"dot", name}
real_names = for name in *names
type(name) == "table" and name[2] or name
if type(source) == "string"
build.assign {
names: real_names
values: [build.chain { base: source, stub} for stub in *stubs]
}
else
source_name = NameProxy "table"
build.group {
{"declare", real_names}
build["do"] {
build.assign_one source_name, source
build.assign {
names: real_names
values: [build.chain { base: source_name, stub} for stub in *stubs]
}
}
}
comprehension: (node, action) ->
_, exp, clauses = unpack node

View File

@ -2,15 +2,25 @@ local hello = yeah.hello
local hello, world
do
local _table_0 = table["cool"]
hello = _table_0.hello
world = _table_0.world
hello, world = _table_0.hello, _table_0.world
end
local a, b, c = items.a, moon.bind(items.b, items), items.c
local a, b, c = items.a, (function()
local _base_0 = items
local _fn_0 = _base_0.b
return function(...)
return _fn_0(_base_0, ...)
end
end)(), items.c
local master, ghost
do
local _table_0 = find("mytable")
master = _table_0.master
ghost = moon.bind(_table_0.ghost, _table_0)
master, ghost = _table_0.master, (function()
local _base_0 = _table_0
local _fn_0 = _base_0.ghost
return function(...)
return _fn_0(_base_0, ...)
end
end)()
end
local yumm
a, yumm = 3434, "hello"
@ -24,7 +34,12 @@ if indent then
local okay, well
do
local _table_1 = tables[100]
okay = _table_1.okay
well = moon.bind(_table_1.well, _table_1)
okay, well = _table_1.okay, (function()
local _base_0 = _table_1
local _fn_0 = _base_0.well
return function(...)
return _fn_0(_base_0, ...)
end
end)()
end
end