From d699b4cac83b81b023323076b74d3d03b6b13ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 1 Apr 2022 17:54:19 +0800 Subject: update --- script/vm/compiler.lua | 13 ++++++++++--- script/vm/field.lua | 14 +++++++++++++- script/vm/global-manager.lua | 20 ++++++++++++++++++++ script/vm/local-id.lua | 1 + 4 files changed, 44 insertions(+), 4 deletions(-) (limited to 'script/vm') diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 158fdc36..6f542367 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -32,9 +32,16 @@ local searchFieldSwitch = util.switch() ---@param node vm.node.global : call(function (node, key, pushResult) if node.cate == 'variable' then - local global = globalMgr.getGlobal('variable', node.name, key) - if global then - pushResult(global) + if key then + local global = globalMgr.getGlobal('variable', node.name, key) + if global then + pushResult(global) + end + else + local globals = globalMgr.getFields('variable', node.name) + for _, global in ipairs(globals) do + pushResult(global) + end end end if node.cate == 'type' then diff --git a/script/vm/field.lua b/script/vm/field.lua index 52c56e7f..563c7868 100644 --- a/script/vm/field.lua +++ b/script/vm/field.lua @@ -7,9 +7,21 @@ local localID = require 'vm.local-id' local globalMgr = require 'vm.global-manager' local nodeMgr = require 'vm.node' +local searchByNodeSwitch = util.switch() + : case 'global' + ---@param global vm.node.global + : call(function (global, pushResult) + for _, set in ipairs(global:getSets()) do + pushResult(set) + end + end) + : default(function (source, pushResult) + pushResult(source) + end) + local function searchByNode(source, pushResult) compiler.compileByParentNode(source, nil, function (field) - pushResult(field) + searchByNodeSwitch(field.type, field, pushResult) end) end diff --git a/script/vm/global-manager.lua b/script/vm/global-manager.lua index 99d1b697..fc9ff633 100644 --- a/script/vm/global-manager.lua +++ b/script/vm/global-manager.lua @@ -224,6 +224,26 @@ function m.getGlobal(cate, name, field) return m.globals[key] end +---@param cate vm.global.cate +---@param name string +---@return vm.node.global[] +function m.getFields(cate, name) + local globals = {} + local key = cate .. '|' .. name + + -- TODO: optimize + for gid, global in pairs(m.globals) do + if gid ~= key + and util.stringStartWith(gid, key) + and gid:sub(#key + 1, #key + 1) == m.ID_SPLITE + and not gid:find(m.ID_SPLITE, #key + 2) then + globals[#globals+1] = global + end + end + + return globals +end + ---@param source parser.object function m.compileObject(source) if source._globalNode ~= nil then diff --git a/script/vm/local-id.lua b/script/vm/local-id.lua index 8d530beb..06e086c7 100644 --- a/script/vm/local-id.lua +++ b/script/vm/local-id.lua @@ -172,6 +172,7 @@ function m.getFields(source) for lid, sources in pairs(root._localIDs) do if lid ~= id and util.stringStartWith(lid, id) + and lid:sub(#id + 1, #id + 1) == m.ID_SPLITE -- only one field and not lid:find(m.ID_SPLITE, #id + 2) then for _, src in ipairs(sources) do -- cgit v1.2.3