-
Notifications
You must be signed in to change notification settings - Fork 6
/
heval.py
62 lines (58 loc) · 1.9 KB
/
heval.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
import sys, json
def heval_kv(js):
return (heval_string(js["key"]), heval_string(js["value"]))
def heval_idx(js):
return "[" + heval_string(js) + "]"
def heval_string(js):
type = js["type"]
if type == "address":
if "func" not in js:
return "None"
result = "?"
func = js["func"]
args = js["args"]
if func["type"] == "pc":
if int(func["value"]) == -1:
result += args[0]["value"]
args = args[1:]
elif int(func["value"]) == -2:
result += "@" + args[0]["value"]
args = args[1:]
else:
result += heval_string(func)
else:
result += heval_string(func)
return result + "".join([ heval_idx(kv) for kv in args ])
v = js["value"]
if type == "bool":
return v
if type == "int":
return str(v) if isinstance(v, int) else v
if type == "atom":
return json.dumps(v, ensure_ascii=False)
if type == "set":
if v == []:
return "{}"
lst = [ heval_string(val) for val in v ]
return "{ " + ", ".join(lst) + " }"
if type == "list":
if v == []:
return "[]"
lst = [ heval_string(val) for val in v ]
return "[ " + ", ".join(lst) + " ]"
if type == "dict":
if v == []:
return "{:}"
lst = [ heval_kv(kv) for kv in v ]
keys = [ k for k,v in lst ]
if keys == [str(i) for i in range(len(v))]:
return "[ " + ", ".join([v for k,v in lst]) + " ]"
else:
return "{ " + ", ".join([k + ": " + v for k,v in lst]) + " }"
if type == "pc":
return "PC(%s)"%v
if type == "context":
return "CONTEXT(" + str(v["pc"]) + ")"
with open(sys.argv[1], encoding='utf-8') as f:
top = json.load(f, strict=False)
print(heval_string(top["symbols"]["1"]))