From f8a6a73eaa1aa7915b296870f9a3862b429c7e74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 17 Feb 2022 21:03:42 +0800 Subject: cleanup --- script/vm/getDef.lua | 16 +++-- script/vm/global-id.lua | 132 ------------------------------------------ script/vm/global-node.lua | 138 ++++++++++++++++++++++++++++++++++++++++++++ script/vm/node/compiler.lua | 7 --- script/vm/state.lua | 12 ++-- 5 files changed, 151 insertions(+), 154 deletions(-) delete mode 100644 script/vm/global-id.lua create mode 100644 script/vm/global-node.lua (limited to 'script/vm') diff --git a/script/vm/getDef.lua b/script/vm/getDef.lua index 2741ae46..3cb33e69 100644 --- a/script/vm/getDef.lua +++ b/script/vm/getDef.lua @@ -1,9 +1,10 @@ ---@class vm -local vm = require 'vm.vm' -local util = require 'utility' -local compiler = require 'vm.node.compiler' -local guide = require 'parser.guide' -local localID = require 'vm.local-id' +local vm = require 'vm.vm' +local util = require 'utility' +local compiler = require 'vm.node.compiler' +local guide = require 'parser.guide' +local localID = require 'vm.local-id' +local globalNode = require 'vm.global-node' local simpleMap @@ -110,10 +111,7 @@ end ---@param source parser.object ---@param results parser.object[] local function searchByGlobal(source, results) - if source.type == 'field' then - source = source.parent - end - local global = source._globalID + local global = globalNode.getNode(source) if not global then return end diff --git a/script/vm/global-id.lua b/script/vm/global-id.lua deleted file mode 100644 index 614e28c5..00000000 --- a/script/vm/global-id.lua +++ /dev/null @@ -1,132 +0,0 @@ -local util = require 'utility' -local guide = require 'parser.guide' -local globalBuilder = require 'vm.node.global' - ----@class parser.object ----@field _globalID vm.node.global - ----@class vm.global-id -local m = {} ----@type table -m.globals = util.defaultTable(globalBuilder) ----@type table> -m.globalSubs = util.defaultTable(function () - return {} -end) - -m.ID_SPLITE = '\x1F' - -local compilerGlobalMap = util.switch() - : case 'local' - : call(function (uri, source) - if source.tag ~= '_ENV' then - return - end - if source.ref then - for _, ref in ipairs(source.ref) do - m.compileNode(uri, ref) - end - end - end) - : case 'setglobal' - : call(function (uri, source) - local name = guide.getKeyName(source) - source._globalID = m.declareGlobal(name, uri, source) - end) - : case 'getglobal' - : call(function (uri, source) - local name = guide.getKeyName(source) - local global = m.getGlobal(name) - global:addGet(uri, source) - source._globalID = global - - local nxt = source.next - if nxt then - m.compileNode(uri, nxt) - end - end) - : case 'setfield' - ---@param uri uri - ---@param source parser.object - : call(function (uri, source) - local parent = source.node._globalID - if not parent then - return - end - local name = parent:getName() .. m.ID_SPLITE .. guide.getKeyName(source) - source._globalID = m.declareGlobal(name, uri, source) - end) - : case 'getfield' - ---@param uri uri - ---@param source parser.object - : call(function (uri, source) - local parent = source.node._globalID - if not parent then - return - end - local name = parent:getName() .. m.ID_SPLITE .. guide.getKeyName(source) - local global = m.getGlobal(name) - global:addGet(uri, source) - source._globalID = global - - local nxt = source.next - if nxt then - m.compileNode(uri, nxt) - end - end) - : getMap() - - ----@param name string ----@param uri uri ----@param source parser.object ----@return vm.node.global -function m.declareGlobal(name, uri, source) - m.globalSubs[uri][name] = true - local node = m.globals[name] - node:addSet(uri, source) - return node -end - ----@param name string ----@param uri? uri ----@return vm.node.global -function m.getGlobal(name, uri) - if uri then - m.globalSubs[uri][name] = true - end - return m.globals[name] -end - ----@param source parser.object -function m.compileNode(uri, source) - if source._globalID ~= nil then - return - end - source._globalID = false - local compiler = compilerGlobalMap[source.type] - if compiler then - compiler(uri, source) - end -end - ----@param source parser.object -function m.compileAst(source) - local uri = guide.getUri(source) - local env = guide.getENV(source) - m.compileNode(uri, env) -end - -function m.getID(source) - return source._globalID -end - -function m.dropUri(uri) - local globalSub = m.globalSubs[uri] - m.globalSubs[uri] = nil - for name in pairs(globalSub) do - m.globals[name]:dropUri(uri) - end -end - -return m diff --git a/script/vm/global-node.lua b/script/vm/global-node.lua new file mode 100644 index 00000000..107ea2e0 --- /dev/null +++ b/script/vm/global-node.lua @@ -0,0 +1,138 @@ +local util = require 'utility' +local guide = require 'parser.guide' +local globalBuilder = require 'vm.node.global' + +---@class parser.object +---@field _globalNode vm.node.global + +---@class vm.global-id +local m = {} +---@type table +m.globals = util.defaultTable(globalBuilder) +---@type table> +m.globalSubs = util.defaultTable(function () + return {} +end) + +m.ID_SPLITE = '\x1F' + +local compilerGlobalMap = util.switch() + : case 'local' + : call(function (uri, source) + if source.tag ~= '_ENV' then + return + end + if source.ref then + for _, ref in ipairs(source.ref) do + m.compileNode(uri, ref) + end + end + end) + : case 'setglobal' + : call(function (uri, source) + local name = guide.getKeyName(source) + source._globalNode = m.declareGlobal(name, uri, source) + end) + : case 'getglobal' + : call(function (uri, source) + local name = guide.getKeyName(source) + local global = m.getGlobal(name) + global:addGet(uri, source) + source._globalNode = global + + local nxt = source.next + if nxt then + m.compileNode(uri, nxt) + end + end) + : case 'setfield' + ---@param uri uri + ---@param source parser.object + : call(function (uri, source) + local parent = source.node._globalNode + if not parent then + return + end + local name = parent:getName() .. m.ID_SPLITE .. guide.getKeyName(source) + source._globalNode = m.declareGlobal(name, uri, source) + end) + : case 'getfield' + ---@param uri uri + ---@param source parser.object + : call(function (uri, source) + local parent = source.node._globalNode + if not parent then + return + end + local name = parent:getName() .. m.ID_SPLITE .. guide.getKeyName(source) + local global = m.getGlobal(name) + global:addGet(uri, source) + source._globalNode = global + + local nxt = source.next + if nxt then + m.compileNode(uri, nxt) + end + end) + : getMap() + + +---@param name string +---@param uri uri +---@param source parser.object +---@return vm.node.global +function m.declareGlobal(name, uri, source) + m.globalSubs[uri][name] = true + local node = m.globals[name] + node:addSet(uri, source) + return node +end + +---@param name string +---@param uri? uri +---@return vm.node.global +function m.getGlobal(name, uri) + if uri then + m.globalSubs[uri][name] = true + end + return m.globals[name] +end + +---@param source parser.object +function m.compileNode(uri, source) + if source._globalNode ~= nil then + return + end + source._globalNode = false + local compiler = compilerGlobalMap[source.type] + if compiler then + compiler(uri, source) + end +end + +---@param source parser.object +function m.compileAst(source) + local uri = guide.getUri(source) + local env = guide.getENV(source) + m.compileNode(uri, env) +end + +---@return vm.node.global +function m.getNode(source) + if source.type == 'field' + or source.type == 'method' then + source = source.parent + end + return source._globalNode +end + +---@param uri uri +function m.dropUri(uri) + local globalSub = m.globalSubs[uri] + m.globalSubs[uri] = nil + for name in pairs(globalSub) do + m.globals[name]:dropUri(uri) + end +end + +return m diff --git a/script/vm/node/compiler.lua b/script/vm/node/compiler.lua index 296917d9..fefe4fb0 100644 --- a/script/vm/node/compiler.lua +++ b/script/vm/node/compiler.lua @@ -2,7 +2,6 @@ local guide = require 'parser.guide' local util = require 'utility' local state = require 'vm.state' local union = require 'vm.node.union' -local globalID = require 'vm.global-id' ---@class parser.object ---@field _compiledNodes boolean @@ -15,12 +14,6 @@ local m = {} ---@alias vm.node parser.object | vm.node.union | vm.node.cross ----@param ... string ----@return string -function m.getGlobalID(...) - return table.concat({...}, m.GLOBAL_SPLITE) -end - function m.setNode(source, node) if not node then return diff --git a/script/vm/state.lua b/script/vm/state.lua index 1eddc9db..83ce6084 100644 --- a/script/vm/state.lua +++ b/script/vm/state.lua @@ -1,6 +1,6 @@ -local util = require 'utility' -local files = require 'files' -local globalID = require 'vm.global-id' +local util = require 'utility' +local files = require 'files' +local globalNode = require 'vm.global-node' ---@class vm.state local m = {} @@ -58,7 +58,7 @@ end for uri in files.eachFile() do local state = files.getState(uri) if state then - globalID.compileAst(state.ast) + globalNode.compileAst(state.ast) end end @@ -66,12 +66,12 @@ files.watch(function (ev, uri) if ev == 'update' then local state = files.getState(uri) if state then - globalID.compileAst(state.ast) + globalNode.compileAst(state.ast) end end if ev == 'remove' then m.dropUri(uri) - globalID.dropUri(uri) + globalNode.dropUri(uri) end end) -- cgit v1.2.3