1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
local guide = require 'parser.guide'
local nodeMgr = require 'vm.node'
---@class vm.sign
---@field parent parser.object
---@field signList vm.node[]
local mt = {}
mt.__index = mt
mt.type = 'sign'
---@param node vm.node
function mt:addSign(node)
self.signList[#self.signList+1] = node
end
---@param argNodes vm.node[]
---@return table<string, vm.node>
function mt:resolve(argNodes)
if not argNodes then
return nil
end
local typeMgr = require 'vm.type'
local compiler = require 'vm.compiler'
local globalMgr = require 'vm.global-manager'
local resolved = {}
---@param typeUnit parser.object
---@param node vm.node
local function resolve(typeUnit, node)
if typeUnit.type == 'doc.generic.name' then
local key = typeUnit[1]
if typeUnit.literal then
-- 'number' -> `T`
for n in nodeMgr.eachNode(node) do
if n.type == 'string' then
local type = globalMgr.declareGlobal('type', n[1], guide.getUri(n))
resolved[key] = nodeMgr.mergeNode(type, resolved[key])
end
end
else
-- number -> T
resolved[key] = nodeMgr.mergeNode(node, resolved[key])
end
end
if typeUnit.type == 'doc.type.array' then
for n in nodeMgr.eachNode(node) do
if n.type == 'doc.type.array' then
-- number[] -> T[]
resolve(typeUnit.node, n.node)
end
end
end
if typeUnit.type == 'doc.type.table' then
for _, ufield in ipairs(typeUnit.fields) do
local ufieldNode = compiler.compileNode(ufield.name)
local uvalueNode = compiler.compileNode(ufield.extends)
if ufieldNode.type == 'doc.generic.name' and uvalueNode.type == 'doc.generic.name' then
-- { [number]: number} -> { [K]: V }
local tfieldNode = typeMgr.getTableKey(node, 'any')
local tvalueNode = typeMgr.getTableValue(node, 'any')
resolve(ufieldNode, tfieldNode)
resolve(uvalueNode, tvalueNode)
else
if ufieldNode.type == 'doc.generic.name' then
-- { [number]: number}|number[] -> { [K]: number }
local tnode = typeMgr.getTableKey(node, uvalueNode)
resolve(ufieldNode, tnode)
else
-- { [number]: number}|number[] -> { [number]: V }
local tnode = typeMgr.getTableValue(node, ufieldNode)
resolve(uvalueNode, tnode)
end
end
end
end
end
for i, node in ipairs(argNodes) do
local sign = self.signList[i]
if not sign then
break
end
for n in nodeMgr.eachNode(sign) do
node = compiler.compileNode(node)
if node then
if sign.optional then
node = nodeMgr.removeOptional(node)
end
resolve(n, node)
end
end
end
return resolved
end
---@return vm.sign
return function ()
local genericMgr = setmetatable({
signList = {},
}, mt)
return genericMgr
end
|