summaryrefslogtreecommitdiff
path: root/script/emmy/type.lua
blob: 8977cf2d0bbaee31ee4c4cecf2d4e948b037f1ce (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
local listMgr = require 'vm.list'

local function buildName(source)
    local names = {}
    for i, type in ipairs(source) do
        if type.type == 'emmyName' then
            names[i] = type[1]
        elseif type.type == 'emmyArrayType' then
            names[i] = type[1][1]..'[]'
        end
    end
    return table.concat(names, '|')
end

---@class EmmyType
local mt = {}
mt.__index = mt
mt.type = 'emmy.type'

function mt:getType()
    return self.name
end

function mt:getName()
    return self.name
end

function mt:getSource()
    return listMgr.get(self.source)
end

function mt:eachClass(callback)
    for _, typeUnit in ipairs(self._childs) do
        ---@type EmmyTypeUnit
        local emmyTypeUnit = typeUnit
        emmyTypeUnit:getClass(callback)
    end
end

function mt:setValue(value)
    self.value = value
    for _, typeUnit in ipairs(self._childs) do
        typeUnit:setValue(value)
    end
end

function mt:getValue()
    return self.value
end

return function (manager, source)
    local self = setmetatable({
        name = buildName(source),
        source = source.id,
        _manager = manager,
        _childs = {},
    }, mt)
    return self
end