1
+ import string
1
2
from abc import ABC , abstractmethod
2
3
from enum import Enum
3
4
from typing import List , Set
@@ -18,9 +19,9 @@ def get_variables(self) -> list[Variable]:
18
19
"""Get collected variables."""
19
20
return self ._variables
20
21
21
- def visit_variable (self , expression : Variable ):
22
+ def visit_variable (self , var : Variable ):
22
23
"""Add visited variables to list"""
23
- self ._variables .append (expression )
24
+ self ._variables .append (var )
24
25
25
26
26
27
class NamingConvention (str , Enum ):
@@ -36,9 +37,9 @@ class RenamingScheme(ABC):
36
37
def __init__ (self , task : DecompilerTask ) -> None :
37
38
"""Collets all needed variables for renaming + filters which should not be renamed"""
38
39
collector = VariableCollector ()
39
- collector .visit_ast (task ._ast )
40
- self ._ast = task ._ast
41
- self ._params : List [Variable ] = task ._function_parameters
40
+ collector .visit_ast (task .syntax_tree )
41
+ self ._ast = task .syntax_tree
42
+ self ._params : List [Variable ] = task .function_parameters
42
43
self ._variables : Set [Variable ] = set (filter (self ._filter_variables , collector .get_variables ()))
43
44
44
45
@@ -54,7 +55,7 @@ def renameVariables(self):
54
55
"""Rename all collected variables with a naming scheme."""
55
56
names : dict [Variable , Variable ] = {}
56
57
for var in self ._variables :
57
- names [var ] = var .copy (self .getVariableName (var ))
58
+ names [var ] = var .copy (name = self .getVariableName (var ))
58
59
59
60
self ._ast .replace_variables_in_subtree (self ._ast .root , names )
60
61
@@ -93,18 +94,19 @@ def _get_counter(self, var: Variable) -> Integer:
93
94
94
95
95
96
def _get_name_identifier (self , name : str ) -> str :
96
- """Return identifier by purging non alpha chars + capitalize the string . If string is too short, return generic"""
97
+ """Return identifier by purging non alpha chars + capitalize the char afterwards . If string is too short, return generic"""
97
98
if len (name ) < 2 :
98
- return "Var"
99
- return "" .join (filter (str .isalpha , name )).capitalize ()
99
+ return "var"
100
+ x = string .capwords ("" .join ([c if c .isalnum () else " " for c in name ]))
101
+ x = x [0 ].lower () + x [1 :]
102
+ return "" .join (filter (str .isalpha , x ))
100
103
101
104
102
105
def getVariableName (self , var : Variable ) -> str :
103
106
"""Return hungarian notation to a given variable. If no prefix exists, make the first char lower case."""
104
107
newName = self ._get_name_identifier (var ._name )
105
108
if (prefix := self ._hungarian_prefix (var .type )):
106
- return f"{ prefix } { self ._type_separator } { newName } { self ._counter_separator } { self ._get_counter (var .name )} "
107
- newName = newName [0 ].lower () + newName [1 :]
109
+ return f"{ prefix } { self ._type_separator } { newName .capitalize ()} { self ._counter_separator } { self ._get_counter (var .name )} "
108
110
return f"{ newName } { self ._counter_separator } { self ._get_counter (var .name )} "
109
111
110
112
def _hungarian_prefix (self , var_type : Type ) -> str :
0 commit comments