summaryrefslogtreecommitdiff
path: root/server-beta/src/core/engineer.lua
blob: 8afbeac279eac83523780344a4bde2555d64a3d9 (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
local guide  = require 'parser.guide'
local config = require 'config'

local type         = type
local setmetatable = setmetatable
local ipairs       = ipairs

_ENV = nil

---@class engineer
local mt = {}
mt.__index = mt
mt.type = 'engineer'

--- 查找所有局部变量引用
function mt:eachRefAsLocal(obj, callback)
    callback(obj, 'local')
    if obj.ref then
        for _, ref in ipairs(obj.ref) do
            if ref.type == 'setlocal' then
                callback(ref, 'set')
            elseif ref.type == 'getlocal' then
                callback(ref, 'get')
            end
        end
    end
end

--- 查找所有引用
function mt:eachRef(obj, callback)
    if obj.type == 'local' then
        self:eachRefAsLocal(obj, callback)
    elseif obj.type == 'getlocal' or obj.type == 'setlocal' then
        self:eachRefAsLocal(obj.loc, callback)
    elseif obj.type == 'setglobal' or obj.type == 'getglobal' then
        self:eachRefAsGlobal(obj, callback)
    end
end

return function (ast)
    if not ast.vm then
        ast.vm = {}
    end
    local self = setmetatable({
        step = 0,
        ast  = ast.ast,
    }, mt)
    return self
end