summaryrefslogtreecommitdiff
path: root/script/vm/type.lua
blob: 46cc39466a99dc7f860c52f0284a526e082657fe (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
local nodeMgr   = require 'vm.node'
local globalMgr = require 'vm.global-manager'
local union     = require 'vm.union'
---@class vm
local vm        = require 'vm.vm'

---@param uri uri
---@param child  vm.node|string
---@param parent vm.node|string
---@param mark?  table
---@return boolean
function vm.isSubType(uri, child, parent, mark)
    if type(parent) == 'string' then
        parent = globalMgr.getGlobal('type', parent)
    end
    if type(child) == 'string' then
        child = globalMgr.getGlobal('type', child)
    end

    if not child or not parent then
        return false
    end

    mark = mark or {}
    for childNode in nodeMgr.eachObject(child) do
        if childNode.type ~= 'global'
        or childNode.cate ~= 'type' then
            goto CONTINUE_CHILD
        end
        if mark[childNode.name] then
            return false
        end
        mark[childNode.name] = true
        for parentNode in nodeMgr.eachObject(parent) do
            if parentNode.type ~= 'global'
            or parentNode.cate ~= 'type' then
                goto CONTINUE_PARENT
            end
            if parentNode.name == 'any' or childNode.name == 'any' then
                return true
            end

            if parentNode.name == childNode.name then
                return true
            end

            for _, set in ipairs(childNode:getSets(uri)) do
                if set.type == 'doc.class' and set.extends then
                    for _, ext in ipairs(set.extends) do
                        if  ext.type == 'doc.extends.name'
                        and vm.isSubType(uri, ext[1], parentNode, mark) then
                            return true
                        end
                    end
                end
                if set.type == 'doc.alias' and set.extends then
                    for _, ext in ipairs(set.extends.types) do
                        if  ext.type == 'doc.type.name'
                        and vm.isSubType(uri, ext[1], parentNode, mark) then
                            return true
                        end
                    end
                end
            end
            ::CONTINUE_PARENT::
        end
        ::CONTINUE_CHILD::
    end

    return false
end

---@param uri uri
---@param tnode vm.node
---@param knode vm.node
---@return vm.union?
function vm.getTableValue(uri, tnode, knode)
    local result = union()
    for tn in nodeMgr.eachObject(tnode) do
        if tn.type == 'doc.type.table' then
            for _, field in ipairs(tn.fields) do
                if vm.isSubType(uri, vm.compileNode(field.name), knode) then
                    result:merge(vm.compileNode(field.extends))
                end
            end
        end
        if tn.type == 'doc.type.array' then
            result:merge(vm.compileNode(tn.node))
        end
        if tn.type == 'table' then
            for _, field in ipairs(tn) do
                if field.type == 'tableindex' then
                    result:merge(vm.compileNode(field.value))
                end
                if field.type == 'tablefield' then
                    if vm.isSubType(uri, knode, 'string') then
                        result:merge(vm.compileNode(field.value))
                    end
                end
                if field.type == 'tableexp' then
                    if vm.isSubType(uri, knode, 'integer') and field.tindex == 1 then
                        result:merge(vm.compileNode(field.value))
                    end
                end
            end
        end
    end
    if result:isEmpty() then
        return nil
    end
    return result
end

---@param uri uri
---@param tnode vm.node
---@param vnode vm.node
---@return vm.union?
function vm.getTableKey(uri, tnode, vnode)
    local result = union()
    for tn in nodeMgr.eachObject(tnode) do
        if tn.type == 'doc.type.table' then
            for _, field in ipairs(tn.fields) do
                if vm.isSubType(uri, vm.compileNode(field.extends), vnode) then
                    result:merge(vm.compileNode(field.name))
                end
            end
        end
        if tn.type == 'doc.type.array' then
            result:merge(globalMgr.getGlobal('type', 'integer'))
        end
        if tn.type == 'table' then
            for _, field in ipairs(tn) do
                if field.type == 'tableindex' then
                    result:merge(vm.compileNode(field.index))
                end
                if field.type == 'tablefield' then
                    result:merge(globalMgr.getGlobal('type', 'string'))
                end
                if field.type == 'tableexp' then
                    result:merge(globalMgr.getGlobal('type', 'integer'))
                end
            end
        end
    end
    if result:isEmpty() then
        return nil
    end
    return result
end