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

---@class vm.node.union
local mt = {}
mt.__index = mt
mt.type = 'union'

---@param node   vm.node
function mt:merge(node)
    if not node then
        return
    end
    if node.type == 'union' then
        for _, c in ipairs(node) do
            if not self[c] then
                self[c]       = true
                self[#self+1] = c
            end
        end
    else
        if not self[node] then
            self[node]    = true
            self[#self+1] = node
        end
    end
end

---@param source parser.object
function mt:subscribeLocal(source)
    for _, c in ipairs(self) do
        localMgr.subscribeLocal(source, c)
        if c.type == 'cross' then
            c:subscribeLocal(source)
        end
    end
end

function mt:eachNode()
    local i = 0
    return function ()
        i = i + 1
        return self[i]
    end
end

---@param me   parser.object
---@param node vm.node
---@return vm.node.union
return function (me, node)
    local union = setmetatable({
        [1] = me,
    }, mt)
    union:merge(node)
    return union
end