summaryrefslogtreecommitdiff
path: root/script/vm
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-06-22 16:54:29 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-06-22 16:54:29 +0800
commit72effeb8f9d9f3c04d4f5aa4f228dcd1d84f2815 (patch)
tree7dd84148241e2d68930250fc8c294ffa80506134 /script/vm
parent09512074acae34fccc60f31a8c84d022e03ddf23 (diff)
downloadlua-language-server-72effeb8f9d9f3c04d4f5aa4f228dcd1d84f2815.zip
update
Diffstat (limited to 'script/vm')
-rw-r--r--script/vm/compiler.lua24
-rw-r--r--script/vm/node.lua9
-rw-r--r--script/vm/type.lua10
3 files changed, 33 insertions, 10 deletions
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua
index 795113e0..2f3cc36f 100644
--- a/script/vm/compiler.lua
+++ b/script/vm/compiler.lua
@@ -1105,7 +1105,7 @@ local binarySwich = util.switch()
start = source.start,
finish = source.finish,
parent = source,
- [1] =result,
+ [1] = result,
})
else
vm.setNode(source, vm.declareGlobal('type', 'integer'))
@@ -1140,7 +1140,7 @@ local binarySwich = util.switch()
start = source.start,
finish = source.finish,
parent = source,
- [1] =result,
+ [1] = result,
})
else
if op == '+'
@@ -1149,10 +1149,15 @@ local binarySwich = util.switch()
or op == '//'
or op == '%' then
local uri = guide.getUri(source)
- if vm.getInfer(source[1]):view(uri) == 'integer'
- and vm.getInfer(source[2]):view(uri) == 'integer' then
- vm.setNode(source, vm.declareGlobal('type', 'integer'))
- return
+ local infer1 = vm.getInfer(source[1])
+ local infer2 = vm.getInfer(source[2])
+ if infer1:hasType(uri, 'integer')
+ or infer2:hasType(uri, 'integer') then
+ if not infer1:hasType(uri, 'number')
+ and not infer2:hasType(uri, 'number') then
+ vm.setNode(source, vm.declareGlobal('type', 'integer'))
+ return
+ end
end
end
vm.setNode(source, vm.declareGlobal('type', 'number'))
@@ -1736,7 +1741,12 @@ local compilerSwitch = util.switch()
if source.op.type == '-' then
local v = vm.getNumber(source[1])
if v == nil then
- vm.setNode(source, vm.declareGlobal('type', 'number'))
+ local infer = vm.getInfer(source[1])
+ if infer:hasType(guide.getUri(source), 'integer') then
+ vm.setNode(source, vm.declareGlobal('type', 'integer'))
+ else
+ vm.setNode(source, vm.declareGlobal('type', 'number'))
+ end
return
else
vm.setNode(source, {
diff --git a/script/vm/node.lua b/script/vm/node.lua
index 61781e5f..8ed5c027 100644
--- a/script/vm/node.lua
+++ b/script/vm/node.lua
@@ -310,6 +310,7 @@ end
---@param source vm.object
---@param node vm.node | vm.object
---@param cover? boolean
+---@return vm.node
function vm.setNode(source, node, cover)
if not node then
if TEST then
@@ -324,18 +325,20 @@ function vm.setNode(source, node, cover)
if cover then
---@cast node vm.node
vm.nodeCache[source] = node
- return
+ return node
end
local me = vm.nodeCache[source]
if me then
me:merge(node)
else
if node.type == 'vm.node' then
- vm.nodeCache[source] = node:copy()
+ me = node:copy()
else
- vm.nodeCache[source] = vm.createNode(node)
+ me = vm.createNode(node)
end
+ vm.nodeCache[source] = me
end
+ return me
end
---@param source vm.object
diff --git a/script/vm/type.lua b/script/vm/type.lua
index 8e8ca96b..5735b482 100644
--- a/script/vm/type.lua
+++ b/script/vm/type.lua
@@ -250,6 +250,16 @@ function vm.canCastType(uri, defNode, refNode)
return true
end
end
+
+ if vm.isSubType(uri, refNode, 'number') then
+ -- allow `local x = 0;x = 1.0`,
+ -- but not allow `local x ---@type integer;x = 1.0`
+ if defInfer:hasType(uri, 'integer')
+ and not defNode:hasType 'integer' then
+ return true
+ end
+ end
+
if vm.isSubType(uri, refNode, defNode) then
return true
end