rufus-lua-moon 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,110 @@
1
+ local types = require("moonscript.types")
2
+ local build, ntype, NOOP
3
+ build, ntype, NOOP = types.build, types.ntype, types.NOOP
4
+ local NameProxy
5
+ NameProxy = require("moonscript.transform.names").NameProxy
6
+ local insert
7
+ insert = table.insert
8
+ local is_singular
9
+ is_singular = function(body)
10
+ if #body ~= 1 then
11
+ return false
12
+ end
13
+ if "group" == ntype(body) then
14
+ return is_singular(body[2])
15
+ else
16
+ return body[1]
17
+ end
18
+ end
19
+ local transform_last_stm
20
+ transform_last_stm = require("moonscript.transform.statements").transform_last_stm
21
+ local Accumulator
22
+ do
23
+ local _class_0
24
+ local _base_0 = {
25
+ body_idx = {
26
+ ["for"] = 4,
27
+ ["while"] = 3,
28
+ foreach = 4
29
+ },
30
+ convert = function(self, node)
31
+ local index = self.body_idx[ntype(node)]
32
+ node[index] = self:mutate_body(node[index])
33
+ return self:wrap(node)
34
+ end,
35
+ wrap = function(self, node, group_type)
36
+ if group_type == nil then
37
+ group_type = "block_exp"
38
+ end
39
+ return build[group_type]({
40
+ build.assign_one(self.accum_name, build.table()),
41
+ build.assign_one(self.len_name, 1),
42
+ node,
43
+ group_type == "block_exp" and self.accum_name or NOOP
44
+ })
45
+ end,
46
+ mutate_body = function(self, body)
47
+ local single_stm = is_singular(body)
48
+ local val
49
+ if single_stm and types.is_value(single_stm) then
50
+ body = { }
51
+ val = single_stm
52
+ else
53
+ body = transform_last_stm(body, function(n)
54
+ if types.is_value(n) then
55
+ return build.assign_one(self.value_name, n)
56
+ else
57
+ return build.group({
58
+ {
59
+ "declare",
60
+ {
61
+ self.value_name
62
+ }
63
+ },
64
+ n
65
+ })
66
+ end
67
+ end)
68
+ val = self.value_name
69
+ end
70
+ local update = {
71
+ build.assign_one(NameProxy.index(self.accum_name, self.len_name), val),
72
+ {
73
+ "update",
74
+ self.len_name,
75
+ "+=",
76
+ 1
77
+ }
78
+ }
79
+ insert(body, build.group(update))
80
+ return body
81
+ end
82
+ }
83
+ _base_0.__index = _base_0
84
+ _class_0 = setmetatable({
85
+ __init = function(self, accum_name)
86
+ self.accum_name = NameProxy("accum")
87
+ self.value_name = NameProxy("value")
88
+ self.len_name = NameProxy("len")
89
+ end,
90
+ __base = _base_0,
91
+ __name = "Accumulator"
92
+ }, {
93
+ __index = _base_0,
94
+ __call = function(cls, ...)
95
+ local _self_0 = setmetatable({}, _base_0)
96
+ cls.__init(_self_0, ...)
97
+ return _self_0
98
+ end
99
+ })
100
+ _base_0.__class = _class_0
101
+ Accumulator = _class_0
102
+ end
103
+ local default_accumulator
104
+ default_accumulator = function(self, node)
105
+ return Accumulator():convert(node)
106
+ end
107
+ return {
108
+ Accumulator = Accumulator,
109
+ default_accumulator = default_accumulator
110
+ }
@@ -0,0 +1,486 @@
1
+ local NameProxy, LocalName
2
+ do
3
+ local _obj_0 = require("moonscript.transform.names")
4
+ NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName
5
+ end
6
+ local Run
7
+ Run = require("moonscript.transform.statements").Run
8
+ local CONSTRUCTOR_NAME = "new"
9
+ local insert
10
+ insert = table.insert
11
+ local build, ntype, NOOP
12
+ do
13
+ local _obj_0 = require("moonscript.types")
14
+ build, ntype, NOOP = _obj_0.build, _obj_0.ntype, _obj_0.NOOP
15
+ end
16
+ local unpack
17
+ unpack = require("moonscript.util").unpack
18
+ local transform_super
19
+ transform_super = function(cls_name, on_base, block, chain)
20
+ if on_base == nil then
21
+ on_base = true
22
+ end
23
+ local relative_parent = {
24
+ "chain",
25
+ cls_name,
26
+ {
27
+ "dot",
28
+ "__parent"
29
+ }
30
+ }
31
+ if not (chain) then
32
+ return relative_parent
33
+ end
34
+ local chain_tail = {
35
+ unpack(chain, 3)
36
+ }
37
+ local head = chain_tail[1]
38
+ if head == nil then
39
+ return relative_parent
40
+ end
41
+ local new_chain = relative_parent
42
+ local _exp_0 = head[1]
43
+ if "call" == _exp_0 then
44
+ if on_base then
45
+ insert(new_chain, {
46
+ "dot",
47
+ "__base"
48
+ })
49
+ end
50
+ local calling_name = block:get("current_method")
51
+ assert(calling_name, "missing calling name")
52
+ chain_tail[1] = {
53
+ "call",
54
+ {
55
+ "self",
56
+ unpack(head[2])
57
+ }
58
+ }
59
+ if ntype(calling_name) == "key_literal" then
60
+ insert(new_chain, {
61
+ "dot",
62
+ calling_name[2]
63
+ })
64
+ else
65
+ insert(new_chain, {
66
+ "index",
67
+ calling_name
68
+ })
69
+ end
70
+ elseif "colon" == _exp_0 then
71
+ local call = chain_tail[2]
72
+ if call and call[1] == "call" then
73
+ chain_tail[1] = {
74
+ "dot",
75
+ head[2]
76
+ }
77
+ chain_tail[2] = {
78
+ "call",
79
+ {
80
+ "self",
81
+ unpack(call[2])
82
+ }
83
+ }
84
+ end
85
+ end
86
+ for _index_0 = 1, #chain_tail do
87
+ local item = chain_tail[_index_0]
88
+ insert(new_chain, item)
89
+ end
90
+ return new_chain
91
+ end
92
+ local super_scope
93
+ super_scope = function(value, t, key)
94
+ local prev_method
95
+ return {
96
+ "scoped",
97
+ Run(function(self)
98
+ prev_method = self:get("current_method")
99
+ self:set("current_method", key)
100
+ return self:set("super", t)
101
+ end),
102
+ value,
103
+ Run(function(self)
104
+ return self:set("current_method", prev_method)
105
+ end)
106
+ }
107
+ end
108
+ return function(self, node, ret, parent_assign)
109
+ local _, name, parent_val, body = unpack(node)
110
+ if parent_val == "" then
111
+ parent_val = nil
112
+ end
113
+ local parent_cls_name = NameProxy("parent")
114
+ local base_name = NameProxy("base")
115
+ local self_name = NameProxy("self")
116
+ local cls_name = NameProxy("class")
117
+ local cls_instance_super
118
+ cls_instance_super = function(...)
119
+ return transform_super(cls_name, true, ...)
120
+ end
121
+ local cls_super
122
+ cls_super = function(...)
123
+ return transform_super(cls_name, false, ...)
124
+ end
125
+ local statements = { }
126
+ local properties = { }
127
+ for _index_0 = 1, #body do
128
+ local item = body[_index_0]
129
+ local _exp_0 = item[1]
130
+ if "stm" == _exp_0 then
131
+ insert(statements, item[2])
132
+ elseif "props" == _exp_0 then
133
+ for _index_1 = 2, #item do
134
+ local tuple = item[_index_1]
135
+ if ntype(tuple[1]) == "self" then
136
+ local k, v
137
+ k, v = tuple[1], tuple[2]
138
+ v = super_scope(v, cls_super, {
139
+ "key_literal",
140
+ k[2]
141
+ })
142
+ insert(statements, build.assign_one(k, v))
143
+ else
144
+ insert(properties, tuple)
145
+ end
146
+ end
147
+ end
148
+ end
149
+ local constructor
150
+ do
151
+ local _accum_0 = { }
152
+ local _len_0 = 1
153
+ for _index_0 = 1, #properties do
154
+ local _continue_0 = false
155
+ repeat
156
+ local tuple = properties[_index_0]
157
+ local key = tuple[1]
158
+ local _value_0
159
+ if key[1] == "key_literal" and key[2] == CONSTRUCTOR_NAME then
160
+ constructor = tuple[2]
161
+ _continue_0 = true
162
+ break
163
+ else
164
+ local val
165
+ key, val = tuple[1], tuple[2]
166
+ _value_0 = {
167
+ key,
168
+ super_scope(val, cls_instance_super, key)
169
+ }
170
+ end
171
+ _accum_0[_len_0] = _value_0
172
+ _len_0 = _len_0 + 1
173
+ _continue_0 = true
174
+ until true
175
+ if not _continue_0 then
176
+ break
177
+ end
178
+ end
179
+ properties = _accum_0
180
+ end
181
+ if not (constructor) then
182
+ if parent_val then
183
+ constructor = build.fndef({
184
+ args = {
185
+ {
186
+ "..."
187
+ }
188
+ },
189
+ arrow = "fat",
190
+ body = {
191
+ build.chain({
192
+ base = "super",
193
+ {
194
+ "call",
195
+ {
196
+ "..."
197
+ }
198
+ }
199
+ })
200
+ }
201
+ })
202
+ else
203
+ constructor = build.fndef()
204
+ end
205
+ end
206
+ local real_name = name or parent_assign and parent_assign[2][1]
207
+ local _exp_0 = ntype(real_name)
208
+ if "chain" == _exp_0 then
209
+ local last = real_name[#real_name]
210
+ local _exp_1 = ntype(last)
211
+ if "dot" == _exp_1 then
212
+ real_name = {
213
+ "string",
214
+ '"',
215
+ last[2]
216
+ }
217
+ elseif "index" == _exp_1 then
218
+ real_name = last[2]
219
+ else
220
+ real_name = "nil"
221
+ end
222
+ elseif "nil" == _exp_0 then
223
+ real_name = "nil"
224
+ else
225
+ local name_t = type(real_name)
226
+ local flattened_name
227
+ if name_t == "string" then
228
+ flattened_name = real_name
229
+ elseif name_t == "table" and real_name[1] == "ref" then
230
+ flattened_name = real_name[2]
231
+ else
232
+ flattened_name = error("don't know how to extract name from " .. tostring(name_t))
233
+ end
234
+ real_name = {
235
+ "string",
236
+ '"',
237
+ flattened_name
238
+ }
239
+ end
240
+ local cls = build.table({
241
+ {
242
+ "__init",
243
+ super_scope(constructor, cls_super, {
244
+ "key_literal",
245
+ "__init"
246
+ })
247
+ },
248
+ {
249
+ "__base",
250
+ base_name
251
+ },
252
+ {
253
+ "__name",
254
+ real_name
255
+ },
256
+ parent_val and {
257
+ "__parent",
258
+ parent_cls_name
259
+ } or nil
260
+ })
261
+ local class_index
262
+ if parent_val then
263
+ local class_lookup = build["if"]({
264
+ cond = {
265
+ "exp",
266
+ {
267
+ "ref",
268
+ "val"
269
+ },
270
+ "==",
271
+ "nil"
272
+ },
273
+ ["then"] = {
274
+ build.assign_one(LocalName("parent"), build.chain({
275
+ base = "rawget",
276
+ {
277
+ "call",
278
+ {
279
+ {
280
+ "ref",
281
+ "cls"
282
+ },
283
+ {
284
+ "string",
285
+ '"',
286
+ "__parent"
287
+ }
288
+ }
289
+ }
290
+ })),
291
+ build["if"]({
292
+ cond = LocalName("parent"),
293
+ ["then"] = {
294
+ build.chain({
295
+ base = LocalName("parent"),
296
+ {
297
+ "index",
298
+ "name"
299
+ }
300
+ })
301
+ }
302
+ })
303
+ }
304
+ })
305
+ insert(class_lookup, {
306
+ "else",
307
+ {
308
+ "val"
309
+ }
310
+ })
311
+ class_index = build.fndef({
312
+ args = {
313
+ {
314
+ "cls"
315
+ },
316
+ {
317
+ "name"
318
+ }
319
+ },
320
+ body = {
321
+ build.assign_one(LocalName("val"), build.chain({
322
+ base = "rawget",
323
+ {
324
+ "call",
325
+ {
326
+ base_name,
327
+ {
328
+ "ref",
329
+ "name"
330
+ }
331
+ }
332
+ }
333
+ })),
334
+ class_lookup
335
+ }
336
+ })
337
+ else
338
+ class_index = base_name
339
+ end
340
+ local cls_mt = build.table({
341
+ {
342
+ "__index",
343
+ class_index
344
+ },
345
+ {
346
+ "__call",
347
+ build.fndef({
348
+ args = {
349
+ {
350
+ "cls"
351
+ },
352
+ {
353
+ "..."
354
+ }
355
+ },
356
+ body = {
357
+ build.assign_one(self_name, build.chain({
358
+ base = "setmetatable",
359
+ {
360
+ "call",
361
+ {
362
+ "{}",
363
+ base_name
364
+ }
365
+ }
366
+ })),
367
+ build.chain({
368
+ base = "cls.__init",
369
+ {
370
+ "call",
371
+ {
372
+ self_name,
373
+ "..."
374
+ }
375
+ }
376
+ }),
377
+ self_name
378
+ }
379
+ })
380
+ }
381
+ })
382
+ cls = build.chain({
383
+ base = "setmetatable",
384
+ {
385
+ "call",
386
+ {
387
+ cls,
388
+ cls_mt
389
+ }
390
+ }
391
+ })
392
+ local value = nil
393
+ do
394
+ local out_body = {
395
+ Run(function(self)
396
+ if name then
397
+ return self:put_name(name)
398
+ end
399
+ end),
400
+ {
401
+ "declare",
402
+ {
403
+ cls_name
404
+ }
405
+ },
406
+ {
407
+ "declare_glob",
408
+ "*"
409
+ },
410
+ parent_val and build.assign_one(parent_cls_name, parent_val) or NOOP,
411
+ build.assign_one(base_name, {
412
+ "table",
413
+ properties
414
+ }),
415
+ build.assign_one(base_name:chain("__index"), base_name),
416
+ parent_val and build.chain({
417
+ base = "setmetatable",
418
+ {
419
+ "call",
420
+ {
421
+ base_name,
422
+ build.chain({
423
+ base = parent_cls_name,
424
+ {
425
+ "dot",
426
+ "__base"
427
+ }
428
+ })
429
+ }
430
+ }
431
+ }) or NOOP,
432
+ build.assign_one(cls_name, cls),
433
+ build.assign_one(base_name:chain("__class"), cls_name),
434
+ build.group((function()
435
+ if #statements > 0 then
436
+ return {
437
+ build.assign_one(LocalName("self"), cls_name),
438
+ build.group(statements)
439
+ }
440
+ end
441
+ end)()),
442
+ parent_val and build["if"]({
443
+ cond = {
444
+ "exp",
445
+ parent_cls_name:chain("__inherited")
446
+ },
447
+ ["then"] = {
448
+ parent_cls_name:chain("__inherited", {
449
+ "call",
450
+ {
451
+ parent_cls_name,
452
+ cls_name
453
+ }
454
+ })
455
+ }
456
+ }) or NOOP,
457
+ build.group((function()
458
+ if name then
459
+ return {
460
+ build.assign_one(name, cls_name)
461
+ }
462
+ end
463
+ end)()),
464
+ (function()
465
+ if ret then
466
+ return ret(cls_name)
467
+ end
468
+ end)()
469
+ }
470
+ value = build.group({
471
+ build.group((function()
472
+ if ntype(name) == "value" then
473
+ return {
474
+ build.declare({
475
+ names = {
476
+ name
477
+ }
478
+ })
479
+ }
480
+ end
481
+ end)()),
482
+ build["do"](out_body)
483
+ })
484
+ end
485
+ return value
486
+ end