-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathclass.moon
More file actions
300 lines (251 loc) · 7.14 KB
/
Copy pathclass.moon
File metadata and controls
300 lines (251 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import NameProxy, LocalName from require "moonscript.transform.names"
import Run from require "moonscript.transform.statements"
CONSTRUCTOR_NAME = "new"
import insert from table
import build, ntype, NOOP from require "moonscript.types"
import unpack from require "moonscript.util"
transform_super = (cls_name, on_base=true, block, chain) ->
relative_parent = {
"chain",
cls_name
{"dot", "__parent"}
}
return relative_parent unless chain
chain_tail = { unpack chain, 3 }
head = chain_tail[1]
if head == nil
return relative_parent
new_chain = relative_parent
switch head[1]
-- calling super, inject calling name and self into chain
when "call"
if on_base
insert new_chain, {"dot", "__base"}
calling_name = block\get "current_method"
assert calling_name, "missing calling name"
chain_tail[1] = {"call", {"self", unpack head[2]}}
if ntype(calling_name) == "key_literal"
insert new_chain, {"dot", calling_name[2]}
else
insert new_chain, {"index", calling_name}
-- colon call on super, replace class with self as first arg
when "colon"
call = chain_tail[2]
-- calling chain tail
if call and call[1] == "call"
chain_tail[1] = {
"dot"
head[2]
}
chain_tail[2] = {
"call"
{
"self"
unpack call[2]
}
}
insert new_chain, item for item in *chain_tail
new_chain
super_scope = (value, t, key) ->
local prev_method
{
"scoped",
Run =>
prev_method = @get "current_method"
@set "current_method", key
@set "super", t
value
Run =>
@set "current_method", prev_method
}
(node, ret, parent_assign) =>
name, parent_val, body = unpack node, 2
name = nil unless name -- anonymous class have `false` set for the name
parent_val = nil if parent_val == ""
parent_cls_name = NameProxy "parent"
base_name = NameProxy "base"
self_name = NameProxy "self"
cls_name = NameProxy "class"
-- super call on instance
cls_instance_super = (...) -> transform_super cls_name, true, ...
-- super call on parent class
cls_super = (...) -> transform_super cls_name, false, ...
-- split apart properties and statements
statements = {}
properties = {}
-- local declarations are hoisted to the top of the class scope so methods
-- can close over the names
hoisted_locals = {}
for item in *body
switch item[1]
when "stm"
stm = item[2]
if ntype(stm) == "declare_with_shadows"
insert hoisted_locals, stm
else
insert statements, stm
when "props"
for tuple in *item[2,]
if ntype(tuple[1]) == "self"
{k,v} = tuple
v = super_scope v, cls_super, {"key_literal", k[2]}
insert statements, build.assign_one k, v
else
insert properties, tuple
-- find constructor
local constructor
properties = for tuple in *properties
key = tuple[1]
if key[1] == "key_literal" and key[2] == CONSTRUCTOR_NAME
constructor = tuple[2]
continue
else
{key, val} = tuple
{key, super_scope val, cls_instance_super, key}
unless constructor
constructor = if parent_val
build.fndef {
args: {{"..."}}
arrow: "fat"
body: {
build.chain { base: "super", {"call", {"..."}} }
}
}
else
build.fndef!
real_name = name or parent_assign and parent_assign[2][1]
real_name = switch ntype real_name
when "chain"
last = real_name[#real_name]
switch ntype last
when "dot"
{"string", '"', last[2]}
when "index"
last[2]
else
"nil"
when "nil"
"nil"
else
name_t = type real_name
-- TODO: don't use string literal as ref
flattened_name = if name_t == "string"
real_name
elseif name_t == "table" and real_name[1] == "ref"
real_name[2]
else
error "don't know how to extract name from #{name_t}"
{"string", '"', flattened_name}
cls = build.table {
{"__init", super_scope constructor, cls_super, {"key_literal", "__init"}}
{"__base", base_name}
{"__name", real_name} -- "quote the string"
parent_val and {"__parent", parent_cls_name} or nil
}
-- looking up a name in the class object
class_index = if parent_val
class_lookup = build["if"] {
cond: { "exp", {"ref", "val"}, "==", "nil" }
then: {
build.assign_one LocalName"parent", build.chain {
base: "rawget"
{
"call", {
{"ref", "cls"}
{"string", '"', "__parent"}
}
}
}
build.if {
cond: LocalName "parent"
then: {
build.chain {
base: LocalName "parent"
{"index", "name"}
}
}
}
}
}
insert class_lookup, {"else", {"val"}}
build.fndef {
args: {{"cls"}, {"name"}}
body: {
build.assign_one LocalName"val", build.chain {
base: "rawget", {"call", {base_name, {"ref", "name"}}}
}
class_lookup
}
}
else
base_name
cls_mt = build.table {
{"__index", class_index}
{"__call", build.fndef {
args: {{"cls"}, {"..."}}
body: {
build.assign_one self_name, build.chain {
base: "setmetatable"
{"call", {"{}", base_name}}
}
build.chain {
base: "cls.__init"
{"call", {self_name, "..."}}
}
self_name
}
}}
}
cls = build.chain {
base: "setmetatable"
{"call", {cls, cls_mt}}
}
value = nil
with build
out_body = {
Run =>
-- make sure we don't assign the class to a local inside the do
@put_name name if name
{"declare", { cls_name }}
-- the parent expression is evaluated before the local shadows are
-- installed so it can reference a name a class body local overrides
parent_val and .assign_one(parent_cls_name, parent_val) or NOOP
.group if #hoisted_locals > 0 then hoisted_locals
{"declare_glob", "*"}
.assign_one base_name, {"table", properties}
.assign_one base_name\chain"__index", base_name
parent_val and .chain({
base: "setmetatable"
{"call", {
base_name,
.chain { base: parent_cls_name, {"dot", "__base"}}
}}
}) or NOOP
.assign_one cls_name, cls
.assign_one base_name\chain"__class", cls_name
.group if #statements > 0 then {
.assign_one LocalName"self", cls_name
.group statements
}
-- run the inherited callback
parent_val and .if({
cond: {"exp", parent_cls_name\chain "__inherited" }
then: {
parent_cls_name\chain "__inherited", {"call", {
parent_cls_name, cls_name
}}
}
}) or NOOP
.group if name then {
.assign_one name, cls_name
}
if ret
ret cls_name
}
value = .group {
.group if ntype(name) == "value" then {
.declare names: {name}
}
.do out_body
}
value