summaryrefslogtreecommitdiff
path: root/script/vm/local-manager.lua
blob: 51bafb24fa43126a291fdc6c2351ec253a737fd0 (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
local util       = require 'utility'
local guide      = require 'parser.guide'

---@class vm.local-node
local m = {}
---@type table<uri, parser.object[]>
m.locals = util.multiTable(2)
---@type table<parser.object, table<parser.object, boolean>>
m.localSubs = util.multiTable(2, function ()
    return setmetatable({}, util.MODE_K)
end)
---@type table<parser.object, boolean>
m.allLocals = {}

---@param source parser.object
function m.declareLocal(source)
    if m.allLocals[source] then
        return
    end
    m.allLocals[source] = true
    local uri = guide.getUri(source)
    local locals = m.locals[uri]
    locals[#locals+1] = source
end

---@param uri uri
function m.dropUri(uri)
    local locals = m.locals[uri]
    m.locals[uri] = nil
    for _, loc in ipairs(locals) do
        m.allLocals[loc] = nil
        local localSubs = m.localSubs[loc]
        m.localSubs[loc] = nil
        for source in pairs(localSubs) do
            source._node = nil
        end
    end
end

return m