summaryrefslogtreecommitdiff
path: root/script/vm/node/union.lua
diff options
context:
space:
mode:
Diffstat (limited to 'script/vm/node/union.lua')
-rw-r--r--script/vm/node/union.lua49
1 files changed, 49 insertions, 0 deletions
diff --git a/script/vm/node/union.lua b/script/vm/node/union.lua
new file mode 100644
index 00000000..a8c917d9
--- /dev/null
+++ b/script/vm/node/union.lua
@@ -0,0 +1,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