summaryrefslogtreecommitdiff
path: root/server/src/vm/emmy.lua
blob: 3ad0a472a3d567e070173100cdfbc1e871c0d0eb (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
local functionMgr = require 'vm.function'
local library = require 'vm.library'
local mt = require 'vm.manager'

function mt:clearEmmy()
    self._emmy = nil
    self._emmyParams = nil
    self._emmyReturns = nil
    self._emmyGeneric = nil
    self._emmyComment = nil
    self._emmyOverLoads = nil
end

function mt:doEmmy(action)
    local tp = action.type
    if tp == 'emmyClass' then
        self:doEmmyClass(action)
    elseif tp == 'emmyType' then
        self:doEmmyType(action)
    elseif tp == 'emmyAlias' then
        self:doEmmyAlias(action)
    elseif tp == 'emmyParam' then
        self:doEmmyParam(action)
    elseif tp == 'emmyReturn' then
        self:doEmmyReturn(action)
    elseif tp == 'emmyField' then
        self:doEmmyField(action)
    elseif tp == 'emmyGeneric' then
        self:doEmmyGeneric(action)
    elseif tp == 'emmyVararg' then
        self:doEmmyVararg(action)
    elseif tp == 'emmyLanguage' then
    elseif tp == 'emmyArrayType' then
        self:doEmmyArrayType(action)
    elseif tp == 'emmyTableType' then
        self:doEmmyTableType(action)
    elseif tp == 'emmyFunctionType' then
        self:doEmmyFunctionType(action)
    elseif tp == 'emmySee' then
        self:doEmmySee(action)
    elseif tp == 'emmyOverLoad' then
        self:doEmmyOverLoad(action)
    elseif tp == 'emmyIncomplete' then
        self:doEmmyIncomplete(action)
    elseif tp == 'emmyComment' then
        self:doEmmyComment(action)
    end
end

function mt:getEmmy()
    local emmy = self._emmy
    self._emmy = nil
    return emmy
end

function mt:addEmmyParam(param)
    if not self._emmyParams then
        self._emmyParams = {}
    end
    self._emmyParams[#self._emmyParams+1] = param
end

function mt:addEmmyReturn(rtn)
    if not self._emmyReturns then
        self._emmyReturns = {}
    end
    self._emmyReturns[#self._emmyReturns+1] = rtn
end

function mt:addEmmyOverLoad(funcObj)
    if not self._emmyOverLoads then
        self._emmyOverLoads = {}
    end
    self._emmyOverLoads[#self._emmyOverLoads+1] = funcObj
end

function mt:getEmmyParams()
    local params = self._emmyParams
    self._emmyParams = nil
    return params
end

function mt:getEmmyReturns()
    local returns = self._emmyReturns
    self._emmyReturns = nil
    return returns
end

function mt:getEmmyOverLoads()
    local overLoads = self._emmyOverLoads
    self._emmyOverLoads = nil
    return overLoads
end

function mt:getEmmyGeneric()
    local generic = self._emmyGeneric
    self._emmyGeneric = nil
    return generic
end

---@return string
function mt:getEmmyComment()
    local comment = self._emmyComment
    self._emmyComment = nil
    return comment
end

function mt:doEmmyClass(action)
    ---@type emmyMgr
    local emmyMgr = self.emmyMgr
    self:instantSource(action)
    self:instantSource(action[1])
    local class = emmyMgr:addClass(action)
    action:set('emmy class', class:getName())
    action[1]:set('emmy class', class:getName())
    local extends = action[2]
    if extends then
        self:instantSource(extends)
        extends:set('emmy class', extends[1])
    end
    self._emmy = class
    action:set('emmy.class', class)
    if self.lsp then
        self.lsp.global:markSet(self:getUri())
    end
end

function mt:buildEmmyType(action)
    ---@type emmyMgr
    local emmyMgr = self.emmyMgr
    self:instantSource(action)
    for _, obj in ipairs(action) do
        self:instantSource(obj)
        obj:set('emmy class', obj[1])
    end
    local type = emmyMgr:addType(action)
    return type
end

function mt:doEmmyType(action)
    local type = self:buildEmmyType(action)
    self._emmy = type
    if self.lsp then
        self.lsp.global:markGet(self:getUri())
    end
    return type
end

function mt:doEmmyAlias(action)
    ---@type emmyMgr
    local emmyMgr = self.emmyMgr
    self:instantSource(action)
    self:instantSource(action[1])
    local type = self:buildEmmyAnyType(action[2])
    local alias = emmyMgr:addAlias(action, type)
    action:set('emmy.alias', alias)
    action[1]:set('emmy class', alias:getName())
    self._emmy = type
    if self.lsp then
        self.lsp.global:markSet(self:getUri())
    end
end

function mt:getGenericByType(type)
    local generics = self._emmyGeneric
    if not generics then
        return
    end
    if #type > 1 then
        return
    end
    local name = type[1][1]
    for _, generic in ipairs(generics) do
        if generic:getName() == name then
            return generic
        end
    end
    return nil
end

function mt:doEmmyParam(action)
    ---@type emmyMgr
    local emmyMgr = self.emmyMgr
    self:instantSource(action)
    self:instantSource(action[1])
    local type = self:getGenericByType(action[2]) or self:buildEmmyAnyType(action[2])
    local param = emmyMgr:addParam(action, type)
    action:set('emmy.param', param)
    self:addEmmyParam(param)
    if self.lsp then
        self.lsp.global:markGet(self:getUri())
    end
end

function mt:doEmmyReturn(action)
    ---@type emmyMgr
    local emmyMgr = self.emmyMgr
    self:instantSource(action)
    local type = self:getGenericByType(action[1]) or self:buildEmmyAnyType(action[1])
    local rtn = emmyMgr:addReturn(action, type)
    action:set('emmy.return', rtn)
    self:addEmmyReturn(rtn)
    if self.lsp then
        self.lsp.global:markGet(self:getUri())
    end
end

function mt:doEmmyField(action)
    ---@type emmyMgr
    local emmyMgr = self.emmyMgr
    self:instantSource(action)
    self:instantSource(action[2])
    local type = self:buildEmmyAnyType(action[3])
    local value = self:createValue('nil', action[2])
    local field = emmyMgr:addField(action, type, value)
    value:setEmmy(type)
    action:set('emmy.field', field)

    local class = self._emmy
    if not self._emmy or self._emmy.type ~= 'emmy.class' then
        return
    end
    class:addField(field)
    action:set('target class', class)
end

function mt:doEmmyGeneric(action)
    ---@type emmyMgr
    local emmyMgr = self.emmyMgr
    self:instantSource(action)

    local defs = {}
    for i, obj in ipairs(action) do
        defs[i] = {}
        defs[i].name = self:instantSource(obj[1])
        if obj[2] then
            defs[i].type = self:buildEmmyAnyType(obj[2])
        end
    end

    local generic = emmyMgr:addGeneric(defs)
    self._emmyGeneric = generic
end

function mt:doEmmyVararg(action)
    ---@type emmyMgr
    local emmyMgr = self.emmyMgr
    self:instantSource(action)
    local type = self:getGenericByType(action[1]) or self:buildEmmyAnyType(action[1])
    local param = emmyMgr:addParam(action, type)
    action:set('emmy.param', param)
    self:addEmmyParam(param)
    if self.lsp then
        self.lsp.global:markGet(self:getUri())
    end
end

function mt:buildEmmyArrayType(action)
    ---@type emmyMgr
    local emmyMgr = self.emmyMgr
    self:instantSource(action)
    action:set('emmy class', action[1])
    local type = emmyMgr:addArrayType(action)
    return type
end

function mt:doEmmyArrayType(action)
    local type = self:buildEmmyArrayType(action)
    self._emmy = type
    if self.lsp then
        self.lsp.global:markGet(self:getUri())
    end
    return type
end

function mt:buildEmmyTableType(action)
    ---@type emmyMgr
    local emmyMgr = self.emmyMgr
    self:instantSource(action)
    local keyType = self:buildEmmyAnyType(action[1])
    local valueType = self:buildEmmyAnyType(action[2])
    local type = emmyMgr:addTableType(action, keyType, valueType)
    return type
end

function mt:doEmmyTableType(action)
    local type = self:buildEmmyTableType(action)
    self._emmy = type
    if self.lsp then
        self.lsp.global:markGet(self:getUri())
    end
    return type
end

function mt:buildEmmyFunctionType(source)
    ---@type emmyMgr
    local emmyMgr = self.emmyMgr
    self:instantSource(source)
    local funcObj = emmyMgr:addFunctionType(source)
    ---@type emmyFunction
    local func = functionMgr.create(source)
    for i = 1, #source // 2 do
        local nameSource = source[i*2-1]
        local typeSource = source[i*2]
        local paramType = self:buildEmmyAnyType(typeSource)
        funcObj:addParam(nameSource[1], paramType)
        local value = self:createValue(paramType:getType(), typeSource)
        value:setEmmy(paramType)
        self:instantSource(nameSource)
        func:addArg(nameSource[1], nameSource, value)
    end
    local returnSource = source[#source]
    if returnSource then
        local returnType = self:buildEmmyAnyType(returnSource)
        funcObj:addReturn(returnType)
        local value = self:createValue(returnType:getType(), returnSource)
        value:setEmmy(returnType)
        func:setReturn(1, value)
    end
    funcObj:bindFunction(func)
    return funcObj
end

function mt:doEmmyFunctionType(action)
    local funcObj = self:buildEmmyFunctionType(action)
    self._emmy = funcObj
    return funcObj
end

function mt:buildEmmyAnyType(source)
    if source.type == 'emmyType' then
        return self:buildEmmyType(source)
    elseif source.type == 'emmyArrayType' then
        return self:buildEmmyArrayType(source)
    elseif source.type == 'emmyTableType' then
        return self:buildEmmyTableType(source)
    elseif source.type == 'emmyFunctionType' then
        return self:buildEmmyFunctionType(source)
    else
        error('Unknown emmy type: ' .. table.dump(source))
    end
end

function mt:doEmmyIncomplete(action)
    self:instantSource(action)
end

function mt:doEmmyComment(action)
    self._emmyComment = action[1]
end

function mt:doEmmySee(action)
    self:instantSource(action)
    self:instantSource(action[2])
    action[2]:set('emmy see', action)
end

function mt:doEmmyOverLoad(action)
    local funcObj = self:buildEmmyFunctionType(action)
    self:addEmmyOverLoad(funcObj)
end