@@ -6,8 +6,6 @@ local vm = require 'vm.vm'
66--- @class vm.variable
77--- @field sets parser.object[]
88--- @field gets parser.object[]
9- --- @field node ? vm.node
10- --- @field type ' global' | ' local'
119
1210--- @class parser.object
1311--- @field package _variableID string | false
@@ -19,7 +17,7 @@ local compileSwitch = util.switch()
1917 : case ' local'
2018 : case ' self'
2119 : call (function (source )
22- source ._variableID = (' l| %d' ):format (source .start )
20+ source ._variableID = (' %d' ):format (source .start )
2321 if not source .ref then
2422 return
2523 end
@@ -30,7 +28,7 @@ local compileSwitch = util.switch()
3028 : case ' setlocal'
3129 : case ' getlocal'
3230 : call (function (source )
33- source ._variableID = (' l| %d' ):format (source .node .start )
31+ source ._variableID = (' %d' ):format (source .node .start )
3432 compileVariableID (source .next )
3533 end )
3634 : case ' getfield'
@@ -123,7 +121,6 @@ function vm.insertVariableID(id, source)
123121 if not root ._variableIDs then
124122 root ._variableIDs = util .multiTable (2 , function (head )
125123 return {
126- type = head :sub (1 , 1 ) == ' l' and ' local' or ' global' ,
127124 sets = {},
128125 gets = {},
129126 }
@@ -154,18 +151,33 @@ function compileVariableID(source)
154151end
155152
156153--- @param source parser.object
157- --- @return string | false
154+ --- @return string ?
158155function vm .getVariableID (source )
159156 if source ._variableID ~= nil then
160- return source ._variableID
157+ return source ._variableID or nil
161158 end
162159 source ._variableID = false
163160 local loc = getVariable (source )
164161 if not loc then
165- return source ._variableID
162+ return source ._variableID or nil
166163 end
167164 compileVariableID (loc )
168- return source ._variableID
165+ return source ._variableID or nil
166+ end
167+
168+ --- @param source parser.object
169+ --- @return string ?
170+ function vm .getVariableName (source )
171+ local id = vm .getVariableID (source )
172+ if not id then
173+ return nil
174+ end
175+ local head = vm .getVariableHead (source )
176+ if not head then
177+ return nil
178+ end
179+ local name = id :gsub (' %d+' , head [1 ]):gsub (vm .ID_SPLITE , ' .' )
180+ return name
169181end
170182
171183--- @param source parser.object
@@ -248,3 +260,20 @@ function vm.getVariableFields(source, includeGets)
248260 end
249261 return fields
250262end
263+
264+ --- @param source parser.object
265+ --- @return parser.object ?
266+ function vm .getVariableHead (source )
267+ local id = vm .getVariableID (source )
268+ if not id then
269+ return nil
270+ end
271+ for _ = 1 , 1000 do
272+ if source .type == ' local'
273+ or source .type == ' self' then
274+ return source
275+ end
276+ source = source .node
277+ end
278+ return nil
279+ end
0 commit comments