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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
local util = require 'utility'
local guide = require 'parser.guide'
---@class vm
local vm = require 'vm.vm'
---@class vm.variable
---@field uri uri
---@field root parser.object
---@field id string
---@field base parser.object
---@field sets parser.object[]
---@field gets parser.object[]
local mt = {}
mt.__index = mt
mt.type = 'variable'
---@param id string
---@return vm.variable
local function createVariable(root, id)
local variable = setmetatable({
root = root,
uri = root.uri,
id = id,
sets = {},
gets = {},
}, mt)
return variable
end
---@class parser.object
---@field package _variableNode vm.variable|false
---@field package _variableNodes table<string, vm.variable>
local compileVariables, getLoc
---@param id string
---@param source parser.object
---@param base parser.object
---@return vm.variable
local function insertVariableID(id, source, base)
local root = guide.getRoot(source)
if not root._variableNodes then
root._variableNodes = util.multiTable(2, function (lid)
local variable = createVariable(root, lid)
return variable
end)
end
local variable = root._variableNodes[id]
variable.base = base
if guide.isAssign(source) then
variable.sets[#variable.sets+1] = source
else
variable.gets[#variable.gets+1] = source
end
return variable
end
local compileSwitch = util.switch()
: case 'local'
: case 'self'
: call(function (source, base)
local id = ('%d'):format(source.start)
local variable = insertVariableID(id, source, base)
source._variableNode = variable
if not source.ref then
return
end
for _, ref in ipairs(source.ref) do
compileVariables(ref, base)
end
end)
: case 'setlocal'
: case 'getlocal'
: call(function (source, base)
local id = ('%d'):format(source.node.start)
local variable = insertVariableID(id, source, base)
source._variableNode = variable
compileVariables(source.next, base)
end)
: case 'getfield'
: case 'setfield'
: call(function (source, base)
local parentNode = source.node._variableNode
if not parentNode then
return
end
local key = guide.getKeyName(source)
if type(key) ~= 'string' then
return
end
local id = parentNode.id .. vm.ID_SPLITE .. key
local variable = insertVariableID(id, source, base)
source._variableNode = variable
source.field._variableNode = variable
if source.type == 'getfield' then
compileVariables(source.next, base)
end
end)
: case 'getmethod'
: case 'setmethod'
: call(function (source, base)
local parentNode = source.node._variableNode
if not parentNode then
return
end
local key = guide.getKeyName(source)
if type(key) ~= 'string' then
return
end
local id = parentNode.id .. vm.ID_SPLITE .. key
local variable = insertVariableID(id, source, base)
source._variableNode = variable
source.method._variableNode = variable
if source.type == 'getmethod' then
compileVariables(source.next, base)
end
end)
: case 'getindex'
: case 'setindex'
: call(function (source, base)
local parentNode = source.node._variableNode
if not parentNode then
return
end
local key = guide.getKeyName(source)
if type(key) ~= 'string' then
return
end
local id = parentNode.id .. vm.ID_SPLITE .. key
local variable = insertVariableID(id, source, base)
source._variableNode = variable
source.index._variableNode = variable
if source.type == 'setindex' then
compileVariables(source.next, base)
end
end)
local leftSwitch = util.switch()
: case 'field'
: case 'method'
: call(function (source)
return getLoc(source.parent)
end)
: case 'getfield'
: case 'setfield'
: case 'getmethod'
: case 'setmethod'
: case 'getindex'
: case 'setindex'
: call(function (source)
return getLoc(source.node)
end)
: case 'getlocal'
: call(function (source)
return source.node
end)
: case 'local'
: case 'self'
: call(function (source)
return source
end)
---@param source parser.object
---@return parser.object?
function getLoc(source)
return leftSwitch(source.type, source)
end
---@return parser.object
function mt:getBase()
return self.base
end
---@return string
function mt:getCodeName()
local name = self.id:gsub(vm.ID_SPLITE, '.'):gsub('^%d+', self.base[1])
return name
end
---@return vm.variable?
function mt:getParent()
local parentID = self.id:match('^(.+)' .. vm.ID_SPLITE)
if not parentID then
return nil
end
return self.root._variableNodes[parentID]
end
---@return string?
function mt:getFieldName()
return self.id:match(vm.ID_SPLITE .. '(.-)$')
end
---@param key? string
function mt:getSets(key)
if not key then
return self.sets
end
local id = self.id .. vm.ID_SPLITE .. key
local variable = self.root._variableNodes[id]
return variable.sets
end
---@param includeGets boolean?
function mt:getFields(includeGets)
local id = self.id
local root = self.root
-- TODO:optimize
local clock = os.clock()
local fields = {}
for lid, variable in pairs(root._variableNodes) do
if lid ~= id
and util.stringStartWith(lid, id)
and lid:sub(#id + 1, #id + 1) == vm.ID_SPLITE
-- only one field
and not lid:find(vm.ID_SPLITE, #id + 2) then
for _, src in ipairs(variable.sets) do
fields[#fields+1] = src
end
if includeGets then
for _, src in ipairs(variable.gets) do
fields[#fields+1] = src
end
end
end
end
local cost = os.clock() - clock
if cost > 1.0 then
log.warn('variable-id getFields takes %.3f seconds', cost)
end
return fields
end
---@param source parser.object
---@param base parser.object
function compileVariables(source, base)
if not source then
return
end
source._variableNode = false
if not compileSwitch:has(source.type) then
return
end
compileSwitch(source.type, source, base)
end
---@param source parser.object
---@return string?
function vm.getVariableID(source)
local variable = vm.getVariableNode(source)
if not variable then
return nil
end
return variable.id
end
---@param source parser.object
---@param key? string
---@return vm.variable?
function vm.getVariable(source, key)
local variable = vm.getVariableNode(source)
if not variable then
return nil
end
if not key then
return variable
end
local root = guide.getRoot(source)
if not root._variableNodes then
return nil
end
local id = variable.id .. vm.ID_SPLITE .. key
return root._variableNodes[id]
end
---@param source parser.object
---@return vm.variable?
function vm.getVariableNode(source)
local variable = source._variableNode
if variable ~= nil then
return variable or nil
end
source._variableNode = false
local loc = getLoc(source)
if not loc then
return nil
end
compileVariables(loc, loc)
return source._variableNode or nil
end
---@param source parser.object
---@param name string
---@return vm.variable?
function vm.getVariableInfoByCodeName(source, name)
local id = vm.getVariableID(source)
if not id then
return nil
end
local root = guide.getRoot(source)
if not root._variableNodes then
return nil
end
local headPos = name:find('.', 1, true)
if not headPos then
return root._variableNodes[id]
end
local vid = id .. name:sub(headPos):gsub('%.', vm.ID_SPLITE)
return root._variableNodes[vid]
end
---@param source parser.object
---@param key? string
---@return parser.object[]?
function vm.getVariableSets(source, key)
local variable = vm.getVariable(source, key)
if not variable then
return nil
end
return variable.sets
end
---@param source parser.object
---@param key? string
---@return parser.object[]?
function vm.getVariableGets(source, key)
local variable = vm.getVariable(source, key)
if not variable then
return nil
end
return variable.gets
end
---@param source parser.object
---@param includeGets boolean
---@return parser.object[]?
function vm.getVariableFields(source, includeGets)
local variable = vm.getVariable(source)
if not variable then
return nil
end
return variable:getFields(includeGets)
end
---@param source parser.object
---@return boolean
function vm.compileByVariable(source)
local variable = vm.getVariableNode(source)
if not variable then
return false
end
vm.setNode(source, variable)
return true
end
---@param source parser.object
local function compileSelf(source)
if source.parent.type ~= 'funcargs' then
return
end
---@type parser.object
local node = source.parent.parent and source.parent.parent.parent and source.parent.parent.parent.node
if not node then
return
end
local fields = vm.getVariableFields(source, false)
if not fields then
return
end
local variableNode = vm.getVariableNode(node)
local globalNode = vm.getGlobalNode(node)
if not variableNode and not globalNode then
return
end
for _, field in ipairs(fields) do
if field.type == 'setfield' then
local key = guide.getKeyName(field)
if key then
if variableNode then
local myID = variableNode.id .. vm.ID_SPLITE .. key
insertVariableID(myID, field, variableNode.base)
end
if globalNode then
local myID = globalNode:getName() .. vm.ID_SPLITE .. key
local myGlobal = vm.declareGlobal('variable', myID, guide.getUri(node))
myGlobal:addSet(guide.getUri(node), field)
end
end
end
end
end
---@param source parser.object
local function compileAst(source)
--[[
local mt
function mt:xxx()
self.a = 1
end
mt.a --> find this definition
]]
guide.eachSourceType(source, 'self', function (src)
compileSelf(src)
end)
end
return {
compileAst = compileAst,
}
|