summaryrefslogtreecommitdiff
path: root/server/src/matcher/compile.lua
blob: a8cf623ba94cccf934872f6dd3048c704210ecd3 (plain)
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
local env = require 'matcher.env'
local mt = {}
mt.__index = mt

function mt:getVar(key, source)
    if key == nil then
        return nil
    end
    local var = self.env.var[key]
             or self:getField(self.env.var._ENV, key, source) -- 这里不需要用getVar来递归获取_ENV
    if not var then
        var = self:addField(self:getVar '_ENV', key, source)
    end
    if var and var.meta then
        var = var.meta
    end
    return var
end

function mt:addInfo(obj, type, source)
    if not obj then
        return nil
    end
    obj[#obj+1] = {
        type = type,
        source = source,
    }
    return obj
end

function mt:createVar(type, key, source)
    local var = {
        type = type,
        key = key,
        source = source,
        childs = {},
    }
    self.results.vars[#self.results.vars+1] = var
    return var
end

function mt:createLabel(key)
    local lbl = {
        key = key,
        type = 'label',
    }
    self.results.labels[#self.results.labels+1] = lbl
    return lbl
end

function mt:createDots()
    local dots = {
        type = 'dots',
    }
    self.results.dots[#self.results.dots+1] = dots
    return dots
end

function mt:createLocal(key, source, var)
    if key == nil then
        return nil
    end
    if not var then
        var = self:createVar('local', key, source)
        self:addInfo(var, 'local', source)
    end
    var.redefinition = self.env.var[key]
    self.env.var[key] = var
    return var
end

function mt:addField(parent, key, source)
    if parent == nil or key == nil then
        return nil
    end
    assert(source)
    local var = parent.childs[key]
    if not var then
        var = self:createVar('field', key, source)
        parent.childs[key] = var
        var.parent = parent
    end
    return var
end

function mt:getField(parent, key, source)
    if parent == nil or key == nil then
        return nil
    end
    local var
    if parent.childs then
        var = parent.childs[key]
    end
    if not var and source then
        var = self:addField(parent, key, source)
    end
    return var
end

function mt:checkName(name)
    local var = self:getVar(name[1], name)
    self:addInfo(var, 'get', name)
    return var
end

function mt:checkDots(source)
    local dots = self.env.dots
    if not dots then
        return
    end
    self:addInfo(dots, 'get', source)
end

function mt:searchCall(call, simple, i)
    local results = {}
    for i, exp in ipairs(call) do
        results[i] = self:searchExp(exp)
    end

    -- 特殊处理 setmetatable
    if i == 2 and simple[1][1] == 'setmetatable' then
        local obj = results[1]
        local metatable = results[2]
        if metatable then
            local index = self:getField(metatable, '__index')
            if obj then
                self:setMeta(obj, index)
                return obj
            else
                return index
            end
        else
            return obj
        end
    end
    return nil
end

function mt:searchSimple(simple)
    local name = simple[1]
    local var
    if name.type == 'name' then
        var = self:getVar(name[1], name)
    end
    self:searchExp(simple[1])
    for i = 2, #simple do
        local obj = simple[i]
        local tp = obj.type
        if     tp == 'call' then
            var = self:searchCall(obj, simple, i)
        elseif tp == ':' then
        elseif tp == 'name' then
            if obj.index then
                self:checkName(obj)
                var = nil
            else
                var = self:getField(var, obj[1], obj)
                self:addInfo(var, 'get', obj)
            end
        else
            if obj.index and (obj.type == 'string' or obj.type == 'number' or obj.type == 'boolean') then
                var = self:getField(var, obj[1], obj)
                self:addInfo(var, 'get', obj)
            else
                self:searchExp(obj)
                var = nil
            end
        end
    end
    return var
end

function mt:searchBinary(exp)
    self:searchExp(exp[1])
    self:searchExp(exp[2])
end

function mt:searchUnary(exp)
    self:searchExp(exp[1])
end

function mt:searchTable(exp)
    local tbl = {
        type = 'table',
        childs = {},
    }
    for _, obj in ipairs(exp) do
        if obj.type == 'pair' then
            local key, value = obj[1], obj[2]
            local res = self:searchExp(value)
            local var = self:addField(tbl, key[1], key)
            self:setTable(var, res)
            self:addInfo(var, 'set', key)
        else
            self:searchExp(obj)
        end
    end
    return tbl
end

function mt:searchExp(exp)
    local tp = exp.type
    if     tp == 'nil' then
    elseif tp == 'string' then
    elseif tp == 'boolean' then
    elseif tp == 'number' then
    elseif tp == 'name' then
        return self:checkName(exp)
    elseif tp == 'simple' then
        return self:searchSimple(exp)
    elseif tp == 'binary' then
        self:searchBinary(exp)
    elseif tp == 'unary' then
        self:searchUnary(exp)
    elseif tp == '...' then
        self:checkDots(exp)
    elseif tp == 'function' then
        self:searchFunction(exp)
    elseif tp == 'table' then
        return self:searchTable(exp)
    end
    return nil
end

function mt:searchReturn(action)
    for _, exp in ipairs(action) do
        self:searchExp(exp)
    end
end

function mt:setTable(var, tbl)
    if not var or not tbl then
        return
    end
    var.childs = tbl.childs
    for _, child in pairs(tbl.childs) do
        child.parent = var
    end
end

function mt:setMeta(var, meta)
    if not var or not meta then
        return
    end
    var.childs.meta = meta
    meta.childs.meta = var
end

function mt:markSimple(simple)
    local name = simple[1]
    local var
    if name.type == 'name' then
        var = self:getVar(name[1], name)
    end
    self:searchExp(simple[1])
    for i = 2, #simple do
        local obj = simple[i]
        local tp  = obj.type
        if     tp == 'call' then
            var = self:searchCall(obj, simple, i)
        elseif tp == ':' then
            var = self:createLocal('self', simple[i-1], self:getVar(simple[i-1][1]))
            var.disableRename = true
        elseif tp == 'name' then
            if obj.index then
                self:checkName(obj)
                var = nil
            else
                if i == #simple then
                    var = self:addField(var, obj[1], obj)
                    self:addInfo(var, 'set', obj)
                else
                    var = self:getField(var, obj[1], obj)
                    self:addInfo(var, 'get', obj)
                end
            end
        else
            if obj.index and (obj.type == 'string' or obj.type == 'number' or obj.type == 'boolean') then
                if i == #simple then
                    var = self:addField(var, obj[1], obj)
                    self:addInfo(var, 'set', obj)
                else
                    var = self:getField(var, obj[1], obj)
                    self:addInfo(var, 'get', obj)
                end
            else
                self:searchExp(obj)
                var = nil
            end
        end
    end
    return var
end

function mt:markSet(simple, tbl)
    if simple.type == 'name' then
        local var = self:getVar(simple[1], simple)
        self:addInfo(var, 'set', simple)
        self:setTable(var, tbl)
    else
        local var = self:markSimple(simple)
        self:setTable(var, tbl)
    end
end

function mt:markLocal(name, tbl)
    if name.type == 'name' then
        local str = name[1]
        -- 创建一个局部变量
        local var = self:createLocal(str, name)
        self:setTable(var, tbl)
    elseif name.type == '...' then
        local dots = self:createDots()
        self:addInfo(dots, 'local', name)
        self.env.dots = dots
    elseif name.type == ':' then
        -- 创建一个局部变量
        self:createLocal('self', name)
    end
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:markSets(action)
    local keys = action[1]
    local values = action[2]
    local results = {}
    -- 要先计算赋值
    local i = 0
    self:forList(values, function (value)
        i = i + 1
        results[i] = self:searchExp(value)
    end)
    local i = 0
    self:forList(keys, function (key)
        i = i + 1
        self:markSet(key, results[i])
    end)
end

function mt:markLocals(action)
    local keys = action[1]
    local values = action[2]
    local results = {}
    -- 要先计算赋值
    local i = 0
    self:forList(values, function (value)
        i = i + 1
        results[i] = self:searchExp(value)
    end)
    local i = 0
    self:forList(keys, function (key)
        i = i + 1
        self:markLocal(key, results[i])
    end)
end

function mt:searchIfs(action)
    for _, block in ipairs(action) do
        self.env:push()
        if block.filter then
            self:searchExp(block.filter)
        end
        self:searchActions(block)
        self.env:pop()
    end
end

function mt:searchLoop(action)
    self.env:push()
    self:markLocal(action.arg)
    self:searchExp(action.min)
    self:searchExp(action.max)
    if action.step then
        self:searchExp(action.step)
    end
    self:searchActions(action)
    self.env:pop()
end

function mt:searchIn(action)
    self:forList(action.exp, function (exp)
        self:searchExp(exp)
    end)
    self.env:push()
    self:forList(action.arg, function (arg)
        self:markLocal(arg)
    end)
    self:searchActions(action)
    self.env:pop()
end

function mt:searchDo(action)
    self.env:push()
    self:searchActions(action)
    self.env:pop()
end

function mt:searchWhile(action)
    self:searchExp(action.filter)
    self.env:push()
    self:searchActions(action)
    self.env:pop()
end

function mt:searchRepeat(action)
    self.env:push()
    self:searchActions(action)
    self:searchExp(action.filter)
    self.env:pop()
end

function mt:searchFunction(func)
    self.env:push()
    self.env:cut 'dots'
    self.env.label = {}
    if func.name then
        self:markSet(func.name)
    end
    self:forList(func.arg, function (arg)
        self:markLocal(arg)
    end)
    self:searchActions(func)
    self.env:pop()
end

function mt:searchLocalFunction(func)
    if func.name then
        self:markLocal(func.name)
    end
    self.env:push()
    self:forList(func.arg, function (arg)
        self:markLocal(arg)
    end)
    self:searchActions(func)
    self.env:pop()
end

function mt:markLabel(label)
    local str = label[1]
    if not self.env.label[str] then
        self.env.label[str] = self:createLabel(str)
    end
    self:addInfo(self.env.label[str], 'set', label)
end

function mt:searchGoTo(obj)
    local str = obj[1]
    if not self.env.label[str] then
        self.env.label[str] = self:createLabel(str)
    end
    self:addInfo(self.env.label[str], 'goto', obj)
end

function mt:searchAction(action)
    local tp = action.type
    if     tp == 'do' then
        self:searchDo(action)
    elseif tp == 'break' then
    elseif tp == 'return' then
        self:searchReturn(action)
    elseif tp == 'label' then
        self:markLabel(action)
    elseif tp == 'goto' then
        self:searchGoTo(action)
    elseif tp == 'set' then
        self:markSets(action)
    elseif tp == 'local' then
        self:markLocals(action)
    elseif tp == 'simple' then
        self:searchSimple(action)
    elseif tp == 'if' then
        self:searchIfs(action)
    elseif tp == 'loop' then
        self:searchLoop(action)
    elseif tp == 'in' then
        self:searchIn(action)
    elseif tp == 'while' then
        self:searchWhile(action)
    elseif tp == 'repeat' then
        self:searchRepeat(action)
    elseif tp == 'function' then
        self:searchFunction(action)
    elseif tp == 'localfunction' then
        self:searchLocalFunction(action)
    end
end

function mt:searchActions(actions)
    for _, action in ipairs(actions) do
        self:searchAction(action)
    end
    return nil
end

return function (ast)
    if not ast then
        return nil
    end
    local searcher = setmetatable({
        env = env {
            var = {},
            usable = {}
        },
        results = {
            labels = {},
            vars = {},
            dots = {},
        }
    }, mt)
    searcher.env.label = {}
    searcher:createLocal('_ENV', { start = -1, finish = -1 })
    searcher:searchActions(ast)

    return searcher.results
end