summaryrefslogtreecommitdiff
path: root/script/vm/node.lua
blob: a8a900ecff6b4924cd09d4e87d580c584b8f5f1d (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
local union      = require 'vm.union'

---@alias vm.node parser.object | vm.node.union | vm.node.global | vm.node.generic | vm.node.generic-manager

---@class vm.node-manager
local m = {}

---@type table<parser.object, vm.node>
m.nodeCache = {}

---@param a vm.node
---@param b vm.node
function m.mergeNode(a, b)
    if not b then
        return a
    end
    if a.type == 'union' then
        a:merge(b)
        return a
    end
    return union(a, b)
end

function m.setNode(source, node)
    if not node then
        return
    end
    local me = m.nodeCache[source]
    if not me then
        m.nodeCache[source] = node
        return
    end
    if me == node then
        return
    end
    m.nodeCache[source] = m.mergeNode(me, node)
end

---@return fun():vm.node
function m.eachNode(node)
    if node.type == 'union' then
        return node:eachNode()
    end
    local first = true
    return function ()
        if first then
            first = false
            return node
        end
        return nil
    end
end

function m.clearNodeCache()
    m.nodeCache = {}
end

return m