summaryrefslogtreecommitdiff
path: root/script/vm/sign.lua
blob: 9773627e531ebb76a22bce04ce9c1b0c550cf8b1 (plain)
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
104
105
local guide         = require 'parser.guide'
local nodeMgr       = require 'vm.node'
---@class vm
local vm            = require 'vm.vm'

---@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 uri uri
---@param args parser.object
---@return table<string, vm.node>
function mt:resolve(uri, args)
    if not args then
        return nil
    end
    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.eachObject(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.eachObject(node) do
                if n.type == 'doc.type.array' then
                    -- number[] -> T[]
                    resolve(typeUnit.node, compiler.compileNode(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[1].type == 'doc.generic.name' and uvalueNode.type[1] == 'doc.generic.name' then
                    -- { [number]: number} -> { [K]: V }
                    local tfieldNode = vm.getTableKey(uri, node, 'any')
                    local tvalueNode = vm.getTableValue(uri, node, 'any')
                    resolve(ufieldNode[1], tfieldNode)
                    resolve(uvalueNode[1], tvalueNode)
                else
                    if ufieldNode[1].type == 'doc.generic.name' then
                        -- { [number]: number}|number[] -> { [K]: number }
                        local tnode = vm.getTableKey(uri, node, uvalueNode)
                        resolve(ufieldNode[1], tnode)
                    elseif uvalueNode[1].type == 'doc.generic.name' then
                        -- { [number]: number}|number[] -> { [number]: V }
                        local tnode = vm.getTableValue(uri, node, ufieldNode)
                        resolve(uvalueNode[1], tnode)
                    end
                end
            end
        end
    end

    for i, arg in ipairs(args) do
        local sign = self.signList[i]
        if not sign then
            break
        end
        for n in nodeMgr.eachObject(sign) do
            local argNode = compiler.compileNode(arg)
            if argNode then
                if sign.optional then
                    argNode = nodeMgr.removeOptional(argNode)
                end
                resolve(n, argNode)
            end
        end
    end

    return resolved
end

---@return vm.sign
return function ()
    local genericMgr = setmetatable({
        signList = {},
    }, mt)
    return genericMgr
end