-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_manager.py
More file actions
39 lines (31 loc) · 1.3 KB
/
heap_manager.py
File metadata and controls
39 lines (31 loc) · 1.3 KB
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
class HeapManager:
def __init__(self):
self.first_free = 500
self.variables = {}
# heap manager is called twice for arrays. firstly with the declare_id and secondly
# with modify attributes. after the second call with have to modify array type for address of array.
# last_assigned is used for this.
self.last_temp = None
def get_temp(self, type_name, size=1, array_attribute=False):
# return address of the first free cell
temp_address = self.first_free
if array_attribute:
self.last_temp.type_name += "-arr"
for i in range(size):
temp = TempVariable(type_name, self.first_free, False)
self.variables[self.first_free] = temp
self.first_free += self.get_length_by_type(type_name)
self.last_temp = temp
return temp_address
@staticmethod
def get_length_by_type(type_name):
if type_name == "int":
return 4
elif type_name == "void":
return 1
def get_type_by_address(self, address):
return self.variables[address].type_name
class TempVariable:
def __init__(self, type_name, address, array_attribute):
self.type_name = type_name
self.address = address