From e3a0eea7fc75807037a24b630241bf62a0fe1299 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, 23 Feb 2022 18:10:22 +0800 Subject: cleanup --- script/vm/getDef.lua | 14 ++--- script/vm/global-manager.lua | 142 ++++++++++++++++++++++++++++++++++++++++++ script/vm/global-node.lua | 142 ------------------------------------------ script/vm/init.lua | 2 +- script/vm/literal-manager.lua | 57 +++++++++++++++++ script/vm/literal-node.lua | 57 ----------------- script/vm/manager.lua | 29 +++++++++ script/vm/node/compiler.lua | 14 ++--- script/vm/node/union.lua | 4 +- script/vm/state.lua | 29 --------- 10 files changed, 245 insertions(+), 245 deletions(-) create mode 100644 script/vm/global-manager.lua delete mode 100644 script/vm/global-node.lua create mode 100644 script/vm/literal-manager.lua delete mode 100644 script/vm/literal-node.lua create mode 100644 script/vm/manager.lua delete mode 100644 script/vm/state.lua (limited to 'script') diff --git a/script/vm/getDef.lua b/script/vm/getDef.lua index df180420..6cc6bf85 100644 --- a/script/vm/getDef.lua +++ b/script/vm/getDef.lua @@ -1,10 +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 globalNode = require 'vm.global-node' +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 globalMgr = require 'vm.global-manager' local simpleMap @@ -115,7 +115,7 @@ end ---@param source parser.object ---@param pushResult fun(src: parser.object) local function searchByGlobal(source, pushResult) - local global = globalNode.getNode(source) + local global = globalMgr.getNode(source) if not global then return end diff --git a/script/vm/global-manager.lua b/script/vm/global-manager.lua new file mode 100644 index 00000000..db037c32 --- /dev/null +++ b/script/vm/global-manager.lua @@ -0,0 +1,142 @@ +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-manager +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' + : case 'setmethod' + : case 'setindex' + ---@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' + : case 'getmethod' + : case 'getindex' + ---@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/global-node.lua b/script/vm/global-node.lua deleted file mode 100644 index 6235ff6b..00000000 --- a/script/vm/global-node.lua +++ /dev/null @@ -1,142 +0,0 @@ -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-node -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' - : case 'setmethod' - : case 'setindex' - ---@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' - : case 'getmethod' - : case 'getindex' - ---@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/init.lua b/script/vm/init.lua index 5a5de99e..a34b5d71 100644 --- a/script/vm/init.lua +++ b/script/vm/init.lua @@ -1,5 +1,5 @@ local vm = require 'vm.vm' -require 'vm.state' +require 'vm.manager' require 'vm.getGlobals' require 'vm.getDocs' require 'vm.getLibrary' diff --git a/script/vm/literal-manager.lua b/script/vm/literal-manager.lua new file mode 100644 index 00000000..9c1f70a8 --- /dev/null +++ b/script/vm/literal-manager.lua @@ -0,0 +1,57 @@ +local util = require 'utility' +local guide = require 'parser.guide' + +---@class vm.literal-node +local m = {} +---@type table +m.literals = util.multiTable(2) +---@type table> +m.literalSubs = util.multiTable(2, function () + return setmetatable({}, util.MODE_K) +end) +---@type table +m.allLiterals = {} + +---@param source parser.object +function m.declareLiteral(source) + if m.allLiterals[source] then + return + end + m.allLiterals[source] = true + local uri = guide.getUri(source) + local literals = m.literals[uri] + literals[#literals+1] = source +end + +---@param source parser.object +---@param node vm.node +function m.subscribeLiteral(source, node) + if not node then + return + end + if node.type == 'union' + or node.type == 'cross' then + node:subscribeLiteral(source) + return + end + if not m.allLiterals[source] then + return + end + m.literalSubs[node][source] = true +end + +---@param uri uri +function m.dropUri(uri) + local literals = m.literals[uri] + m.literals[uri] = nil + for _, literal in ipairs(literals) do + m.allLiterals[literal] = nil + local literalSubs = m.literalSubs[literal] + m.literalSubs[literal] = nil + for source in pairs(literalSubs) do + source._node = nil + end + end +end + +return m diff --git a/script/vm/literal-node.lua b/script/vm/literal-node.lua deleted file mode 100644 index 9c1f70a8..00000000 --- a/script/vm/literal-node.lua +++ /dev/null @@ -1,57 +0,0 @@ -local util = require 'utility' -local guide = require 'parser.guide' - ----@class vm.literal-node -local m = {} ----@type table -m.literals = util.multiTable(2) ----@type table> -m.literalSubs = util.multiTable(2, function () - return setmetatable({}, util.MODE_K) -end) ----@type table -m.allLiterals = {} - ----@param source parser.object -function m.declareLiteral(source) - if m.allLiterals[source] then - return - end - m.allLiterals[source] = true - local uri = guide.getUri(source) - local literals = m.literals[uri] - literals[#literals+1] = source -end - ----@param source parser.object ----@param node vm.node -function m.subscribeLiteral(source, node) - if not node then - return - end - if node.type == 'union' - or node.type == 'cross' then - node:subscribeLiteral(source) - return - end - if not m.allLiterals[source] then - return - end - m.literalSubs[node][source] = true -end - ----@param uri uri -function m.dropUri(uri) - local literals = m.literals[uri] - m.literals[uri] = nil - for _, literal in ipairs(literals) do - m.allLiterals[literal] = nil - local literalSubs = m.literalSubs[literal] - m.literalSubs[literal] = nil - for source in pairs(literalSubs) do - source._node = nil - end - end -end - -return m diff --git a/script/vm/manager.lua b/script/vm/manager.lua new file mode 100644 index 00000000..b80cfb43 --- /dev/null +++ b/script/vm/manager.lua @@ -0,0 +1,29 @@ + +local files = require 'files' +local globalManager = require 'vm.global-manager' +local literalManager = require 'vm.literal-manager' + +---@class vm.state +local m = {} +for uri in files.eachFile() do + local state = files.getState(uri) + if state then + globalManager.compileAst(state.ast) + end +end + +files.watch(function (ev, uri) + if ev == 'update' then + local state = files.getState(uri) + if state then + globalManager.compileAst(state.ast) + end + end + if ev == 'remove' then + globalManager.dropUri(uri) + literalManager.dropUri(uri) + end +end) + + +return m diff --git a/script/vm/node/compiler.lua b/script/vm/node/compiler.lua index e8595ebf..e642856a 100644 --- a/script/vm/node/compiler.lua +++ b/script/vm/node/compiler.lua @@ -1,8 +1,8 @@ -local guide = require 'parser.guide' -local util = require 'utility' -local union = require 'vm.node.union' -local localID = require 'vm.local-id' -local literalNode = require 'vm.literal-node' +local guide = require 'parser.guide' +local util = require 'utility' +local union = require 'vm.node.union' +local localID = require 'vm.local-id' +local literalMgr = require 'vm.literal-manager' ---@class parser.object ---@field _compiledNodes boolean @@ -119,7 +119,7 @@ local compilerMap = util.switch() : case 'string' : case 'function' : call(function (source) - m.setNode(source, literalNode.declareLiteral(source)) + m.setNode(source, literalMgr.declareLiteral(source)) end) : case 'local' : call(function (source) @@ -183,7 +183,7 @@ function m.compileNode(source) if compiler then compiler(source) end - literalNode.subscribeLiteral(source, source._node) + literalMgr.subscribeLiteral(source, source._node) if source._globalNode then m.setNode(source, source._globalNode) end diff --git a/script/vm/node/union.lua b/script/vm/node/union.lua index a8c917d9..a550e1a5 100644 --- a/script/vm/node/union.lua +++ b/script/vm/node/union.lua @@ -1,4 +1,4 @@ -local state = require 'vm.state' +local literalMgr = require 'vm.literal-manager' ---@class vm.node.union local mt = {} @@ -22,7 +22,7 @@ end ---@param source parser.object function mt:subscribeLiteral(source) for _, c in ipairs(self) do - state.subscribeLiteral(source, c) + literalMgr.subscribeLiteral(source, c) if c.type == 'cross' then c:subscribeLiteral(source) end diff --git a/script/vm/state.lua b/script/vm/state.lua deleted file mode 100644 index 901cf081..00000000 --- a/script/vm/state.lua +++ /dev/null @@ -1,29 +0,0 @@ - -local files = require 'files' -local globalNode = require 'vm.global-node' -local literalNode = require 'vm.literal-node' - ----@class vm.state -local m = {} -for uri in files.eachFile() do - local state = files.getState(uri) - if state then - globalNode.compileAst(state.ast) - end -end - -files.watch(function (ev, uri) - if ev == 'update' then - local state = files.getState(uri) - if state then - globalNode.compileAst(state.ast) - end - end - if ev == 'remove' then - globalNode.dropUri(uri) - literalNode.dropUri(uri) - end -end) - - -return m -- cgit v1.2.3