-
Notifications
You must be signed in to change notification settings - Fork 1
/
explainer.py
123 lines (113 loc) · 4.34 KB
/
explainer.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from typing import List
from lang_ast import AST
from nodes import Node, nodes
class Explainer(object):
def __init__(self, code: str, arg_types: List[type], indent: int=0, remaining: int=0):
self.code = code
self.arg_types = arg_types
self.indent = indent
if remaining == 0:
remaining = self.code.count(b"\n")
self.remaining = remaining
self.tokens = self.parse_code(self.code)
def __str__(self):
rtn = self.to_str(self.tokens).split("\n")
max_len = max(len(line) for line in rtn)
return "\n".join("{} - ".format(line.ljust(max_len)) for line in rtn)
def to_str(self, tokens, indent=0):
rtn = []
for i in tokens:
line = i["char"]
extend = ""
for arg in i["args"]:
if isinstance(arg, list):
if len(arg) == 1:
line += arg[0]["char"]
else:
new = self.to_str(arg, indent+len(line))
line += new.replace(" ", "").replace("\n", "")
if new[-1] in "()":
new = new[:-1].rstrip()
extend = "\n{}".format(new)
else:
line += arg
rtn.append(" "*indent+line+extend)
indent += len(line)
return "\n".join(rtn)
def parse_code(self, code):
rtn = []
while code:
new = {}
new_code, node = AST._add_node(code)
diff = code[:-len(new_code)]
if diff == b"":
diff = code
if diff.startswith(b"."):
if not node.ignore_dot:
new["char"] = diff[:2].decode("ascii")
diff = diff[2:]
else:
new["char"] = diff[:1].decode("ascii")
diff = diff[1:]
func = node.__init__
annotations = func.__annotations__
arg_names = func.__code__.co_varnames[1:func.__code__.co_argcount]
new["args"] = []
for arg in arg_names:
if arg in annotations:
const_arg = annotations[arg]
arg_node = nodes[const_arg]
new_diff, results = node.add_const_arg(diff[:], arg_node, const_arg)
if new_diff is None:
continue
if len(new_diff) != 0:
diff = diff[:-len(new_diff)]
if annotations[arg] in (Node.EvalLiteral, Node.NodeSingle, Node.NodeClass):
new["args"].append(self.parse_code(diff))
else:
new["args"].append(diff.decode("utf-8"))
diff = new_diff
code = new_code
rtn.append(new)
return rtn
def optimise(code):
rtn = bytearray()
while code:
new_code, node = AST._add_node(code)
diff = code[:-len(new_code)]
if diff == b"":
diff = code
if diff.startswith(b"."):
if not node.ignore_dot:
diff[1] |= 0x80
rtn.append(diff[1])
diff = diff[2:]
else:
rtn.append(diff[0])
diff = diff[1:]
func = node.__init__
annotations = func.__annotations__
node_types = annotations.values()
arg_names = func.__code__.co_varnames[1:func.__code__.co_argcount]
if Node.EvalLiteral in node_types or\
Node.NodeSingle in node_types or\
Node.NodeClass in node_types:
for arg in arg_names:
if arg in annotations:
const_arg = annotations[arg]
arg_node = nodes[const_arg]
new_diff, results = node.add_const_arg(diff[:], arg_node, const_arg)
if len(new_diff) != 0:
diff = diff[:-len(new_diff)]
if annotations[arg] in (Node.EvalLiteral, Node.NodeSingle, Node.NodeClass):
rtn.extend(optimise(diff))
else:
rtn.extend(diff)
diff = new_diff
else:
rtn.extend(diff)
code = new_code
return rtn
if __name__ == '__main__':
e = Explainer("hF1_P\n(P", arg_types=[int])
print(e)