-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathannotate.py
115 lines (103 loc) · 2.33 KB
/
annotate.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
import struct
from bytecode import OPCODES, unsigned_int_s, signed_int_s, double_s, positional_instructions
DECODE_OPCODES = {}
for k in OPCODES:
DECODE_OPCODES[OPCODES[k] / 0x1000000] = k
WORD_ARG = set('GET SET GET_GLOBAL SET_GLOBAL SET_LOCAL PUSH_LITERAL PUSH_WORD SOURCE_FILE'.split())
POS_ARG = positional_instructions
LIT_ARG = set('PUSH_INTEGER LINE_NUMBER'.split())
clb = '\033[%sm'
header = clb % 96
opcode = clb % 0
arg_index = clb % 93
arg_literal = clb % 94
arg_offset = clb % 91
arg_nothing = clb % 30
literal_type = clb % 0
literal_str = clb % 31
literal_ident = clb % 32
literal_num = clb % 34
literal_frac = clb % 35
literal_list = clb % 33
literal_dict = clb % 36
end = clb % 0
def unsigned_int(x):
return unsigned_int_s.unpack(x)[0]
_count = -1
def nl():
global _count
_count += 1
if _count == 8:
_count = 0
return '\n'
else:
return ''
def hexify(s):
return ' '.join(nl() + '%02x' % ord(x) for x in s) + ' '
def ann(text):
yield header
yield hexify(text[:8])
size = unsigned_int(text[4:8])
text = text[8:]
for j in range(0, size * 4, 4):
yield opcode
yield hexify(text[j])
op = DECODE_OPCODES[ord(text[j])]
if op in WORD_ARG:
yield arg_index
elif op in LIT_ARG:
yield arg_literal
elif op in POS_ARG:
yield arg_offset
else:
yield arg_nothing
yield hexify(text[j + 1:j + 4])
text = text[size * 4:]
i = 0
while i < len(text):
yield literal_type
s = text[i]
yield hexify(s)
if s == '\x02':
j = 9
yield literal_num
elif s == '\x82':
j = 4
yield literal_num
elif s == '\x07':
j = 17
yield literal_frac
elif s == '\x87':
j = 3
yield literal_frac
elif s == '\x03':
j = unsigned_int(text[i+1:i+5]) * 3 + 5
yield literal_list
elif s == '\x83':
j = ord(text[i+1]) * 3 + 2
yield literal_list
elif s in ('\x05', '\x85'):
if s == '\x05':
j = unsigned_int(text[i+1:i+5]) * 6 + 5
else:
j = ord(text[i+1]) * 6 + 2
yield literal_dict
elif s < '\x80':
j = unsigned_int(text[i+1:i+5]) + 5
if s == '\x00':
yield literal_ident
elif s == '\x01':
yield literal_str
else:
j = ord(text[i+1]) + 2
if s == '\x80':
yield literal_ident
elif s == '\x81':
yield literal_str
yield hexify(text[i+1:i+j])
i += j
yield '\n'
yield end
if __name__ == '__main__':
import sys
sys.stdout.write(''.join(ann(sys.stdin.read())))