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
|
local guide = require 'parser.guide'
local type = type
_ENV = nil
local pushError, Root, Compile, CompileBlock, Cache, Block, GoToTag, Version, ENVMode, Value
--[[
-- value 类右字面量创建,在set get call中传递
-- obj 用 vref 表标记当前可能的 value 是哪些
Value
type -> 确定类型
literal -> 字面量对象
tag -> 特殊标记
ref -> 值的引用者
child -> 值的child
func -> 函数对象
--]]
local function createValue(data)
local id = #Value+1
Value[id] = data
data.ref = {}
return id
end
local function addValue(objID, valueID)
local obj = Root[objID]
local vref = obj.vref
if not vref then
vref = {}
obj.vref = vref
Cache[vref] = {}
end
local cache = Cache[vref]
if cache[valueID] then
return
end
cache[valueID] = true
vref[#vref+1] = valueID
local valueRef = Value[valueID].ref
valueRef[#valueRef+1] = objID
end
local function getValue(objID)
local obj = Root[objID]
local vref = obj.vref
if vref then
return vref
end
addValue(objID, createValue {})
return obj.vref
end
local function mergeValue(objID1, objID2)
local obj1 = Root[objID1]
local obj2 = Root[objID2]
local vref1 = obj1.vref
local vref2 = obj2.vref
if not vref2 then
return
end
if not vref1 then
vref1 = {}
obj1.vref = vref1
Cache[vref1] = {}
end
local cache = Cache[vref1]
for i = 1, #vref2 do
local valueID = vref2[i]
if not cache[valueID] then
cache[valueID] = true
vref1[#vref1+1] = valueID
local valueRef = Value[valueID].ref
valueRef[#valueRef+1] = objID1
end
end
end
local function setChildValue(objID, keyID, valueID)
if not valueID then
return
end
local objVref = getValue(objID)
local key = guide.getKeyName(Root[keyID])
local valueVref = getValue(valueID)
for i = 1, #objVref do
local value = Value[objVref[i]]
if not value.child then
value.child = {}
end
value.child[key] = valueVref
end
end
local function addLocalRef(nodeID, objID)
local node = Root[nodeID]
local obj = Root[objID]
if not node.ref then
node.ref = {}
end
node.ref[#node.ref+1] = objID
obj.node = nodeID
end
local vmMap = {
['nil'] = function (obj)
Root[#Root+1] = obj
return #Root
end,
['boolean'] = function (obj)
Root[#Root+1] = obj
return #Root
end,
['string'] = function (obj)
Root[#Root+1] = obj
return #Root
end,
['number'] = function (obj)
Root[#Root+1] = obj
return #Root
end,
['...'] = function (obj)
Root[#Root+1] = obj
return #Root
end,
['getname'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local loc = guide.getLocal(Root, obj, obj[1], obj.start)
if loc then
obj.type = 'getlocal'
obj.loc = Cache[loc]
if not loc.ref then
loc.ref = {}
end
loc.ref[#loc.ref+1] = id
else
obj.type = 'getglobal'
if ENVMode == '_ENV' then
local node, nodeID = guide.getLocal(Root, obj, '_ENV', obj.start)
if node then
addLocalRef(nodeID, id)
end
end
end
return id
end,
['getfield'] = function (obj)
local node = obj.node
Root[#Root+1] = obj
local id = #Root
obj.node = Compile(node, id)
return id
end,
['call'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local node = obj.node
local args = obj.args
if node then
obj.node = Compile(node, id)
end
if args then
obj.args = Compile(args, id)
end
return id
end,
['callargs'] = function (obj)
Root[#Root+1] = obj
local id = #Root
for i = 1, #obj do
local arg = obj[i]
obj[i] = Compile(arg, id)
end
return id
end,
['binary'] = function (obj)
local e1 = obj[1]
local e2 = obj[2]
Root[#Root+1] = obj
local id = #Root
obj[1] = Compile(e1, id)
obj[2] = Compile(e2, id)
return id
end,
['unary'] = function (obj)
local e = obj[1]
Root[#Root+1] = obj
local id = #Root
obj[1] = Compile(e, id)
return id
end,
['varargs'] = function (obj)
local func = guide.getParentFunction(Root, obj)
if func then
local index = guide.getFunctionVarArgs(Root, func)
if not index then
pushError {
type = 'UNEXPECT_DOTS',
start = obj.start,
finish = obj.finish,
}
end
end
Root[#Root+1] = obj
return #Root
end,
['paren'] = function (obj)
local exp = obj.exp
Root[#Root+1] = obj
local id = #Root
obj.exp = Compile(exp, id)
return id
end,
['getindex'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local node = obj.node
obj.node = Compile(node, id)
local index = obj.index
if index then
obj.index = Compile(index, id)
end
return id
end,
['setindex'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local node = obj.node
obj.node = Compile(node, id)
local index = obj.index
if index then
obj.index = Compile(index, id)
end
local value = obj.value
if value then
obj.value = Compile(value, id)
end
return id
end,
['getmethod'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local node = obj.node
local method = obj.method
obj.node = Compile(node, id)
if method then
obj.method = Compile(method, id)
end
return id
end,
['setmethod'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local node = obj.node
local method = obj.method
local value = obj.value
obj.node = Compile(node, id)
if method then
obj.method = Compile(method, id)
end
value.localself = {
type = 'local',
start = 0,
finish = 0,
method = id,
effect = obj.finish,
[1] = 'self',
}
obj.value = Compile(value, id)
return id
end,
['method'] = function (obj)
Root[#Root+1] = obj
return #Root
end,
['function'] = function (obj)
local lastBlock = Block
Block = obj
Root[#Root+1] = obj
local id = #Root
if obj.localself then
Compile(obj.localself, id)
obj.localself = nil
end
local args = obj.args
if args then
obj.args = Compile(args, id)
end
for i = 1, #obj do
local act = obj[i]
obj[i] = Compile(act, id)
end
Block = lastBlock
return id
end,
['funcargs'] = function (obj)
Root[#Root+1] = obj
local id = #Root
for i = 1, #obj do
local arg = obj[i]
obj[i] = Compile(arg, id)
end
return id
end,
['table'] = function (obj)
Root[#Root+1] = obj
local id = #Root
for i = 1, #obj do
local v = obj[i]
obj[i] = Compile(v, id)
end
return id
end,
['tablefield'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local value = obj.value
if value then
obj.value = Compile(value, id)
end
return id
end,
['tableindex'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local index = obj.index
local value = obj.value
obj.index = Compile(index, id)
obj.value = Compile(value, id)
return id
end,
['index'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local index = obj.index
obj.index = Compile(index, id)
return id
end,
['select'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local vararg = obj.vararg
if not Cache[vararg] then
obj.vararg = Compile(vararg, id)
Cache[vararg] = obj.vararg
else
obj.vararg = Cache[vararg]
if not vararg.extParent then
vararg.extParent = {}
end
vararg.extParent[#vararg.extParent+1] = id
end
return id
end,
['setname'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local value = obj.value
if value then
obj.value = Compile(value, id)
end
local loc = guide.getLocal(Root, obj, obj[1], obj.start)
if loc then
obj.type = 'setlocal'
obj.loc = Cache[loc]
if not loc.ref then
loc.ref = {}
end
loc.ref[#loc.ref+1] = id
else
obj.type = 'setglobal'
if ENVMode == '_ENV' then
local node, nodeID = guide.getLocal(Root, obj, '_ENV', obj.start)
if node then
addLocalRef(nodeID, id)
setChildValue(nodeID, id, obj.value)
end
end
end
return id
end,
['local'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local attrs = obj.attrs
if attrs then
for i = 1, #attrs do
local attr = attrs[i]
attrs[i] = Compile(attr, id)
end
end
if Block then
if not Block.locals then
Block.locals = {}
end
Block.locals[#Block.locals+1] = id
end
if obj.localfunction then
obj.localfunction = nil
Cache[obj] = id
local value = obj.value
if value then
obj.value = Compile(value, id)
end
else
local value = obj.value
if value then
obj.value = Compile(value, id)
end
Cache[obj] = id
end
return id
end,
['localattr'] = function (obj)
Root[#Root+1] = obj
return #Root
end,
['setfield'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local node = obj.node
local value = obj.value
obj.node = Compile(node, id)
obj.value = Compile(value, id)
return id
end,
['do'] = function (obj)
local lastBlock = Block
Block = obj
Root[#Root+1] = obj
local id = #Root
CompileBlock(obj, id)
Block = lastBlock
return id
end,
['return'] = function (obj)
Root[#Root+1] = obj
local id = #Root
for i = 1, #obj do
local act = obj[i]
obj[i] = Compile(act, id)
end
if Block and Block[#Block] ~= obj then
pushError {
type = 'ACTION_AFTER_RETURN',
start = obj.start,
finish = obj.finish,
}
end
return id
end,
['label'] = function (obj)
Root[#Root+1] = obj
local id = #Root
local block = guide.getBlock(Root, obj)
if block then
if not block.labels then
block.labels = {}
end
local name = obj[1]
local label = guide.getLabel(Root, block, name)
if label then
pushError {
type = 'REDEFINED_LABEL',
start = obj.start,
finish = obj.finish,
relative = {
{
label.start,
label.finish,
}
}
}
end
block.labels[name] = id
end
return id
end,
['goto'] = function (obj)
Root[#Root+1] = obj
local id = #Root
GoToTag[#GoToTag+1] = obj
return id
end,
['if'] = function (obj)
Root[#Root+1] = obj
local id = #Root
for i = 1, #obj do
local block = obj[i]
obj[i] = Compile(block, id)
end
return id
end,
['ifblock'] = function (obj)
local lastBlock = Block
Block = obj
Root[#Root+1] = obj
local id = #Root
local filter = obj.filter
obj.filter = Compile(filter, id)
CompileBlock(obj, id)
Block = lastBlock
return id
end,
['elseifblock'] = function (obj)
local lastBlock = Block
Block = obj
Root[#Root+1] = obj
local id = #Root
local filter = obj.filter
if filter then
obj.filter = Compile(filter, id)
end
CompileBlock(obj, id)
Block = lastBlock
return id
end,
['elseblock'] = function (obj)
local lastBlock = Block
Block = obj
Root[#Root+1] = obj
local id = #Root
CompileBlock(obj, id)
Block = lastBlock
return id
end,
['loop'] = function (obj)
local lastBlock = Block
Block = obj
Root[#Root+1] = obj
local id = #Root
local loc = obj.loc
local max = obj.max
local step = obj.step
if loc then
obj.loc = Compile(loc, id)
end
if max then
obj.max = Compile(max, id)
end
if step then
obj.step = Compile(step, id)
end
CompileBlock(obj, id)
Block = lastBlock
return id
end,
['in'] = function (obj)
local lastBlock = Block
Block = obj
Root[#Root+1] = obj
local id = #Root
local keys = obj.keys
for i = 1, #keys do
local loc = keys[i]
keys[i] = Compile(loc, id)
end
CompileBlock(obj, id)
Block = lastBlock
return id
end,
['while'] = function (obj)
local lastBlock = Block
Block = obj
Root[#Root+1] = obj
local id = #Root
local filter = obj.filter
obj.filter = Compile(filter, id)
CompileBlock(obj, id)
Block = lastBlock
return id
end,
['repeat'] = function (obj)
local lastBlock = Block
Block = obj
Root[#Root+1] = obj
local id = #Root
CompileBlock(obj, id)
local filter = obj.filter
obj.filter = Compile(filter, id)
Block = lastBlock
return id
end,
['break'] = function (obj)
if not guide.getBreakBlock(Root, obj) then
pushError {
type = 'BREAK_OUTSIDE',
start = obj.start,
finish = obj.finish,
}
end
Root[#Root+1] = obj
return #Root
end,
['main'] = function (obj)
Block = obj
Root[#Root+1] = obj
local id = #Root
if ENVMode == '_ENV' then
local envID = Compile({
type = 'local',
start = 0,
finish = 0,
effect = 0,
[1] = '_ENV',
}, id)
addValue(envID, createValue {
type = 'table',
tag = '_ENV',
})
end
CompileBlock(obj, id)
Block = nil
return id
end,
}
function CompileBlock(obj, parent)
for i = 1, #obj do
local act = obj[i]
local f = vmMap[act.type]
if f then
act.parent = parent
obj[i] = f(act)
end
end
end
function Compile(obj, parent)
if not obj then
return nil
end
local f = vmMap[obj.type]
if not f then
return nil
end
obj.parent = parent
return f(obj)
end
local function compileGoTo(obj)
local name = obj[1]
local label = guide.getLabel(Root, obj, name)
if not label then
pushError {
type = 'NO_VISIBLE_LABEL',
start = obj.start,
finish = obj.finish,
info = {
label = name,
}
}
return
end
-- 如果有局部变量在 goto 与 label 之间声明,
-- 并在 label 之后使用,则算作语法错误
-- 如果 label 在 goto 之前声明,那么不会有中间声明的局部变量
if obj.start > label.start then
return
end
local block = guide.getBlock(Root, obj)
local locals = block and block.locals
if not locals then
return
end
for i = 1, #locals do
local loc = Root[locals[i]]
-- 检查局部变量声明位置为 goto 与 label 之间
if loc.start < obj.start or loc.finish > label.finish then
goto CONTINUE
end
-- 检查局部变量的使用位置在 label 之后
local refs = loc.ref
if not refs then
goto CONTINUE
end
for j = 1, #refs do
local ref = Root[refs[j]]
if ref.finish > label.finish then
pushError {
type = 'JUMP_LOCAL_SCOPE',
start = obj.start,
finish = obj.finish,
info = {
loc = loc[1],
},
relative = {
{
start = label.start,
finish = label.finish,
},
{
start = loc.start,
finish = loc.finish,
}
},
}
return
end
end
::CONTINUE::
end
end
local function PostCompile()
for i = 1, #GoToTag do
compileGoTo(GoToTag[i])
end
end
return function (self, lua, mode, version)
local state, err = self:parse(lua, mode, version)
if not state then
return nil, err
end
pushError = state.pushError
Root = state.root
Version = version
if version == 'Lua 5.1' or version == 'LuaJIT' then
ENVMode = 'fenv'
else
ENVMode = '_ENV'
end
Cache = {}
GoToTag = {}
Value = {}
if type(state.ast) == 'table' then
Compile(state.ast)
end
PostCompile()
state.ast = nil
state.value = Value
Cache = nil
Value = nil
return state
end
|