Skip to content

Commit 3be7be0

Browse files
committed
extract super transform out
1 parent 8f647cc commit 3be7be0

File tree

2 files changed

+141
-130
lines changed

2 files changed

+141
-130
lines changed

moonscript/transform/class.lua

Lines changed: 79 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,80 @@ do
1313
local _obj_0 = require("moonscript.types")
1414
build, ntype, NOOP = _obj_0.build, _obj_0.ntype, _obj_0.NOOP
1515
end
16+
local transform_super
17+
transform_super = function(cls_name, block, chain)
18+
local relative_parent = {
19+
"chain",
20+
cls_name,
21+
{
22+
"dot",
23+
"__parent"
24+
}
25+
}
26+
if not (chain) then
27+
return relative_parent
28+
end
29+
local chain_tail = {
30+
unpack(chain, 3)
31+
}
32+
local head = chain_tail[1]
33+
if head == nil then
34+
return relative_parent
35+
end
36+
local new_chain = relative_parent
37+
local _exp_0 = head[1]
38+
if "call" == _exp_0 then
39+
local calling_name = block:get("current_method")
40+
assert(calling_name, "missing calling name")
41+
chain_tail[1] = {
42+
"call",
43+
{
44+
"self",
45+
unpack(head[2])
46+
}
47+
}
48+
if ntype(calling_name) == "key_literal" then
49+
insert(new_chain, {
50+
"dot",
51+
calling_name[2]
52+
})
53+
else
54+
insert(new_chain, {
55+
"index",
56+
calling_name
57+
})
58+
end
59+
elseif "colon" == _exp_0 then
60+
local call = chain_tail[2]
61+
if call and call[1] == "call" then
62+
chain_tail[1] = {
63+
"dot",
64+
head[2]
65+
}
66+
chain_tail[2] = {
67+
"call",
68+
{
69+
"self",
70+
unpack(call[2])
71+
}
72+
}
73+
end
74+
end
75+
for _index_0 = 1, #chain_tail do
76+
local item = chain_tail[_index_0]
77+
insert(new_chain, item)
78+
end
79+
return new_chain
80+
end
1681
local super_scope
17-
super_scope = function(value, key)
82+
super_scope = function(value, t, key)
1883
local prev_method
1984
return {
2085
"scoped",
2186
Run(function(self)
2287
prev_method = self:get("current_method")
23-
return self:set("current_method", key)
88+
self:set("current_method", key)
89+
return self:set("super", t)
2490
end),
2591
value,
2692
Run(function(self)
@@ -33,6 +99,14 @@ return function(self, node, ret, parent_assign)
3399
if parent_val == "" then
34100
parent_val = nil
35101
end
102+
local parent_cls_name = NameProxy("parent")
103+
local base_name = NameProxy("base")
104+
local self_name = NameProxy("self")
105+
local cls_name = NameProxy("class")
106+
local cls_super
107+
cls_super = function(...)
108+
return transform_super(cls_name, ...)
109+
end
36110
local statements = { }
37111
local properties = { }
38112
for _index_0 = 1, #body do
@@ -70,7 +144,7 @@ return function(self, node, ret, parent_assign)
70144
key, val = tuple[1], tuple[2]
71145
_value_0 = {
72146
key,
73-
super_scope(val, key)
147+
super_scope(val, cls_super, key)
74148
}
75149
end
76150
_accum_0[_len_0] = _value_0
@@ -83,10 +157,6 @@ return function(self, node, ret, parent_assign)
83157
end
84158
properties = _accum_0
85159
end
86-
local parent_cls_name = NameProxy("parent")
87-
local base_name = NameProxy("base")
88-
local self_name = NameProxy("self")
89-
local cls_name = NameProxy("class")
90160
if not (constructor) then
91161
if parent_val then
92162
constructor = build.fndef({
@@ -149,7 +219,7 @@ return function(self, node, ret, parent_assign)
149219
local cls = build.table({
150220
{
151221
"__init",
152-
super_scope(constructor, {
222+
super_scope(constructor, cls_super, {
153223
"key_literal",
154224
"__init"
155225
})
@@ -303,72 +373,8 @@ return function(self, node, ret, parent_assign)
303373
local out_body = {
304374
Run(function(self)
305375
if name then
306-
self:put_name(name)
376+
return self:put_name(name)
307377
end
308-
return self:set("super", function(block, chain)
309-
local relative_parent = {
310-
"chain",
311-
cls_name,
312-
{
313-
"dot",
314-
"__parent"
315-
}
316-
}
317-
if not (chain) then
318-
return relative_parent
319-
end
320-
local chain_tail = {
321-
unpack(chain, 3)
322-
}
323-
local head = chain_tail[1]
324-
if head == nil then
325-
return relative_parent
326-
end
327-
local new_chain = relative_parent
328-
local _exp_1 = head[1]
329-
if "call" == _exp_1 then
330-
local calling_name = block:get("current_method")
331-
assert(calling_name, "missing calling name")
332-
chain_tail[1] = {
333-
"call",
334-
{
335-
"self",
336-
unpack(head[2])
337-
}
338-
}
339-
if ntype(calling_name) == "key_literal" then
340-
insert(new_chain, {
341-
"dot",
342-
calling_name[2]
343-
})
344-
else
345-
insert(new_chain, {
346-
"index",
347-
calling_name
348-
})
349-
end
350-
elseif "colon" == _exp_1 then
351-
local call = chain_tail[2]
352-
if call and call[1] == "call" then
353-
chain_tail[1] = {
354-
"dot",
355-
head[2]
356-
}
357-
chain_tail[2] = {
358-
"call",
359-
{
360-
"self",
361-
unpack(call[2])
362-
}
363-
}
364-
end
365-
end
366-
for _index_0 = 1, #chain_tail do
367-
local item = chain_tail[_index_0]
368-
insert(new_chain, item)
369-
end
370-
return new_chain
371-
end)
372378
end),
373379
{
374380
"declare",

moonscript/transform/class.moon

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,66 @@ CONSTRUCTOR_NAME = "new"
66
import insert from table
77
import build, ntype, NOOP from require "moonscript.types"
88

9-
super_scope = (value, key) ->
9+
transform_super = (cls_name, block, chain) ->
10+
relative_parent = {
11+
"chain",
12+
cls_name
13+
{"dot", "__parent"}
14+
}
15+
16+
return relative_parent unless chain
17+
18+
chain_tail = { unpack chain, 3 }
19+
head = chain_tail[1]
20+
21+
if head == nil
22+
return relative_parent
23+
24+
new_chain = relative_parent
25+
26+
switch head[1]
27+
-- calling super, inject calling name and self into chain
28+
when "call"
29+
calling_name = block\get "current_method"
30+
assert calling_name, "missing calling name"
31+
chain_tail[1] = {"call", {"self", unpack head[2]}}
32+
33+
if ntype(calling_name) == "key_literal"
34+
insert new_chain, {"dot", calling_name[2]}
35+
else
36+
insert new_chain, {"index", calling_name}
37+
38+
-- colon call on super, replace class with self as first arg
39+
when "colon"
40+
call = chain_tail[2]
41+
-- calling chain tail
42+
if call and call[1] == "call"
43+
chain_tail[1] = {
44+
"dot"
45+
head[2]
46+
}
47+
48+
chain_tail[2] = {
49+
"call"
50+
{
51+
"self"
52+
unpack call[2]
53+
}
54+
}
55+
56+
insert new_chain, item for item in *chain_tail
57+
new_chain
58+
59+
60+
super_scope = (value, t, key) ->
1061
local prev_method
1162

1263
{
1364
"scoped",
1465
Run =>
1566
prev_method = @get "current_method"
1667
@set "current_method", key
68+
@set "super", t
1769
value
1870
Run =>
1971
@set "current_method", prev_method
@@ -23,6 +75,13 @@ super_scope = (value, key) ->
2375
_, name, parent_val, body = unpack node
2476
parent_val = nil if parent_val == ""
2577

78+
parent_cls_name = NameProxy "parent"
79+
base_name = NameProxy "base"
80+
self_name = NameProxy "self"
81+
cls_name = NameProxy "class"
82+
83+
cls_super = (...) -> transform_super cls_name, ...
84+
2685
-- split apart properties and statements
2786
statements = {}
2887
properties = {}
@@ -46,12 +105,8 @@ super_scope = (value, key) ->
46105
continue
47106
else
48107
{key, val} = tuple
49-
{key, super_scope val, key}
108+
{key, super_scope val, cls_super, key}
50109

51-
parent_cls_name = NameProxy "parent"
52-
base_name = NameProxy "base"
53-
self_name = NameProxy "self"
54-
cls_name = NameProxy "class"
55110

56111
unless constructor
57112
constructor = if parent_val
@@ -91,7 +146,7 @@ super_scope = (value, key) ->
91146
{"string", '"', flattened_name}
92147

93148
cls = build.table {
94-
{"__init", super_scope constructor, {"key_literal", "__init"}}
149+
{"__init", super_scope constructor, cls_super, {"key_literal", "__init"}}
95150
{"__base", base_name}
96151
{"__name", real_name} -- "quote the string"
97152
parent_val and {"__parent", parent_cls_name} or nil
@@ -167,56 +222,6 @@ super_scope = (value, key) ->
167222
-- make sure we don't assign the class to a local inside the do
168223
@put_name name if name
169224

170-
@set "super", (block, chain) ->
171-
relative_parent = {
172-
"chain",
173-
cls_name
174-
{"dot", "__parent"}
175-
}
176-
177-
return relative_parent unless chain
178-
179-
chain_tail = { unpack chain, 3 }
180-
head = chain_tail[1]
181-
182-
if head == nil
183-
return relative_parent
184-
185-
new_chain = relative_parent
186-
187-
switch head[1]
188-
-- calling super, inject calling name and self into chain
189-
when "call"
190-
calling_name = block\get "current_method"
191-
assert calling_name, "missing calling name"
192-
chain_tail[1] = {"call", {"self", unpack head[2]}}
193-
194-
if ntype(calling_name) == "key_literal"
195-
insert new_chain, {"dot", calling_name[2]}
196-
else
197-
insert new_chain, {"index", calling_name}
198-
199-
-- colon call on super, replace class with self as first arg
200-
when "colon"
201-
call = chain_tail[2]
202-
-- calling chain tail
203-
if call and call[1] == "call"
204-
chain_tail[1] = {
205-
"dot"
206-
head[2]
207-
}
208-
209-
chain_tail[2] = {
210-
"call"
211-
{
212-
"self"
213-
unpack call[2]
214-
}
215-
}
216-
217-
insert new_chain, item for item in *chain_tail
218-
new_chain
219-
220225
{"declare", { cls_name }}
221226
{"declare_glob", "*"}
222227

0 commit comments

Comments
 (0)