summaryrefslogtreecommitdiff
path: root/server/src/emmy/manager.lua
blob: 931c94494e68284ca32979b571ae48c15104b62b (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
local listMgr      = require 'vm.list'
local sourceMgr    = require 'vm.source'
local newClass     = require 'emmy.class'
local newType      = require 'emmy.type'
local newTypeUnit  = require 'emmy.typeUnit'
local newAlias     = require 'emmy.alias'
local newParam     = require 'emmy.param'
local newReturn    = require 'emmy.return'
local newField     = require 'emmy.field'
local newGeneric   = require 'emmy.generic'
local newArrayType = require 'emmy.arrayType'
local newTableType = require 'emmy.tableType'
local newFuncType  = require 'emmy.funcType'

local mt = {}
mt.__index = mt
mt.__name = 'emmyMgr'

function mt:flushClass(name)
    local list = self._class[name]
    if not list then
        return
    end
    local version = listMgr.getVersion()
    if version == list.version then
        return
    end
    for srcId in pairs(list) do
        if not listMgr.get(srcId) then
            list[srcId] = nil
        end
    end
    if not next(list) then
        self._class[name] = nil
        return
    end
    list.version = version
end

function mt:eachClassByName(name, callback)
    self:flushClass(name)
    local list = self._class[name]
    if not list then
        return
    end
    for k, class in pairs(list) do
        if k ~= 'version' then
            local res = callback(class)
            if res ~= nil then
                return res
            end
        end
    end
end

function mt:eachClass(...)
    local n = select('#', ...)
    if n == 1 then
        local callback = ...
        for name in pairs(self._class) do
            local res = self:eachClassByName(name, callback)
            if res ~= nil then
                return res
            end
        end
    else
        local name, callback = ...
        return self:eachClassByName(name, callback)
    end
end

function mt:getClass(name)
    self:flushClass(name)
    local list = self._class[name]
    local version = listMgr.getVersion()
    if not list then
        list = {
            version = version,
        }
        self._class[name] = list
    end
    return list
end

function mt:newClass(name, extends, source)
    local list = self:getClass(name)
    list[source.id] = newClass(self, name, extends, source)
    return list[source.id]
end

function mt:addClass(source)
    local className = source[1][1]
    local extends = source[2] and source[2][1]
    local class = self:newClass(className, extends, source)
    return class
end

function mt:addType(source)
    local typeObj = newType(self, source)
    for i, obj in ipairs(source) do
        local typeUnit = newTypeUnit(self, obj)
        local className = obj[1]
        local list = self:getClass(className)
        typeUnit:setParent(typeObj)
        list[source.id] = typeUnit
        typeObj._childs[i] = typeUnit
        obj:set('emmy.typeUnit', typeUnit)
    end
    return typeObj
end

function mt:addArrayType(source)
    local typeObj = newArrayType(self, source)
    local typeUnit = newTypeUnit(self, source)
    local list = self:getClass(source[1])
    typeUnit:setParent(typeObj)
    list[source.id] = typeUnit
    source:set('emmy.typeUnit', typeUnit)
    return typeObj
end

function mt:addTableType(source, keyType, valueType)
    local typeObj = newTableType(self, source, keyType, valueType)
    return typeObj
end

function mt:addFunctionType(source)
    local typeObj = newFuncType(self, source)
    return typeObj
end

function mt:addAlias(source, typeObj)
    local aliasName = source[1][1]
    local aliasObj = newAlias(self, source)
    aliasObj:bindType(typeObj)
    local list = self:getClass(aliasName)
    list[source.id] = aliasObj
    for i = 3, #source do
        aliasObj:addEnum(source[i][1][1], source[i].option)
    end
    return aliasObj
end

function mt:addParam(source, bind)
    local paramObj = newParam(self, source)
    if bind.type == 'emmy.generic' then
        paramObj:bindGeneric(bind)
    else
        paramObj:bindType(bind)
        self:eachClass(bind:getName(), function (class)
            if class.type == 'emmy.alias' then
                class:eachEnum(function (enum, option)
                    paramObj:addEnum(enum, option)
                end)
            end
        end)
    end
    for i = 3, #source do
        paramObj:addEnum(source[i][1][1], source[i].option)
    end
    paramObj:setOption(source.option)
    return paramObj
end

function mt:addReturn(source, bind)
    local returnObj = newReturn(self, source)
    if bind.type == 'emmy.generic' then
        returnObj:bindGeneric(bind)
    else
        returnObj:bindType(bind)
    end
    return returnObj
end

function mt:addField(source, typeObj, value)
    local fieldObj = newField(self, source)
    fieldObj:bindType(typeObj)
    fieldObj:bindValue(value)
    return fieldObj
end

function mt:addGeneric(defs)
    local genericObj = newGeneric(self, defs)
    return genericObj
end

function mt:remove()
end

function mt:count()
    local count = 0
    for _, list in pairs(self._class) do
        for k in pairs(list) do
            if k ~= 'version' then
                count = count + 1
            end
        end
    end
    return count
end

return function ()
    ---@class emmyMgr
    local self = setmetatable({
        _class = {},
    }, mt)

    local source = sourceMgr.dummy()
    self:newClass('any',       nil,     source)
    self:newClass('string',   'any',    source)
    self:newClass('number',   'any',    source)
    self:newClass('integer',  'number', source)
    self:newClass('boolean',  'any',    source)
    self:newClass('table',    'any',    source)
    self:newClass('function', 'any',    source)
    self:newClass('nil',      'any',    source)
    self:newClass('userdata', 'any',    source)
    self:newClass('thread',   'any',    source)

    return self
end