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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
|
local env = require 'matcher.env'
local library = require 'matcher.library'
local DefaultSource = { start = 0, finish = 0 }
local mt = {}
mt.__index = mt
function mt:createLocal(key, source, value)
local loc = {
type = 'local',
key = key,
source = source or DefaultSource,
}
self.scope.locals[key] = loc
self.results.locals[#self.results.locals+1] = loc
self:addInfo(loc, 'local', source)
self:setValue(loc, value, source)
return loc
end
function mt:addInfo(obj, type, source)
if source and not source.start then
error('Miss start: ' .. table.dump(source))
end
obj[#obj+1] = {
type = type,
source = source or DefaultSource,
}
return obj
end
function mt:createDots(source)
local dots = {
type = 'dots',
source = source or DefaultSource,
}
self.chunk.dots = dots
return dots
end
function mt:buildTable(source)
local tbl = self:createValue('table', source)
if not source then
return tbl
end
local n = 0
for _, obj in ipairs(source) do
if obj.type == 'pair' then
local value = self:getExp(obj[2])
local key = obj[1]
if key.index then
local index = self:getIndex(key)
local field = self:createField(tbl, index, key)
if value.type == 'list' then
self:setValue(field, value[1], key)
else
self:setValue(field, value, key)
end
else
if key.type == 'name' then
local index = key[1]
local field = self:createField(tbl, index, key)
if value.type == 'list' then
self:setValue(field, value[1], key)
else
self:setValue(field, value, key)
end
end
end
else
local value = self:getExp(obj)
if value.type == 'list' then
for i, v in ipairs(value) do
local field = self:createField(tbl, n + i)
self:setValue(field, v)
end
break
else
n = n + 1
local field = self:createField(tbl, n)
self:setValue(field, value)
end
end
end
return tbl
end
function mt:mergeValue(a, b, mark)
if not mark then
mark = {}
end
if mark[a] or mark[b] then
return
end
mark[a] = true
mark[b] = true
self:mergeChild(a, b, mark)
for k in pairs(a) do
a[k] = nil
end
for k, v in pairs(b) do
a[k] = v
end
end
function mt:mergeField(a, b, mark)
if not mark then
mark = {}
end
for i, info in ipairs(a) do
a[i] = nil
b[#b+1] = info
end
for i, v in ipairs(b) do
a[i] = v
end
self:mergeValue(a.value, b.value, mark)
end
function mt:mergeChild(a, b, mark)
if not a.child and not b.child then
return
end
if not mark then
mark = {}
end
local child = a.child or {}
local other = b.child or {}
a.child = nil
b.child = nil
for k, v in pairs(other) do
if child[k] then
self:mergeField(v, child[k], mark)
else
child[k] = v
end
end
a.child = child
b.child = child
end
function mt:setValue(var, value, source)
assert(not value or value.type ~= 'list')
value = value or self:createValue('nil', source)
if var.value then
if value.type == 'nil' then
self:mergeChild(var.value, value)
else
self:mergeValue(var.value, value)
end
value = var.value
else
var.value = value
end
if source and source.start then
self:addInfo(var, 'set', source)
end
return value
end
function mt:getValue(var)
if not var.value then
var.value = self:createValue('nil')
end
return var.value
end
function mt:createField(pValue, name, source)
assert(pValue.type ~= 'local' and pValue.type ~= 'field')
local field = {
type = 'field',
key = name,
source = source or DefaultSource,
}
self.results.fields[#self.results.fields+1] = field
if not pValue.child then
pValue.child = {}
end
pValue.child[name] = field
return field
end
function mt:getField(pValue, name, source)
local field = (pValue.child and pValue.child[name])
or self:createField(pValue, name, source)
return field
end
function mt:buildFunction(exp, object)
local func = self:createValue('function')
func.returns = {
type = 'list',
}
func.args = {}
if not exp then
return func
end
self.scope:push()
self.chunk:push()
self.chunk:cut 'dots'
self.chunk:cut 'labels'
if object then
local var = self:createLocal('self', object.source, self:getValue(object))
func.args[1] = var
end
local stop
self:forList(exp.arg, function (arg)
if stop then
return
end
if arg.type == 'name' then
local var = self:createLocal(arg[1], arg)
func.args[#func.args+1] = var
elseif arg.type == '...' then
local dots = self:createDots(arg)
func.args[#func.args+1] = dots
stop = true
end
end)
self:doActions(exp)
self.chunk.func = func
self.results.funcs[#self.results.funcs+1] = func
self.chunk:pop()
self.scope:pop()
return func
end
function mt:forList(list, callback)
if not list then
return
end
if list.type == 'list' then
for i = 1, #list do
callback(list[i])
end
else
callback(list)
end
end
function mt:setFunctionArgs(func, values)
if not func.args then
return
end
for i, var in ipairs(func.args) do
if var.type == 'dots' then
local list = {
type = 'list',
}
if values then
for n = i, #values do
list[n-i+1] = values[n]
end
self:setValue(var, list)
else
self:setValue(var, nil)
end
break
else
if values then
self:setValue(var, values[i])
else
self:setValue(var, nil)
end
end
end
end
function mt:checkMetaIndex(value, meta)
local index = self:getField(meta, '__index')
if not index then
return
end
local indexValue = self:getValue(index)
-- TODO 支持function
self:mergeChild(value, indexValue)
end
function mt:callSetMetaTable(func, values)
self:setFunctionReturn(func, 1, values[1])
-- 检查 __index
self:checkMetaIndex(values[1], values[2])
end
function mt:call(func, values)
local lib = func.lib
if lib and lib.special then
if lib.special == 'setmetatable' then
self:callSetMetaTable(func, values)
end
end
self:setFunctionArgs(func, values)
return self:getFunctionReturns(func)
end
function mt:getCurrentFunction()
return self.chunk.func
end
function mt:setFunctionReturn(func, index, value)
func.returns[index] = value or self:createValue('nil')
end
function mt:getFunctionReturns(func)
if not func.returns then
func.returns = {
type = 'list',
[1] = self:createValue('nil'),
}
end
return func.returns
end
function mt:createValue(type, source, v)
local value = {
type = type,
source = source or DefaultSource,
value = v,
}
return value
end
function mt:getLibValue(lib)
local tp = lib.type
if not tp then
return
end
local value
if tp == 'table' then
value = self:buildTable()
elseif tp == 'function' then
value = self:buildFunction()
if lib.returns then
for i, rtn in ipairs(lib.returns) do
self:setFunctionReturn(value, i, self:getLibValue(rtn))
end
end
if lib.args then
local values = {}
for i, arg in ipairs(lib.args) do
values[i] = self:getLibValue(arg) or self:createValue('nil')
end
self:setFunctionArgs(value, values)
end
elseif tp == 'string' then
value = self:createValue('string', nil, lib.value)
elseif tp == 'boolean' then
value = self:createValue('boolean', nil, lib.value)
elseif tp == 'number' then
value = self:createValue('number', nil, lib.value)
elseif tp == 'integer' then
value = self:createValue('integer', nil, lib.value)
elseif tp == 'nil' then
value = self:createValue('nil')
else
value = self:createValue(tp)
end
return value
end
function mt:getName(name, source)
local var = self.scope.locals[name]
or self:getField(self:getValue(self.scope.locals._ENV), name, source)
return var
end
function mt:getIndex(obj)
local tp = obj.type
if tp == 'name' then
local var = self:getName(obj[1])
local value = self:getValue(var)
self:addInfo(var, 'get', obj)
return value
elseif (tp == 'string' or tp == 'number' or tp == 'boolean') then
return obj[1]
else
return self:getExp(obj)
end
end
function mt:unpackList(list)
local res = {}
if list.type == 'list' or list.type == 'call' then
for i, exp in ipairs(list) do
local value = self:getExp(exp)
if value.type == 'list' then
if i == #list then
for _, v in ipairs(value) do
res[#res+1] = v
end
else
res[#res+1] = value[1]
end
else
res[#res+1] = value
end
end
else
local value = self:getExp(list)
if value.type == 'list' then
for i, v in ipairs(value) do
res[i] = v
end
else
res[1] = value
end
end
return res
end
function mt:getSimple(simple, mode)
local value = self:getExp(simple[1])
local field
if simple[1].type == 'name' then
field = self:getName(simple[1][1])
else
field = self:createValue('nil', simple[1])
end
local object
for i = 2, #simple do
local obj = simple[i]
local tp = obj.type
if tp == 'call' then
local args = self:unpackList(obj)
if object then
table.insert(args, 1, object)
end
-- 函数的返回值一定是list
value = self:call(value, args)
if i < #simple then
value = value[1] or self:createValue('nil')
end
elseif obj.index then
local index = self:getIndex(obj)
field = self:getField(value, index, obj)
value = self:getValue(field)
if mode == 'value' or i < #simple then
if obj.start then
self:addInfo(field, 'get', obj)
end
end
else
if tp == 'name' then
field = self:getField(value, obj[1], obj)
value = self:getValue(field)
if mode == 'value' or i < #simple then
self:addInfo(field, 'get', obj)
end
elseif tp == ':' then
object = field
end
end
end
if mode == 'value' then
return value, object
elseif mode == 'field' then
return field, object
end
error('Unknow simple mode: ' .. mode)
end
function mt:getBinary(exp)
local v1 = self:getExp(exp[1])
local v2 = self:getExp(exp[2])
local op = exp.op
-- TODO 搜索元方法
if op == 'or'
or op == 'and'
or op == '<='
or op == '>='
or op == '<'
or op == '>'
or op == '~='
or op == '=='
then
return self:createValue('boolean')
elseif op == '|'
or op == '~'
or op == '&'
or op == '<<'
or op == '>>'
or op == '//'
then
return self:createValue('integer')
elseif op == '..' then
return self:createValue('string')
elseif op == '+'
or op == '-'
or op == '*'
or op == '/'
or op == '^'
or op == '%'
then
return self:createValue('number')
end
return nil
end
function mt:getUnary(exp)
local v1 = self:getExp(exp[1])
local op = exp.op
-- TODO 搜索元方法
if op == 'not' then
return self:createValue('boolean')
elseif op == '#' then
return self:createValue('integer')
elseif op == '-' then
return self:createValue('number')
elseif op == '~' then
return self:createValue('integer')
end
return nil
end
function mt:getDots()
if not self.chunk.dots then
self:createDots()
end
return self.chunk.dots
end
function mt:getExp(exp)
local tp = exp.type
if tp == 'nil' then
return self:createValue('nil', exp)
elseif tp == 'string' then
return self:createValue('string', exp, exp[1])
elseif tp == 'boolean' then
return self:createValue('boolean', exp, exp[1])
elseif tp == 'number' then
return self:createValue('number', exp, exp[1])
elseif tp == 'name' then
local var = self:getName(exp[1], exp)
local value = self:getValue(var)
self:addInfo(var, 'get', exp)
return value
elseif tp == 'simple' then
return self:getSimple(exp, 'value')
elseif tp == 'binary' then
return self:getBinary(exp)
elseif tp == 'unary' then
return self:getUnary(exp)
elseif tp == '...' then
-- TODO 传递不定参数
local var = self:getDots()
local value = self:getValue(var)
self:addInfo(var, 'get', exp)
return value
elseif tp == 'function' then
return self:buildFunction(exp)
elseif tp == 'table' then
return self:buildTable(exp)
end
error('Unkown exp type: ' .. tostring(tp))
end
function mt:doDo(action)
self.scope:push()
self:doActions(action)
self.scope:pop()
end
function mt:doReturn(action)
for i, exp in ipairs(action) do
local value = self:getExp(exp)
self:setFunctionReturn(self:getCurrentFunction(), i, value)
end
end
function mt:createLabel(action)
local name = action[1]
if not self.chunk.labels[name] then
local label = {
type = 'label',
key = name,
}
self.chunk.labels[name] = label
self.results.labels[#self.results.labels+1] = label
end
return self.chunk.labels[name]
end
function mt:doSet(action)
if not action[2] then
return
end
-- 要先计算值
local values = self:unpackList(action[2])
self:forList(action[1], function (key)
local value = table.remove(values, 1)
if key.type == 'name' then
local var = self:getName(key[1], key)
self:setValue(var, value, key)
elseif key.type == 'simple' then
local field = self:getSimple(key, 'field')
self:setValue(field, value, key[#key])
end
end)
end
function mt:doLocal(action)
local values
if action[2] then
values = self:unpackList(action[2])
end
self:forList(action[1], function (key)
local value
if values then
value = table.remove(values, 1)
end
self:createLocal(key[1], key, value)
end)
end
function mt:doIf(action)
for _, block in ipairs(action) do
self.scope:push()
if block.filter then
self:getExp(block.filter)
end
self:doActions(block)
self.scope:pop()
end
end
function mt:doLoop(action)
self.scope:push()
local min = self:getExp(action.min)
self:getExp(action.max)
if action.step then
self:getExp(action.step)
end
local arg = self:createLocal(action.arg[1], action.arg, min)
self:doActions(action)
self.scope:pop()
end
function mt:doIn(action)
self.scope:push()
local func
local list = {
type = 'list'
}
if action.exp.type == 'list' then
func = self:getExp(action.exp[1])
for i = 2, #action.exp do
list[i-1] = action.exp[i]
end
else
func = self:getExp(action.exp)
end
local args = self:unpackList(list)
local values = self:call(func, args)
self:forList(action.arg, function (arg)
local value = table.remove(values, 1)
local var = self:createLocal(arg[1], arg, value)
end)
self:doActions(action)
self.scope:pop()
end
function mt:doWhile(action)
self.scope:push()
self:getExp(action.filter)
self:doActions(action)
self.scope:pop()
end
function mt:doRepeat(action)
self.scope:push()
self:doActions(action)
self:getExp(action.filter)
self.scope:pop()
end
function mt:doFunction(action)
local name = action.name
local var, object
local source
if name.type == 'simple' then
var, object = self:getSimple(name, 'field')
source = name[#name]
else
var = self:getName(name[1], name)
source = name
end
local func = self:buildFunction(action, object)
self:setValue(var, func, source)
end
function mt:doLocalFunction(action)
local name = action.name
local var = self:createLocal(name[1], name)
local func = self:buildFunction(action)
self:setValue(var, func, name)
end
function mt:doAction(action)
local tp = action.type
if tp == 'do' then
self:doDo(action)
elseif tp == 'break' then
elseif tp == 'return' then
self:doReturn(action)
elseif tp == 'label' then
local label = self:createLabel(action)
self:addInfo(label, 'set', action)
elseif tp == 'goto' then
local label = self:createLabel(action)
self:addInfo(label, 'goto', action)
elseif tp == 'set' then
self:doSet(action)
elseif tp == 'local' then
self:doLocal(action)
elseif tp == 'simple' then
self:getSimple(action, 'value')
elseif tp == 'if' then
self:doIf(action)
elseif tp == 'loop' then
self:doLoop(action)
elseif tp == 'in' then
self:doIn(action)
elseif tp == 'while' then
self:doWhile(action)
elseif tp == 'repeat' then
self:doRepeat(action)
elseif tp == 'function' then
self:doFunction(action)
elseif tp == 'localfunction' then
self:doLocalFunction(action)
end
end
function mt:doActions(actions)
for _, action in ipairs(actions) do
self:doAction(action)
end
end
function mt:createEnvironment()
-- 整个文件是一个函数
self.chunk.func = self:buildFunction()
-- 隐藏的上值`_ENV`
local parent = self:createLocal('_ENV')
-- 隐藏的参数`...`
self:createDots()
local pValue = self:setValue(parent, self:buildTable())
-- 设置全局变量
for name, info in pairs(library.global) do
local field = self:createField(pValue, name)
if info.lib then
local value = self:getLibValue(info.lib)
value = self:setValue(field, value)
value.lib = info.lib
end
if info.child then
local fValue = self:getValue(field)
for fname, flib in pairs(info.child) do
local ffield = self:createField(fValue, fname)
local value = self:getLibValue(flib)
value = self:setValue(ffield, value)
value.lib = flib
end
end
end
end
local function compile(ast)
local vm = setmetatable({
scope = env {
locals = {},
},
chunk = env {
labels = {},
},
results = {
locals = {},
fields = {},
labels = {},
funcs = {},
},
}, mt)
-- 创建初始环境
vm:createEnvironment()
-- 执行代码
vm:doActions(ast)
return vm
end
return function (ast)
if not ast then
return nil
end
local suc, res = xpcall(compile, log.error, ast)
if not suc then
return nil
end
return res
end
|