summaryrefslogtreecommitdiff
path: root/script/vm/value.lua
blob: d29ca9d0241efff66c5b3392b69b4f4661b45a70 (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
local guide    = require 'parser.guide'
---@class vm
local vm       = require 'vm.vm'

---@param source parser.object?
---@return boolean|nil
function vm.test(source)
    if not source then
        return nil
    end
    local node = vm.compileNode(source)
    local hasTrue, hasFalse
    for n in node:eachObject() do
        if n.type == 'boolean'
        or n.type == 'doc.type.boolean' then
            if n[1] == true then
                hasTrue = true
            end
            if n[1] == false then
                hasFalse = true
            end
        end
        if n.type == 'global' and n.cate == 'type' then
            if n.name == 'true' then
                hasTrue = true
            end
            if n.name == 'false'
            or n.name == 'nil' then
                hasFalse = true
            end
        end
        if n.type == 'nil' then
            hasFalse = true
        end
        if n.type == 'string'
        or n.type == 'number'
        or n.type == 'integer'
        or n.type == 'table'
        or n.type == 'function' then
            hasTrue = true
        end
    end
    if hasTrue == hasFalse then
        return nil
    end
    if hasTrue then
        return true
    else
        return false
    end
end

---@param v vm.object
---@return string?
local function getUnique(v)
    if v.type == 'boolean' then
        if v[1] == nil then
            return false
        end
        return ('%s'):format(v[1])
    end
    if v.type == 'number' then
        if not v[1] then
            return false
        end
        return ('num:%s'):format(v[1])
    end
    if v.type == 'integer' then
        if not v[1] then
            return false
        end
        return ('num:%s'):format(v[1])
    end
    if v.type == 'table' then
        return ('table:%s@%d'):format(guide.getUri(v), v.start)
    end
    if v.type == 'function' then
        return ('func:%s@%d'):format(guide.getUri(v), v.start)
    end
    return false
end

---@param a vm.object?
---@param b vm.object?
---@return boolean|nil
function vm.equal(a, b)
    if not a or not b then
        return false
    end
    local nodeA = vm.compileNode(a)
    local nodeB = vm.compileNode(b)
    local mapA = {}
    for obj in nodeA:eachObject() do
        local unique = getUnique(obj)
        if not unique then
            return nil
        end
        mapA[unique] = true
    end
    for obj in nodeB:eachObject() do
        local unique = getUnique(obj)
        if not unique then
            return nil
        end
        if not mapA[unique] then
            return false
        end
    end
    return true
end

---@param v vm.object?
---@return integer?
function vm.getInteger(v)
    if not v then
        return nil
    end
    local node = vm.compileNode(v)
    local result
    for n in node:eachObject() do
        if n.type == 'integer' then
            if result then
                return nil
            else
                result = n[1]
            end
        elseif n.type == 'number' then
            if result then
                return nil
            elseif not math.tointeger(n[1]) then
                return nil
            else
                result = math.tointeger(n[1])
            end
        elseif n.type ~= 'local'
        and    n.type ~= 'global' then
            return nil
        end
    end
    return result
end

---@param v vm.object?
---@return integer?
function vm.getString(v)
    if not v then
        return nil
    end
    local node = vm.compileNode(v)
    local result
    for n in node:eachObject() do
        if n.type == 'string' then
            if result then
                return nil
            else
                result = n[1]
            end
        elseif n.type ~= 'local'
        and    n.type ~= 'global' then
            return nil
        end
    end
    return result
end

---@param v vm.object?
---@return number?
function vm.getNumber(v)
    if not v then
        return nil
    end
    local node = vm.compileNode(v)
    local result
    for n in node:eachObject() do
        if n.type == 'number'
        or n.type == 'integer' then
            if result then
                return nil
            else
                result = n[1]
            end
        elseif n.type ~= 'local'
        and    n.type ~= 'global' then
            return nil
        end
    end
    return result
end

---@param v vm.object
---@return boolean|nil
function vm.getBoolean(v)
    if not v then
        return nil
    end
    local node = vm.compileNode(v)
    local result
    for n in node:eachObject() do
        if n.type == 'boolean' then
            if result then
                return nil
            else
                result = n[1]
            end
        elseif n.type ~= 'local'
        and    n.type ~= 'global' then
            return nil
        end
    end
    return result
end

---@param v vm.object
---@return table<any, boolean>?
function vm.getLiterals(v)
    if not v then
        return nil
    end
    local map
    local node = vm.compileNode(v)
    for n in node:eachObject() do
        local literal
        if n.type == 'boolean'
        or n.type == 'string'
        or n.type == 'number'
        or n.type == 'integer' then
            literal = n[1]
        end
        if n.type == 'doc.type.string'
        or n.type == 'doc.type.integer'
        or n.type == 'doc.type.boolean' then
            literal = n[1]
        end
        if literal ~= nil then
            if not map then
                map = {}
            end
            map[literal] = true
        end
    end
    return map
end