-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcollect.py
58 lines (54 loc) · 1.55 KB
/
collect.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
from convert import *
valued_opcodes = set('PUSH_WORD PUSH_LITERAL SET SET_LOCAL SET_GLOBAL GET GET_GLOBAL SOURCE_FILE'.split())
class Contain(object):
def __init__(self, t, value):
self.type = t
self.value = value
self.count = 1
self.index = None
class Bucket(object):
def __init__(self):
self.bucket = []
def get_value_and_type(self, v):
if isinstance(v, (ProperWord, Ident)):
b = 'ident'
elif isinstance(v, Number):
b = 'num'
elif isinstance(v, String):
b = 'str'
elif isinstance(v, Fraction):
b = 'frac'
elif isinstance(v, str):
return v, 'ident'
return v.value, b
def add(self, v):
value, b = self.get_value_and_type(v)
for item in self.bucket:
if item.value == value and item.type == b: #need to disambiguate str and ident
item.count += 1
break
else:
self.bucket.append(Contain(b, value))
def get(self, v):
value, b = self.get_value_and_type(v)
for item in self.bucket:
if item.value == value and item.type == b: #need to disambiguate str and ident
return item.index
def sort(self):
self.bucket.sort(key=lambda x: x.count, reverse=True)
def number(self):
for i, item in enumerate(self.bucket):
item.index = i
def raw(self):
return [(p.type, p.value) for p in self.bucket]
def collect(flat_file):
buck = Bucket()
for instruction in flat_file:
if instruction.opcode in valued_opcodes:
buck.add(instruction.ref)
buck.sort()
buck.number()
for instruction in flat_file:
if instruction.opcode in valued_opcodes:
instruction.ref = buck.get(instruction.ref)
return flat_file, buck.raw()