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

---@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
            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

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