mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
replace hoist_declarations with declare_glob in classes, fixes #65
This commit is contained in:
parent
f56095256b
commit
1e84b5d9ff
@ -87,46 +87,6 @@ is_singular = function(body)
|
|||||||
return body[1]
|
return body[1]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local find_assigns
|
|
||||||
find_assigns = function(body, out)
|
|
||||||
if out == nil then
|
|
||||||
out = { }
|
|
||||||
end
|
|
||||||
local _list_0 = body
|
|
||||||
for _index_0 = 1, #_list_0 do
|
|
||||||
local thing = _list_0[_index_0]
|
|
||||||
local _exp_0 = thing[1]
|
|
||||||
if "group" == _exp_0 then
|
|
||||||
find_assigns(thing[2], out)
|
|
||||||
elseif "assign" == _exp_0 then
|
|
||||||
table.insert(out, thing[2])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return out
|
|
||||||
end
|
|
||||||
local hoist_declarations
|
|
||||||
hoist_declarations = function(body)
|
|
||||||
local assigns = { }
|
|
||||||
local _list_0 = find_assigns(body)
|
|
||||||
for _index_0 = 1, #_list_0 do
|
|
||||||
local names = _list_0[_index_0]
|
|
||||||
local _list_1 = names
|
|
||||||
for _index_1 = 1, #_list_1 do
|
|
||||||
local name = _list_1[_index_1]
|
|
||||||
if type(name) == "string" then
|
|
||||||
table.insert(assigns, name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local idx = 1
|
|
||||||
while mtype(body[idx]) == Run do
|
|
||||||
idx = idx + 1
|
|
||||||
end
|
|
||||||
return table.insert(body, idx, {
|
|
||||||
"declare",
|
|
||||||
assigns
|
|
||||||
})
|
|
||||||
end
|
|
||||||
local extract_declarations
|
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
|
||||||
@ -1194,6 +1154,10 @@ local Statement = Transformer({
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end),
|
end),
|
||||||
|
{
|
||||||
|
"declare_glob",
|
||||||
|
"*"
|
||||||
|
},
|
||||||
_with_0.assign_one(parent_cls_name, parent_val == "" and "nil" or parent_val),
|
_with_0.assign_one(parent_cls_name, parent_val == "" and "nil" or parent_val),
|
||||||
_with_0.assign_one(base_name, {
|
_with_0.assign_one(base_name, {
|
||||||
"table",
|
"table",
|
||||||
@ -1261,7 +1225,6 @@ local Statement = Transformer({
|
|||||||
end
|
end
|
||||||
end)()
|
end)()
|
||||||
}
|
}
|
||||||
hoist_declarations(out_body)
|
|
||||||
value = _with_0.group({
|
value = _with_0.group({
|
||||||
_with_0.group((function()
|
_with_0.group((function()
|
||||||
if ntype(name) == "value" then
|
if ntype(name) == "value" then
|
||||||
|
@ -44,30 +44,6 @@ is_singular = (body) ->
|
|||||||
else
|
else
|
||||||
body[1]
|
body[1]
|
||||||
|
|
||||||
find_assigns = (body, out={}) ->
|
|
||||||
for thing in *body
|
|
||||||
switch thing[1]
|
|
||||||
when "group"
|
|
||||||
find_assigns thing[2], out
|
|
||||||
when "assign"
|
|
||||||
table.insert out, thing[2] -- extract names
|
|
||||||
out
|
|
||||||
|
|
||||||
hoist_declarations = (body) ->
|
|
||||||
assigns = {}
|
|
||||||
|
|
||||||
-- hoist the plain old assigns
|
|
||||||
for names in *find_assigns body
|
|
||||||
for name in *names
|
|
||||||
table.insert assigns, name if type(name) == "string"
|
|
||||||
|
|
||||||
-- insert after runs
|
|
||||||
idx = 1
|
|
||||||
while mtype(body[idx]) == Run do idx += 1
|
|
||||||
|
|
||||||
table.insert body, idx, {"declare", assigns}
|
|
||||||
|
|
||||||
|
|
||||||
-- this mutates body searching for assigns
|
-- this mutates body searching for assigns
|
||||||
extract_declarations = (body=@current_stms, start=@current_stm_i + 1, out={}) =>
|
extract_declarations = (body=@current_stms, start=@current_stm_i + 1, out={}) =>
|
||||||
for i=start,#body
|
for i=start,#body
|
||||||
@ -640,6 +616,8 @@ Statement = Transformer {
|
|||||||
else
|
else
|
||||||
parent_cls_name
|
parent_cls_name
|
||||||
|
|
||||||
|
{"declare_glob", "*"}
|
||||||
|
|
||||||
.assign_one parent_cls_name, parent_val == "" and "nil" or parent_val
|
.assign_one parent_cls_name, parent_val == "" and "nil" or parent_val
|
||||||
.assign_one base_name, {"table", properties}
|
.assign_one base_name, {"table", properties}
|
||||||
.assign_one base_name\chain"__index", base_name
|
.assign_one base_name\chain"__index", base_name
|
||||||
@ -685,8 +663,6 @@ Statement = Transformer {
|
|||||||
ret cls_name
|
ret cls_name
|
||||||
}
|
}
|
||||||
|
|
||||||
hoist_declarations out_body
|
|
||||||
|
|
||||||
value = .group {
|
value = .group {
|
||||||
.group if ntype(name) == "value" then {
|
.group if ntype(name) == "value" then {
|
||||||
.declare names: {name}
|
.declare names: {name}
|
||||||
|
@ -162,3 +162,14 @@ class Something
|
|||||||
nil
|
nil
|
||||||
|
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
-- hoisting
|
||||||
|
class Something
|
||||||
|
val = 23
|
||||||
|
{:insert} = table
|
||||||
|
new: => print insert, val -- prints nil 23
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
nil
|
||||||
|
@ -987,5 +987,47 @@ do
|
|||||||
_parent_0.__inherited(_parent_0, _class_0)
|
_parent_0.__inherited(_parent_0, _class_0)
|
||||||
end
|
end
|
||||||
Something = _class_0
|
Something = _class_0
|
||||||
return _class_0
|
|
||||||
end
|
end
|
||||||
|
do
|
||||||
|
local val, insert
|
||||||
|
local _parent_0 = nil
|
||||||
|
local _base_0 = { }
|
||||||
|
_base_0.__index = _base_0
|
||||||
|
if _parent_0 then
|
||||||
|
setmetatable(_base_0, _parent_0.__base)
|
||||||
|
end
|
||||||
|
local _class_0 = setmetatable({
|
||||||
|
__init = function(self)
|
||||||
|
return print(insert, val)
|
||||||
|
end,
|
||||||
|
__base = _base_0,
|
||||||
|
__name = "Something",
|
||||||
|
__parent = _parent_0
|
||||||
|
}, {
|
||||||
|
__index = function(cls, name)
|
||||||
|
local val = rawget(_base_0, name)
|
||||||
|
if val == nil and _parent_0 then
|
||||||
|
return _parent_0[name]
|
||||||
|
else
|
||||||
|
return val
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
__call = function(cls, ...)
|
||||||
|
local _self_0 = setmetatable({}, _base_0)
|
||||||
|
cls.__init(_self_0, ...)
|
||||||
|
return _self_0
|
||||||
|
end
|
||||||
|
})
|
||||||
|
_base_0.__class = _class_0
|
||||||
|
local self = _class_0
|
||||||
|
val = 23
|
||||||
|
do
|
||||||
|
local _obj_0 = table
|
||||||
|
insert = _obj_0.insert
|
||||||
|
end
|
||||||
|
if _parent_0 and _parent_0.__inherited then
|
||||||
|
_parent_0.__inherited(_parent_0, _class_0)
|
||||||
|
end
|
||||||
|
Something = _class_0
|
||||||
|
end
|
||||||
|
return nil
|
Loading…
Reference in New Issue
Block a user