Skip to content

Commit 1e84b5d

Browse files
committed
replace hoist_declarations with declare_glob in classes, fixes #65
1 parent f560952 commit 1e84b5d

File tree

4 files changed

+61
-69
lines changed

4 files changed

+61
-69
lines changed

moonscript/transform.lua

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -87,46 +87,6 @@ is_singular = function(body)
8787
return body[1]
8888
end
8989
end
90-
local find_assigns
91-
find_assigns = function(body, out)
92-
if out == nil then
93-
out = { }
94-
end
95-
local _list_0 = body
96-
for _index_0 = 1, #_list_0 do
97-
local thing = _list_0[_index_0]
98-
local _exp_0 = thing[1]
99-
if "group" == _exp_0 then
100-
find_assigns(thing[2], out)
101-
elseif "assign" == _exp_0 then
102-
table.insert(out, thing[2])
103-
end
104-
end
105-
return out
106-
end
107-
local hoist_declarations
108-
hoist_declarations = function(body)
109-
local assigns = { }
110-
local _list_0 = find_assigns(body)
111-
for _index_0 = 1, #_list_0 do
112-
local names = _list_0[_index_0]
113-
local _list_1 = names
114-
for _index_1 = 1, #_list_1 do
115-
local name = _list_1[_index_1]
116-
if type(name) == "string" then
117-
table.insert(assigns, name)
118-
end
119-
end
120-
end
121-
local idx = 1
122-
while mtype(body[idx]) == Run do
123-
idx = idx + 1
124-
end
125-
return table.insert(body, idx, {
126-
"declare",
127-
assigns
128-
})
129-
end
13090
local extract_declarations
13191
extract_declarations = function(self, body, start, out)
13292
if body == nil then
@@ -1194,6 +1154,10 @@ local Statement = Transformer({
11941154
end
11951155
end)
11961156
end),
1157+
{
1158+
"declare_glob",
1159+
"*"
1160+
},
11971161
_with_0.assign_one(parent_cls_name, parent_val == "" and "nil" or parent_val),
11981162
_with_0.assign_one(base_name, {
11991163
"table",
@@ -1261,7 +1225,6 @@ local Statement = Transformer({
12611225
end
12621226
end)()
12631227
}
1264-
hoist_declarations(out_body)
12651228
value = _with_0.group({
12661229
_with_0.group((function()
12671230
if ntype(name) == "value" then

moonscript/transform.moon

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,6 @@ is_singular = (body) ->
4444
else
4545
body[1]
4646

47-
find_assigns = (body, out={}) ->
48-
for thing in *body
49-
switch thing[1]
50-
when "group"
51-
find_assigns thing[2], out
52-
when "assign"
53-
table.insert out, thing[2] -- extract names
54-
out
55-
56-
hoist_declarations = (body) ->
57-
assigns = {}
58-
59-
-- hoist the plain old assigns
60-
for names in *find_assigns body
61-
for name in *names
62-
table.insert assigns, name if type(name) == "string"
63-
64-
-- insert after runs
65-
idx = 1
66-
while mtype(body[idx]) == Run do idx += 1
67-
68-
table.insert body, idx, {"declare", assigns}
69-
70-
7147
-- this mutates body searching for assigns
7248
extract_declarations = (body=@current_stms, start=@current_stm_i + 1, out={}) =>
7349
for i=start,#body
@@ -640,6 +616,8 @@ Statement = Transformer {
640616
else
641617
parent_cls_name
642618

619+
{"declare_glob", "*"}
620+
643621
.assign_one parent_cls_name, parent_val == "" and "nil" or parent_val
644622
.assign_one base_name, {"table", properties}
645623
.assign_one base_name\chain"__index", base_name
@@ -685,8 +663,6 @@ Statement = Transformer {
685663
ret cls_name
686664
}
687665

688-
hoist_declarations out_body
689-
690666
value = .group {
691667
.group if ntype(name) == "value" then {
692668
.declare names: {name}

tests/inputs/class.moon

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,14 @@ class Something
162162
nil
163163

164164

165+
--
166+
167+
-- hoisting
168+
class Something
169+
val = 23
170+
{:insert} = table
171+
new: => print insert, val -- prints nil 23
172+
173+
--
174+
175+
nil

tests/outputs/class.lua

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,5 +987,47 @@ do
987987
_parent_0.__inherited(_parent_0, _class_0)
988988
end
989989
Something = _class_0
990-
return _class_0
991-
end
990+
end
991+
do
992+
local val, insert
993+
local _parent_0 = nil
994+
local _base_0 = { }
995+
_base_0.__index = _base_0
996+
if _parent_0 then
997+
setmetatable(_base_0, _parent_0.__base)
998+
end
999+
local _class_0 = setmetatable({
1000+
__init = function(self)
1001+
return print(insert, val)
1002+
end,
1003+
__base = _base_0,
1004+
__name = "Something",
1005+
__parent = _parent_0
1006+
}, {
1007+
__index = function(cls, name)
1008+
local val = rawget(_base_0, name)
1009+
if val == nil and _parent_0 then
1010+
return _parent_0[name]
1011+
else
1012+
return val
1013+
end
1014+
end,
1015+
__call = function(cls, ...)
1016+
local _self_0 = setmetatable({}, _base_0)
1017+
cls.__init(_self_0, ...)
1018+
return _self_0
1019+
end
1020+
})
1021+
_base_0.__class = _class_0
1022+
local self = _class_0
1023+
val = 23
1024+
do
1025+
local _obj_0 = table
1026+
insert = _obj_0.insert
1027+
end
1028+
if _parent_0 and _parent_0.__inherited then
1029+
_parent_0.__inherited(_parent_0, _class_0)
1030+
end
1031+
Something = _class_0
1032+
end
1033+
return nil

0 commit comments

Comments
 (0)