summaryrefslogtreecommitdiff
path: root/server-beta/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-21 20:37:14 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-21 20:37:14 +0800
commit128e0f95772c0afc43a447c039f08f13b268ad0a (patch)
treea1a322c0d5207b72813bece37b188c219c2b51c5 /server-beta/src
parentbafe0e44914ad3b669388b94b91d9992edeb8e07 (diff)
downloadlua-language-server-128e0f95772c0afc43a447c039f08f13b268ad0a.zip
更新一些类型推断
Diffstat (limited to 'server-beta/src')
-rw-r--r--server-beta/src/vm/getValue.lua90
1 files changed, 90 insertions, 0 deletions
diff --git a/server-beta/src/vm/getValue.lua b/server-beta/src/vm/getValue.lua
index f2a227a1..6faf591e 100644
--- a/server-beta/src/vm/getValue.lua
+++ b/server-beta/src/vm/getValue.lua
@@ -452,7 +452,19 @@ local function checkValue(source)
end
end
+local function hasTypeInResults(results, type)
+ for i = 1, #results do
+ if results[i].type == type then
+ return true
+ end
+ end
+ return false
+end
+
local function inferByCall(results, source)
+ if #results ~= 0 then
+ return
+ end
if not source.parent then
return
end
@@ -469,6 +481,9 @@ local function inferByCall(results, source)
end
local function inferByGetTable(results, source)
+ if #results ~= 0 then
+ return
+ end
local next = source.next
if not next then
return
@@ -577,6 +592,79 @@ local function checkLibraryArg(source)
}
end
+local function inferByUnary(results, source)
+ if #results ~= 0 then
+ return
+ end
+ local parent = source.parent
+ if not parent or parent.type ~= 'unary' then
+ return
+ end
+ local op = parent.op
+ if op.type == '#' then
+ insert(results, {
+ type = 'string',
+ source = vm.librarySource(source)
+ })
+ insert(results, {
+ type = 'table',
+ source = vm.librarySource(source)
+ })
+ elseif op.type == '~' then
+ insert(results, {
+ type = 'integer',
+ source = vm.librarySource(source)
+ })
+ elseif op.type == '-' then
+ insert(results, {
+ type = 'number',
+ source = vm.librarySource(source)
+ })
+ end
+end
+
+local function inferByBinary(results, source)
+ if #results ~= 0 then
+ return
+ end
+ local parent = source.parent
+ if not parent or parent.type ~= 'binary' then
+ return
+ end
+ local op = parent.op
+ if op.type == '<='
+ or op.type == '>='
+ or op.type == '<'
+ or op.type == '>'
+ or op.type == '^'
+ or op.type == '/'
+ or op.type == '+'
+ or op.type == '-'
+ or op.type == '*'
+ or op.type == '%' then
+ insert(results, {
+ type = 'number',
+ source = vm.librarySource(source)
+ })
+ elseif op.type == '|'
+ or op.type == '~'
+ or op.type == '&'
+ or op.type == '<<'
+ or op.type == '>>'
+ -- 整数的可能性比较高
+ or op.type == '//' then
+ insert(results, {
+ type = 'integer',
+ source = vm.librarySource(source)
+ })
+ elseif op.type == '..' then
+ insert(results, {
+ type = 'string',
+ source = vm.librarySource(source)
+ })
+ end
+end
+
local function getValue(source)
local results = checkLiteral(source)
or checkValue(source)
@@ -593,6 +681,8 @@ local function getValue(source)
checkDef(results, source)
inferByCall(results, source)
inferByGetTable(results, source)
+ inferByUnary(results, source)
+ inferByBinary(results, source)
if #results == 0 then
return nil