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
|
local function getDefaultSource()
return { start = 0, finish = 0 }
end
local mt = {}
mt.__index = mt
mt.type = 'value'
function mt:setValue(value)
self._value = value
end
function mt:getValue()
return self._value
end
function mt:inference(tp, rate)
if type(tp) == 'table' then
for _, ctp in ipairs(tp) do
self:inference(ctp, rate)
end
return
end
if tp == '...' then
error('Value type cant be ...')
end
if not tp then
tp = 'nil'
end
if tp == 'any' or tp == 'nil' then
rate = 0.0
end
if not self._type then
self._type = {}
end
local current = self._type[tp] or 0.0
self._type[tp] = current + (1 - current) * rate
end
function mt:getType()
if not self._type then
return 'nil'
end
local mRate = 0.0
local mType
for tp, rate in pairs(self._type) do
if rate > mRate then
mRate = rate
mType = tp
end
end
return mType or 'any'
end
function mt:setMetaTable(metatable, source)
if not self._meta then
self._meta = {}
end
local uri = source and source.uri or ''
self._meta[uri] = metatable
end
function mt:createField(name, source)
local field = {
type = 'field',
key = name,
source = source or getDefaultSource(),
}
if not self._child then
self._child = {}
end
local uri = source and source.uri or ''
if not self._child[uri] then
self._child[uri] = {}
end
self._child[uri][name] = field
self:inference('table', 0.5)
return field
end
function mt:rawGetField(name, source)
local uri = source and source.uri or ''
if not self._child then
return nil
end
if self._child[uri] then
local field = self._child[uri][name]
if field then
return field
end
end
for _, childs in pairs(self._child) do
local field = childs[name]
if field then
return field
end
end
return nil
end
function mt:getMeta(name, source)
if not self._meta then
return nil
end
local uri = source and source.uri or ''
if self._meta[uri] then
local meta = self._meta[uri]:rawGetField(name, source)
if meta then
return meta
end
end
for _, indexValue in pairs(self._meta) do
local meta = indexValue:rawGetField(name, source)
if meta then
return meta
end
end
return nil
end
function mt:getChild()
if not self._child then
self._child = {}
end
return self._child
end
function mt:setChild(child)
self._child = child
end
function mt:getField(name, source, stack)
local field = self:rawGetField(name, source)
if not field then
local indexMeta = self:getMeta('__index', source)
if not indexMeta then
return nil
end
if not stack then
stack = 0
end
stack = stack + 1
if stack > 10 then
return nil
end
return indexMeta.value:getField(name, source, stack)
end
return field
end
function mt:rawEachField(callback, mark)
if not self._child then
return nil
end
if not mark then
mark = {}
end
for _, childs in pairs(self._child) do
for name, field in pairs(childs) do
if not mark[name] then
mark[name] = true
local res = callback(name, field)
if res ~= nil then
return res
end
end
end
end
return nil
end
function mt:eachField(callback, mark, stack)
if not mark then
mark = {}
end
local res = self:rawEachField(callback, mark)
if res ~= nil then
return res
end
local indexMeta = self:getMeta('__index')
if not indexMeta then
return nil
end
if not stack then
stack = 0
end
stack = stack + 1
if stack > 10 then
return nil
end
return indexMeta.value:eachField(callback, mark, stack)
end
function mt:removeUri(uri)
if self._child then
self._child[uri] = nil
self:rawEachField(function (key, field)
field.value:removeUri(uri)
end)
end
if self._info then
self._info[uri] = nil
end
if self._meta then
self._meta[uri] = nil
end
end
function mt:getDeclarat()
if not self._info then
return nil
end
local uri = self.uri or ''
local infos = self._info[uri]
if not infos then
return nil
end
for _, info in ipairs(infos) do
if info.type == 'local' then
return info.source
end
end
for _, info in ipairs(infos) do
if info.type == 'return' then
return info.source
end
end
for _, info in ipairs(infos) do
if info.type == 'set' then
return info.source
end
end
return nil
end
function mt:addInfo(tp, source)
if source and not source.start then
error('Miss start: ' .. table.dump(source))
end
if not self._info then
self._info = {}
end
local uri = source and source.uri or ''
if not self._info[uri] then
self._info[uri] = {}
end
self._info[uri][#self._info[uri]+1] = {
type = tp,
source = source or getDefaultSource(),
}
return self
end
function mt:eachInfo(callback)
if not self._info then
return nil
end
for _, infos in pairs(self._info) do
for i = 1, #infos do
local res = callback(infos[i])
if res ~= nil then
return res
end
end
end
return nil
end
return function (tp, source, value)
if tp == '...' then
error('Value type cant be ...')
end
-- TODO lib里的多类型
local self = setmetatable({
source = source or getDefaultSource(),
uri = source and source.uri,
}, mt)
if value ~= nil then
self:setValue(value)
end
if type(tp) == 'table' then
for i = 1, #tp do
self:inference(tp[i], 0.9)
end
else
self:inference(tp, 1.0)
end
return self
end
|