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
|
local error = error
_ENV = nil
local m = {}
local blockTypes = {
['while'] = true,
['in'] = true,
['loop'] = true,
['repeat'] = true,
['do'] = true,
['function'] = true,
['ifblock'] = true,
['elseblock'] = true,
['elseifblock'] = true,
['main'] = true,
}
local breakBlockTypes = {
['while'] = true,
['in'] = true,
['loop'] = true,
['repeat'] = true,
}
--- 寻找所在函数
function m.getParentFunction(root, obj)
for _ = 1, 1000 do
obj = root[obj.parent]
if not obj then
break
end
local tp = obj.type
if tp == 'function' then
return obj
end
end
return nil
end
--- 寻找所在区块
function m.getBlock(root, obj)
for _ = 1, 1000 do
if not obj then
return nil
end
local tp = obj.type
if blockTypes[tp] then
return obj
end
obj = root[obj.parent]
end
error('guide.getBlock overstack')
end
--- 寻找所在父区块
function m.getParentBlock(root, obj)
for _ = 1, 1000 do
obj = root[obj.parent]
if not obj then
return nil
end
local tp = obj.type
if blockTypes[tp] then
return obj
end
end
error('guide.getParentBlock overstack')
end
--- 寻找所在可break的父区块
function m.getBreakBlock(root, obj)
for _ = 1, 1000 do
obj = root[obj.parent]
if not obj then
return nil
end
local tp = obj.type
if breakBlockTypes[tp] then
return obj
end
if tp == 'function' then
return nil
end
end
error('guide.getBreakBlock overstack')
end
--- 寻找函数的不定参数,返回不定参在第几个参数上,以及该参数对象。
--- 如果函数是主函数,则返回`0, nil`。
---@return table
---@return integer
function m.getFunctionVarArgs(root, func)
if func.type == 'main' then
return 0, nil
end
if func.type ~= 'function' then
return nil, nil
end
local args = root[func.args]
if not args then
return nil, nil
end
for i = 1, #args do
local arg = root[args[i]]
if arg.type == '...' then
return i, arg
end
end
return nil, nil
end
--- 获取指定区块中可见的局部变量
---@param root table
---@param block table
---@param name string {comment = '变量名'}
---@param pos integer {comment = '可见位置'}
function m.getLocal(root, block, name, pos)
block = m.getBlock(root, block)
for _ = 1, 1000 do
if not block then
return nil
end
local locals = block.locals
local res
if not locals then
goto CONTINUE
end
for i = 1, #locals do
local loc = root[locals[i]]
if loc.effect > pos then
break
end
if loc[1] == name then
if not res or res.effect < loc.effect then
res = loc
end
end
end
if res then
return res
end
::CONTINUE::
block = m.getParentBlock(root, block)
end
error('guide.getLocal overstack')
end
--- 获取指定区块中可见的标签
---@param root table
---@param block table
---@param name string {comment = '标签名'}
function m.getLabel(root, block, name)
block = m.getBlock(root, block)
for _ = 1, 1000 do
if not block then
return nil
end
local labels = block.labels
if labels then
local label = labels[name]
if label then
return root[label]
end
end
if block.type == 'function' then
return nil
end
block = m.getParentBlock(root, block)
end
error('guide.getLocal overstack')
end
return m
|