summaryrefslogtreecommitdiff
path: root/script/vm/infer.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-03-08 19:41:06 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-03-08 19:41:06 +0800
commit256b37771a9203ab0c27d5690e35d9a1f9185465 (patch)
treeffe9fda7167f17099dd3bdcfa27ec5c4e51dadca /script/vm/infer.lua
parentdd7317a907b88cb102f45535ef19d67b47564c7f (diff)
downloadlua-language-server-256b37771a9203ab0c27d5690e35d9a1f9185465.zip
update
Diffstat (limited to 'script/vm/infer.lua')
-rw-r--r--script/vm/infer.lua52
1 files changed, 52 insertions, 0 deletions
diff --git a/script/vm/infer.lua b/script/vm/infer.lua
new file mode 100644
index 00000000..8b2cf5ee
--- /dev/null
+++ b/script/vm/infer.lua
@@ -0,0 +1,52 @@
+local util = require 'utility'
+local nodeMgr = require 'vm.node'
+
+---@class vm.infer-manager
+local m = {}
+
+local viewNodeMap = util.switch()
+ : case 'boolean'
+ : case 'string'
+ : case 'table'
+ : case 'function'
+ : case 'number'
+ : case 'integer'
+ : call(function (source)
+ return source.type
+ end)
+ : getMap()
+
+---@param node vm.node
+---@return string?
+local function viewNode(node)
+ if viewNodeMap[node.type] then
+ return viewNodeMap[node.type](node)
+ end
+end
+
+---@param source parser.object
+function m.viewType(source)
+ local compiler = require 'vm.compiler'
+ local node = compiler.compileNode(source)
+ local views = {}
+ for n in nodeMgr.eachNode(node) do
+ local view = viewNode(n)
+ if view then
+ views[view] = true
+ end
+ end
+ if views['number'] then
+ views['integer'] = nil
+ end
+ local array = {}
+ for view in pairs(views) do
+ array[#array+1] = view
+ end
+ if #array == 0 then
+ return 'unknown'
+ end
+ table.sort(array)
+ return table.concat(array, '|')
+end
+
+return m