From 72effeb8f9d9f3c04d4f5aa4f228dcd1d84f2815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 22 Jun 2022 16:54:29 +0800 Subject: update --- script/vm/compiler.lua | 24 +++++++++++++++++------- script/vm/node.lua | 9 ++++++--- script/vm/type.lua | 10 ++++++++++ 3 files changed, 33 insertions(+), 10 deletions(-) (limited to 'script/vm') 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 -- cgit v1.2.3