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
|
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)
local loc = {
type = 'local',
key = key,
source = source or DefaultSource,
}
self.results.locals[#self.results.locals+1] = loc
self.scope.locals[key] = loc
return loc
end
function mt:addInfo(obj, type, source)
obj[#obj+1] = {
type = type,
source = source,
}
return obj
end
function mt:createDots(source)
local dots = {
type = 'dots',
source = source or DefaultSource,
}
self.func.dots = dots
return dots
end
function mt:createTable(source)
local tbl = {
type = 'table',
source = source or DefaultSource,
}
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)
self:setValue(field, value)
else
if key.type == 'name' then
local index = key[1]
local field = self:createField(tbl, index, key)
self:setValue(field, value)
end
end
else
local value = self:getExp(obj)
n = n + 1
local field = self:createField(tbl, n)
self:setValue(field, value)
end
end
return tbl
end
function mt:setValue(var, value)
assert(not value or value.type ~= 'list')
var.value = value or self:createNil(var)
return value
end
function mt:getValue(var)
if not var.value then
var.value = self:createNil()
end
return var.value
end
function mt:createField(pValue, name, source)
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:createFunction(exp)
local func = {
type = 'function',
args = {},
returns = {
type = 'list',
},
}
if exp then
local stop
self:forList(exp.args, function (arg)
if stop then
return
end
if arg.type == 'name' then
func.args[#func.args+1] = self:createLocal(arg[1], arg)
elseif arg.type == '...' then
func.args[#func.args+1] = self:createDots(arg)
stop = true
end
end)
end
self.func.func = func
self.results.funcs[#self.results.funcs+1] = func
self.newFuncs[#self.newFuncs+1] = func
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:call(func, values)
if func.used then
return self:getFunctionReturns(func)
end
func.used = true
self.func:push()
self.scope:push()
self.func:cut 'labels'
self.func:cut 'dots'
for i = 1, #func.args do
local var = func.arg[i]
if var then
if var.type == 'dots' then
local list = {
type = 'list',
}
for n = i, #values do
list[n-i+1] = values[n]
end
self:setValue(var, list)
break
else
self:setValue(var, values[i])
end
end
end
self:doActions(func)
self.func:pop()
self.scope:pop()
return self:getFunctionReturns(func)
end
function mt:getCurrentFunction()
return self.func.func
end
function mt:setFunctionReturn(index, value)
local func = self:getCurrentFunction()
func.returns[index] = value
end
function mt:getFunctionReturns(func)
if not func.returns then
func.returns = {
type = 'list',
[1] = self:createNil(),
}
end
return func.returns
end
function mt:createString(str, source)
return {
type = 'string',
source = source or DefaultSource,
value = str,
}
end
function mt:createBoolean(bool, source)
return {
type = 'boolean',
source = source or DefaultSource,
value = bool or false,
}
end
function mt:createNumber(num, source)
return {
type = 'number',
source = source or DefaultSource,
value = num or 0.0,
}
end
function mt:createInteger(int, source)
return {
type = 'integer',
source = source or DefaultSource,
value = int or 0,
}
end
function mt:createNil(source)
return {
type = 'nil',
source = source or DefaultSource,
}
end
function mt:setLib(obj, lib)
obj.lib = lib
local tp = lib.type
if not tp then
return
end
if tp == 'table' then
self:setValue(obj, self:createTable())
elseif tp == 'function' then
self:setValue(obj, self:createFunction()) -- TODO
elseif tp == 'string' then
self:setValue(obj, self:createString(lib.value))
elseif tp == 'boolean' then
self:setValue(obj, self:createBoolean(lib.value))
elseif tp == 'number' then
self:setValue(obj, self:createNumber(lib.value))
elseif tp == 'integer' then
self:setValue(obj, self:createInteger(lib.value))
end
end
function mt:getName(name, source)
local var = self.scope.locals[name]
or self:getField(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])
return self:getValue(var)
elseif (tp == 'string' or tp == 'number' or tp == 'boolean') then
return obj[1]
else
return self:getExp(obj)
end
end
function mt:getSimple(simple)
local value = self:getExp(simple[1])
local field
local object
for i = 2, #simple do
local obj = simple[i]
local tp = obj.type
if tp == 'call' then
local args = {}
if object then
args[1] = object
end
for _, arg in ipairs(obj) do
args[#args+1] = self:getExp(arg)
end
-- 函数的返回值一定是list
value = self:call(value, args)
if i < #simple then
value = value[1]
end
elseif obj.index then
local index = self:getIndex(obj)
field = self:getField(value, index, obj)
value = self:getValue(field)
else
if tp == 'name' then
field = self:getField(value, obj[1])
value = self:getValue(field)
elseif tp == ':' then
object = value
end
end
end
return value, field
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:createBoolean()
elseif op == '|'
or op == '~'
or op == '&'
or op == '<<'
or op == '>>'
or op == '//'
then
return self:createInteger()
elseif op == '..' then
return self:createString()
elseif op == '+'
or op == '-'
or op == '*'
or op == '/'
or op == '^'
or op == '%'
then
return self:createNumber()
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:createBoolean()
elseif op == '#' then
return self:createInteger()
elseif op == '-' then
return self:createNumber()
elseif op == '~' then
return self:createInteger()
end
return nil
end
function mt:getDots()
if not self.func.dots then
self:createDots()
end
return self.func.dots
end
function mt:getExp(exp)
local tp = exp.type
if tp == 'nil' then
return self:createNil(exp)
elseif tp == 'string' then
return self:createString(exp[1], exp)
elseif tp == 'boolean' then
return self:createBoolean(exp[1], exp)
elseif tp == 'number' then
return self:createNumber(exp[1], exp)
elseif tp == 'name' then
local var = self:getName(exp[1], exp)
self:addInfo(var, 'get', exp)
return self:getValue(var)
elseif tp == 'simple' then
return self:getSimple(exp)
elseif tp == 'binary' then
return self:getBinary(exp)
elseif tp == 'unary' then
return self:getUnary(exp)
elseif tp == '...' then
local var = self:getDots()
self:addInfo(var, 'get', exp)
return self:getValue(var)
elseif tp == 'function' then
return self:createFunction(exp)
elseif tp == 'table' then
return self:createTable(exp)
end
return nil
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(i, value)
end
end
function mt:createLabel(action)
local name = action[1]
if not self.func.labels[name] then
local label = {
type = 'label',
key = name,
}
self.func.labels[name] = label
self.results.labels[#self.results.labels+1] = label
end
self:addInfo(self.func.labels[name], 'set', action)
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
self:createLabel(action)
end
end
function mt:doActions(actions)
for _, action in ipairs(actions) do
self:doAction(action)
end
end
function mt:createEnvironment()
-- 整个文件是一个函数
self.func.func = self:createFunction()
-- 隐藏的上值`_ENV`
local parent = self:createLocal('_ENV')
-- 隐藏的参数`...`
self:createDots()
local pValue = self:setValue(parent, self:createTable())
-- 设置全局变量
for name, info in pairs(library.global) do
local field = self:createField(pValue, name)
if info.lib then
self:setLib(field, 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)
self:setLib(ffield, flib)
end
end
end
end
local function compile(ast)
local vm = setmetatable({
scope = env {
locals = {},
},
func = env {
labels = {},
},
results = {
locals = {},
fields = {},
labels = {},
funcs = {},
},
newFuncs = {},
}, mt)
-- 创建初始环境
vm:createEnvironment()
-- 执行代码
vm:doActions(ast)
-- 把所有没跑过的函数跑一遍(可能会创建新的函数,需要一直跑到没有新的函数为止)
while true do
local func = table.remove(vm.newFuncs, 1)
if not func then
break
end
vm:call(func)
end
return vm.results
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
|