diff options
-rw-r--r-- | script/parser/guide.lua | 2 | ||||
-rw-r--r-- | script/vm/compiler.lua | 91 | ||||
-rw-r--r-- | script/vm/generic.lua | 14 | ||||
-rw-r--r-- | script/vm/global.lua | 21 | ||||
-rw-r--r-- | script/vm/local-id.lua | 4 | ||||
-rw-r--r-- | script/vm/sign.lua | 69 | ||||
-rw-r--r-- | script/vm/type.lua | 4 | ||||
-rw-r--r-- | script/vm/visible.lua | 2 |
8 files changed, 117 insertions, 90 deletions
diff --git a/script/parser/guide.lua b/script/parser/guide.lua index b9b60ce7..424de63e 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -73,7 +73,7 @@ local type = type ---@field hasBreak? true ---@field hasError? true ---@field [integer] parser.object|any ----@field public _root parser.object +---@field package _root parser.object ---@class guide ---@field debugMode boolean diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 4a4486e9..69cfdc55 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -11,7 +11,7 @@ local LOCK = {} ---@class parser.object ---@field _compiledNodes boolean ---@field _node vm.node ----@field public _globalBase table +---@field package _globalBase table ---@field cindex integer ---@field func parser.object ---@field operators? parser.object[] @@ -447,69 +447,6 @@ function vm.getClassFields(suri, object, key, ref, pushResult) searchGlobal(object) end ----@class parser.object ----@field public _sign vm.sign|false - ----@param source parser.object ----@return vm.sign|false -local function getObjectSign(source) - if source._sign ~= nil then - return source._sign - end - source._sign = false - if source.type == 'function' then - if not source.bindDocs then - return false - end - for _, doc in ipairs(source.bindDocs) do - if doc.type == 'doc.generic' then - if not source._sign then - source._sign = vm.createSign() - break - end - end - end - if not source._sign then - return false - end - if source.args then - for _, arg in ipairs(source.args) do - local argNode = vm.compileNode(arg) - if arg.optional then - argNode:addOptional() - end - source._sign:addSign(argNode) - end - end - end - if source.type == 'doc.type.function' - or source.type == 'doc.type.table' - or source.type == 'doc.type.array' then - local hasGeneric - guide.eachSourceType(source, 'doc.generic.name', function () - hasGeneric = true - end) - if not hasGeneric then - return false - end - source._sign = vm.createSign() - if source.type == 'doc.type.function' then - for _, arg in ipairs(source.args) do - if arg.extends then - local argNode = vm.compileNode(arg.extends) - if arg.optional then - argNode:addOptional() - end - source._sign:addSign(argNode) - else - source._sign:addSign(vm.createNode()) - end - end - end - end - return source._sign -end - ---@param func parser.object ---@param index integer ---@return (parser.object|vm.generic)? @@ -537,7 +474,7 @@ function vm.getReturnOfFunction(func, index) return nil end end - local sign = getObjectSign(func) + local sign = vm.getSign(func) if not sign then return rtn end @@ -880,7 +817,7 @@ local function compileCallArgNode(arg, call, callNode, fixIndex, myIndex) for n in callNode:eachObject() do if n.type == 'function' then ---@cast n parser.object - local sign = getObjectSign(n) + local sign = vm.getSign(n) local farg = getFuncArg(n, myIndex) if farg then for fn in vm.compileNode(farg):eachObject() do @@ -958,9 +895,9 @@ function vm.compileCallArg(arg, call, index) end ---@class parser.object ----@field public _iterator? table ----@field public _iterArgs? table ----@field public _iterVars? table<parser.object, vm.node> +---@field package _iterator? table +---@field package _iterArgs? table +---@field package _iterVars? table<parser.object, vm.node> ---@param source parser.object ---@param target parser.object @@ -1391,7 +1328,7 @@ local compilerSwitch = util.switch() local index = source.returnIndex local hasMarkDoc if func.bindDocs then - local sign = getObjectSign(func) + local sign = vm.getSign(func) local lastReturn for _, doc in ipairs(func.bindDocs) do if doc.type == 'doc.return' then @@ -1406,7 +1343,7 @@ local compilerSwitch = util.switch() end) end if hasGeneric then - ---@cast sign -false + ---@cast sign -? vm.setNode(source, vm.createGeneric(rtn, sign)) else vm.setNode(source, vm.compileNode(rtn)) @@ -1638,8 +1575,8 @@ local compilerSwitch = util.switch() if set.extends then for _, ext in ipairs(set.extends) do if ext.type == 'doc.type.table' then - if ext._generic then - local resolved = ext._generic:resolve(uri, source.signs) + if vm.getGeneric(ext) then + local resolved = vm.getGeneric(ext):resolve(uri, source.signs) vm.setNode(source, resolved) end end @@ -1647,8 +1584,8 @@ local compilerSwitch = util.switch() end end if set.type == 'doc.alias' then - if set.extends._generic then - local resolved = set.extends._generic:resolve(uri, source.signs) + if vm.getGeneric(set.extends) then + local resolved = vm.getGeneric(set.extends):resolve(uri, source.signs) vm.setNode(source, resolved) end end @@ -1833,7 +1770,7 @@ local function compileByGlobal(source) if set.extends then for _, ext in ipairs(set.extends) do if ext.type == 'doc.type.table' then - if not ext._generic then + if not vm.getGeneric(ext) then globalNode:merge(vm.compileNode(ext)) end end @@ -1841,7 +1778,7 @@ local function compileByGlobal(source) end end if set.type == 'doc.alias' then - if not set.extends._generic then + if not vm.getGeneric(set.extends) then globalNode:merge(vm.compileNode(set.extends)) end end diff --git a/script/vm/generic.lua b/script/vm/generic.lua index 78df7caf..4981f48a 100644 --- a/script/vm/generic.lua +++ b/script/vm/generic.lua @@ -2,7 +2,7 @@ local vm = require 'vm.vm' ---@class parser.object ----@field public _generic vm.generic +---@field package _generic vm.generic ---@class vm.generic ---@field sign vm.sign @@ -136,6 +136,18 @@ function mt:resolve(uri, args) return result end +---@param source parser.object +---@param generic vm.generic +function vm.setGeneric(source, generic) + source._generic = generic +end + +---@param source parser.object +---@return vm.generic? +function vm.getGeneric(source) + return source._generic +end + ---@param proto vm.object ---@param sign vm.sign ---@return vm.generic diff --git a/script/vm/global.lua b/script/vm/global.lua index 86fd78cc..e94f9239 100644 --- a/script/vm/global.lua +++ b/script/vm/global.lua @@ -164,9 +164,11 @@ local function createGlobal(name, cate) }, mt) end +---@alias parser.enum string|integer + ---@class parser.object ----@field public _globalNode vm.global|false ----@field public _enums? (string|integer)[] +---@field package _globalNode vm.global|false +---@field package _enums? parser.enum[] ---@type table<string, vm.global> local allGlobals = {} @@ -324,14 +326,15 @@ local compilerGlobalSwitch = util.switch() source._globalNode = class if source.signs then - source._sign = vm.createSign() - for _, sign in ipairs(source.signs) do - source._sign:addSign(vm.compileNode(sign)) + local sign = vm.createSign() + vm.setSign(source, sign) + for _, obj in ipairs(source.signs) do + sign:addSign(vm.compileNode(obj)) end if source.extends then for _, ext in ipairs(source.extends) do if ext.type == 'doc.type.table' then - ext._generic = vm.createGeneric(ext, source._sign) + vm.setGeneric(ext, vm.createGeneric(ext, sign)) end end end @@ -540,6 +543,12 @@ function vm.getGlobalNode(source) end ---@param source parser.object +---@return parser.enum[]? +function vm.getEnums(source) + return source._enums +end + +---@param source parser.object local function compileSelf(source) if source.parent.type ~= 'funcargs' then return diff --git a/script/vm/local-id.lua b/script/vm/local-id.lua index 75ac7f8d..1c3fd356 100644 --- a/script/vm/local-id.lua +++ b/script/vm/local-id.lua @@ -4,8 +4,8 @@ local guide = require 'parser.guide' local vm = require 'vm.vm' ---@class parser.object ----@field public _localID string|false ----@field public _localIDs table<string, { sets: parser.object[], gets: parser.object[] }> +---@field package _localID string|false +---@field package _localIDs table<string, { sets: parser.object[], gets: parser.object[] }> local compileLocalID, getLocal diff --git a/script/vm/sign.lua b/script/vm/sign.lua index 21044a28..3791ca4f 100644 --- a/script/vm/sign.lua +++ b/script/vm/sign.lua @@ -260,3 +260,72 @@ function vm.createSign() }, mt) return genericMgr end + +---@class parser.object +---@field package _sign vm.sign|false|nil + +---@param source parser.object +---@param sign vm.sign +function vm.setSign(source, sign) + source._sign = sign +end + +---@param source parser.object +---@return vm.sign? +function vm.getSign(source) + if source._sign ~= nil then + return source._sign or nil + end + source._sign = false + if source.type == 'function' then + if not source.bindDocs then + return nil + end + for _, doc in ipairs(source.bindDocs) do + if doc.type == 'doc.generic' then + if not source._sign then + source._sign = vm.createSign() + break + end + end + end + if not source._sign then + return nil + end + if source.args then + for _, arg in ipairs(source.args) do + local argNode = vm.compileNode(arg) + if arg.optional then + argNode:addOptional() + end + source._sign:addSign(argNode) + end + end + end + if source.type == 'doc.type.function' + or source.type == 'doc.type.table' + or source.type == 'doc.type.array' then + local hasGeneric + guide.eachSourceType(source, 'doc.generic.name', function () + hasGeneric = true + end) + if not hasGeneric then + return nil + end + source._sign = vm.createSign() + if source.type == 'doc.type.function' then + for _, arg in ipairs(source.args) do + if arg.extends then + local argNode = vm.compileNode(arg.extends) + if arg.optional then + argNode:addOptional() + end + source._sign:addSign(argNode) + else + source._sign:addSign(vm.createNode()) + end + end + end + end + return source._sign or nil +end diff --git a/script/vm/type.lua b/script/vm/type.lua index f2870d28..53b1ec3b 100644 --- a/script/vm/type.lua +++ b/script/vm/type.lua @@ -52,7 +52,7 @@ local function checkEnum(parentName, child, uri) end for _, set in ipairs(parentClass:getSets(uri)) do if set.type == 'doc.enum' then - if not set._enums then + if not vm.getEnums(set) then return false end if child.type ~= 'string' @@ -62,7 +62,7 @@ local function checkEnum(parentName, child, uri) and child.type ~= 'doc.type.integer' then return false end - return util.arrayHas(set._enums, child[1]) + return util.arrayHas(vm.getEnums(set), child[1]) end end diff --git a/script/vm/visible.lua b/script/vm/visible.lua index 9d667744..6a2d9cc5 100644 --- a/script/vm/visible.lua +++ b/script/vm/visible.lua @@ -7,7 +7,7 @@ local glob = require 'glob' ---@alias parser.visibleType 'public' | 'protected' | 'private' | 'package' ---@class parser.object ----@field public _visibleType? parser.visibleType +---@field package _visibleType? parser.visibleType ---@param source parser.object ---@return parser.visibleType |