-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
64 lines (54 loc) · 2.16 KB
/
util.py
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
import statements
def starts_with(x, y):
if len(y) > len(x): return False
return x[:len(y)] == y
def is_numeral(x):
if len(x) == 0: return False
while len(x) > 0:
if x[0] > '9' or x[0] < '0': return False
x = x[1:]
return True
def is_string(x):
if len(x) < 2: return False
if not x[0] == '"' or not x[-1] == '"': return False
esc = False
for c in x[1:-1]:
if not esc and (c == '"'): return False
esc = not esc and (c == '\\')
return not esc
def is_address(x): # [0-9A-Za-z.$_-]+@[0-9A-Za-z.$_-]
if len(x) < 3 or x[0] == '@': return False
at = False
for c in x:
if c == '@' and at: return False
if c >= '0' and c <= '9': continue
if c >= 'A' and c <= 'Z': continue
if c >= 'a' and c <= 'z': continue
if not (c == '.' or c == '$' or c == '_' or c == '-' or c == '@'): return False
at = at or c == '@'
return (not x[-1] == '@') and at
def to_var_name(x):
return '_v' + x.replace('_', '_0').replace('-', '_1').replace('.', '_2').replace('$', '_3')
def addr_to_var_name(addr):
if addr.domain == "global" and addr.user == "main": return "main"
if addr.domain == "local": return to_var_name(addr.user)
if addr.domain == "arguments":
if is_numeral(addr.user): return "_a_" + addr.user
else: return "arguments[" + addr.user + "]"
return to_var_name(addr.domain) + '_4' + to_var_name(addr.user)
def max_with_def(lst, default):
lst = list(lst)
if len(lst) == 0: return default
return max(lst)
def max_arg_index_value(value):
if isinstance(value, statements.Addr) and value.domain == "arguments" and is_numeral(value.user):
return int(value.user)
return -1
def max_arg_index_statement(stmt):
if isinstance(stmt, statements.VariableSet):
return max([max_arg_index_value(stmt.lhs), max_arg_index_value(stmt.rhs)])
elif isinstance(stmt, statements.FunctionCall):
aliases = max_with_def(map(max_arg_index_value, stmt.aliases), -1)
args = max_with_def(map(max_arg_index_value, stmt.args), -1)
return max(aliases, args, max_arg_index_value(stmt.func_name))
else: return -1