summaryrefslogtreecommitdiff
path: root/script/vm/union.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-03-02 20:51:58 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-03-02 20:51:58 +0800
commit98ec375b0d758a4bd206149aef5f9d2d84878962 (patch)
treee9dddc46773b5b536b16c5c7df0fe0662c55c4c7 /script/vm/union.lua
parentbbf4d92a166bb8676f8c7e9a5f735a81c5ed2b03 (diff)
downloadlua-language-server-98ec375b0d758a4bd206149aef5f9d2d84878962.zip
cleanup
Diffstat (limited to 'script/vm/union.lua')
-rw-r--r--script/vm/union.lua56
1 files changed, 56 insertions, 0 deletions
diff --git a/script/vm/union.lua b/script/vm/union.lua
new file mode 100644
index 00000000..f0c259df
--- /dev/null
+++ b/script/vm/union.lua
@@ -0,0 +1,56 @@
+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,
+ [me] = true,
+ }, mt)
+ union:merge(node)
+ return union
+end