diff options
-rw-r--r-- | script/core/generic.lua | 82 | ||||
-rw-r--r-- | script/core/linker.lua | 80 |
2 files changed, 102 insertions, 60 deletions
diff --git a/script/core/generic.lua b/script/core/generic.lua index 4246f8bd..19c445cd 100644 --- a/script/core/generic.lua +++ b/script/core/generic.lua @@ -1,17 +1,7 @@ -local util = require 'utility' - -local counter = util.counter() - ---@class generic.value ---@field type string ---@field closure generic.closure ----@field types parser.guide.object[]|generic.value[] - ----递归实例化对象 ----@param obj parser.guide.object ----@param callback fun(docName: parser.guide.object) -local function instantValue(obj, callback) -end +---@field proto parser.guide.object ---@class generic.closure ---@field type string @@ -22,30 +12,78 @@ end local m = {} ----给闭包设置调用信息 ----@param closure generic.closure ----@param params parser.guide.object[] -function m.setCallParams(closure, params) - -- 立刻解决所有的泛型 - -- 对每个参数进行核对,存入泛型表 - -- 为所有的 param 与 return 创建副本 - -- 如果return中有function,也要递归创建闭包 +local function instantValue(closure, proto) + ---@type generic.value + local value = { + type = 'generic.value', + closure = closure, + proto = proto, + } + return value +end + +---递归实例化对象 +---@param obj parser.guide.object +---@return generic.value +local function createValue(closure, obj) + if obj.type == 'doc.type' then + local types = {} + local hasGeneric + for i, tp in ipairs(obj.types) do + local genericValue = createValue(tp) + if genericValue then + hasGeneric = true + types[i] = genericValue + else + types[i] = tp + end + end + if hasGeneric then + local value = instantValue(closure, obj) + value.types = types + return value + else + return nil + end + end end ---创建一个闭包 ---@param protoFunction parser.guide.object # 原型函数 ---@param parentClosure? generic.closure ---@return generic.closure -function m.createClosure(protoFunction, parentClosure) +function m.createClosure(protoFunction, call, parentClosure) ---@type generic.closure local closure = { type = 'generic.closure', - id = counter(), + parent = parentClosure, proto = protoFunction, - upvalues = {}, + call = call, + upvalues = parentClosure and parentClosure.upvalues or {}, params = {}, returns = {}, } + + -- 立刻解决所有的泛型 + -- 对每个参数进行核对,存入泛型表 + -- 为所有的 param 与 return 创建副本 + -- 如果return中有function,也要递归创建闭包 + if protoFunction.type == 'function' then + for _, doc in ipairs(protoFunction.bindDocs) do + if doc.type == 'doc.param' then + local extends = doc.extends + closure.params[extends.paramIndex] = createValue(closure, extends) + elseif doc.type == 'doc.return' then + for _, rtn in ipairs(doc.returns) do + closure.returns[rtn.returnIndex] = createValue(closure, rtn) + end + end + end + end + if protoFunction.type == 'doc.function' then + + end + return closure end diff --git a/script/core/linker.lua b/script/core/linker.lua index f351a4f0..5ca11087 100644 --- a/script/core/linker.lua +++ b/script/core/linker.lua @@ -1,6 +1,5 @@ local util = require 'utility' local guide = require 'parser.guide' -local generic = require 'core.generic' local Linkers local LastIDCache = {} @@ -123,9 +122,6 @@ local function getKey(source) or source.type == 'doc.extends.name' or source.type == 'doc.see.name' then local name = source[1] - if source.typeGeneric then - return source.start, nil - end return name, nil elseif source.type == 'doc.class' or source.type == 'doc.type' @@ -139,6 +135,13 @@ local function getKey(source) return source.start, nil elseif source.type == 'doc.see.field' then return ('%q'):format(source[1]), source.parent.name + elseif source.type == 'generic.closure' then + return source.call.start, nil + elseif source.type == 'generic.value' then + return ('%s|%s'):format( + source.closure.call.start, + getKey(source.proto) + ) end return nil, nil end @@ -192,6 +195,12 @@ local function checkMode(source) if source.type == 'doc.vararg' then return 'dv:' end + if source.type == 'generic.closure' then + return 'gc:' + end + if source.type == 'generic.value' then + return 'gv:' + end if isGlobal(source) then return 'g:' end @@ -249,17 +258,6 @@ local function getID(source) return id end ----添加关联单元 ----@param id string ----@param source parser.guide.object -local function pushSource(id, source) - local link = getLink(id) - if not link.sources then - link.sources = {} - end - link.sources[#link.sources+1] = source -end - ---添加关联的前进ID ---@param id string ---@param forwardID string @@ -294,14 +292,38 @@ local function pushBackward(id, backwardID) link.backward[#link.backward+1] = backwardID end -local function findDocState() +---@class link +-- 当前节点的id +---@field id string +-- 使用该ID的单元 +---@field sources parser.guide.object[] +-- 前进的关联ID +---@field forward string[] +-- 后退的关联ID +---@field backward string[] +-- 函数调用参数信息(用于泛型) +---@field call parser.guide.object + +local m = {} + +m.SPLIT_CHAR = SPLIT_CHAR +m.RETURN_INDEX_CHAR = RETURN_INDEX_CHAR +m.PARAM_INDEX_CHAR = PARAM_INDEX_CHAR +---添加关联单元 +---@param id string +---@param source parser.guide.object +function m.pushSource(id, source) + local link = getLink(id) + if not link.sources then + link.sources = {} + end + link.sources[#link.sources+1] = source end ----前进 ---@param source parser.guide.object ---@return parser.guide.object[] -local function compileLink(source) +function m.compileLink(source) local id = getID(source) local parent = source.parent if not parent then @@ -554,24 +576,6 @@ local function compileLink(source) end end ----@class link --- 当前节点的id ----@field id string --- 使用该ID的单元 ----@field sources parser.guide.object[] --- 前进的关联ID ----@field forward string[] --- 后退的关联ID ----@field backward string[] --- 函数调用参数信息(用于泛型) ----@field call parser.guide.object - -local m = {} - -m.SPLIT_CHAR = SPLIT_CHAR -m.RETURN_INDEX_CHAR = RETURN_INDEX_CHAR -m.PARAM_INDEX_CHAR = PARAM_INDEX_CHAR - ---根据ID来获取所有的link ---@param root parser.guide.object ---@param id string @@ -637,9 +641,9 @@ function m.compileLinks(source) guide.eachSource(root, function (src) local id = getID(src) if id then - pushSource(id, src) + m.pushSource(id, src) end - compileLink(src) + m.compileLink(src) end) return Linkers end |