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

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

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

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

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