summaryrefslogtreecommitdiff
path: root/script/vm/node.lua
diff options
context:
space:
mode:
authorsumneko <sumneko@hotmail.com>2022-03-05 21:44:25 +0800
committersumneko <sumneko@hotmail.com>2022-03-05 21:44:25 +0800
commitaa6703f8e866cbc2cb50740000ad6ca1e46dd860 (patch)
treef8a79bd67aa5b0362d6dd13d84b27c43bd92def5 /script/vm/node.lua
parente556621ca31bc80c78048b82412cb46bdb3a085a (diff)
downloadlua-language-server-aa6703f8e866cbc2cb50740000ad6ca1e46dd860.zip
cleanup
Diffstat (limited to 'script/vm/node.lua')
-rw-r--r--script/vm/node.lua56
1 files changed, 56 insertions, 0 deletions
diff --git a/script/vm/node.lua b/script/vm/node.lua
new file mode 100644
index 00000000..79a4d2cb
--- /dev/null
+++ b/script/vm/node.lua
@@ -0,0 +1,56 @@
+local union = require 'vm.union'
+
+---@alias vm.node parser.object | vm.node.union | vm.node.global | vm.node.generic
+
+local m = {}
+
+---@type table<parser.object, vm.node>
+m.nodeCache = {}
+
+---@param a vm.node
+---@param b vm.node
+function m.mergeNode(a, b)
+ if not b then
+ return a
+ end
+ if a.type == 'union' then
+ a:merge(b)
+ return a
+ end
+ return union(a, b)
+end
+
+function m.setNode(source, node)
+ if not node then
+ return
+ end
+ local me = m.nodeCache[source]
+ if not me then
+ m.nodeCache[source] = node
+ return
+ end
+ if me == node then
+ return
+ end
+ m.nodeCache[source] = m.mergeNode(me, node)
+end
+
+function m.eachNode(node)
+ if node.type == 'union' then
+ return node:eachNode()
+ end
+ local first = true
+ return function ()
+ if first then
+ first = false
+ return node
+ end
+ return nil
+ end
+end
+
+function m.clearNodeCache()
+ m.nodeCache = {}
+end
+
+return m