mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 23:58:20 +00:00
Collect used library functions as reusable names for the renamer.
Implements another TODO. There was a TODO about a new counter per scope, but that makes no sense. The renamer only acts on global variables, global function and parameter names, state names, and event parameters. We're already restarting the counters at every function, which is the closest to what that TODO was about.
This commit is contained in:
parent
93828b9286
commit
1cdf9f7ff0
3 changed files with 22 additions and 12 deletions
|
@ -23,13 +23,9 @@
|
|||
#
|
||||
# A side effect of this change is that the script becomes unreadable gibberish.
|
||||
|
||||
# TODO: Make a new counter per scope.
|
||||
# TODO: Reuse used library function names for UDF and event parameters.
|
||||
|
||||
class renamer(object):
|
||||
CharSet1 = '_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
|
||||
CharSet2 = '0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
|
||||
# TODO: Derive these from builtins.txt somehow.
|
||||
Kws = frozenset({'do', 'if', 'PI',
|
||||
'for', 'key', 'EOF',
|
||||
'jump', 'else', 'list', 'TRUE', 'LOOP', 'case'
|
||||
|
@ -54,7 +50,7 @@ class renamer(object):
|
|||
if ret not in self.Kws and ret not in self.UsedNames:
|
||||
return ret
|
||||
|
||||
def ShrinkNames(self):
|
||||
def ShrinkNames(self, UsableAsGlobals = None, UsableAsParams = None):
|
||||
"""Implements the shrinknames option."""
|
||||
self.WordFirstChar = 0
|
||||
self.WordRestOfChars = []
|
||||
|
@ -66,11 +62,13 @@ class renamer(object):
|
|||
'System', 'UThread', 'UThreadStackFrame', 'Pop',
|
||||
'IsRestoring', 'IsSaveDue', 'ResumeVoid',
|
||||
])
|
||||
if UsableAsGlobals is not None:
|
||||
ReusableNames |= UsableAsGlobals
|
||||
|
||||
# Names from ReusableNames that have already been used
|
||||
self.UsedNames = set()
|
||||
|
||||
# Make a first pass to separate the symbols into three categories.
|
||||
# Make a preliminary pass to separate the symbols into three categories
|
||||
globalvars = []
|
||||
states = []
|
||||
functions = []
|
||||
|
@ -89,8 +87,8 @@ class renamer(object):
|
|||
assert False, 'Invalid kind at this scope: ' \
|
||||
+ kind # pragma: no cover
|
||||
|
||||
# We make four passes, one for states, then functions, then globals,
|
||||
# then parameter names and locals, in that order.
|
||||
# We make four passes, one for states, then function names, then
|
||||
# global names, then parameter names and locals, in that order.
|
||||
|
||||
# State names are usable as short identifiers for parameters.
|
||||
stateNames = []
|
||||
|
@ -159,6 +157,8 @@ class renamer(object):
|
|||
|
||||
# Do the same for function and event parameter names. Pure locals get
|
||||
# long distinct names.
|
||||
if UsableAsParams is not None:
|
||||
ReusableNames |= UsableAsParams
|
||||
First = True
|
||||
restart = self.WordFirstChar
|
||||
restartReusable = ReusableNames
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue