-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol_table.py
174 lines (145 loc) · 5.05 KB
/
symbol_table.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from utils import error_handler
class Symbol:
def __init__(self, var_name, type_of_var, type_of_data, function_name, scope, type_size, size, address):
self.var_name = var_name
self.type_of_var = type_of_var # int char float bool
self.type_of_data = type_of_data # array temp var
self.function = function_name # global or function name
self.scope = scope # scope number
self.type_size = type_size # size of it var
self.size = size # size taken
self.address = address # address
self.released = True
class SymbolTable:
function = "Global"
scope = 0
bitmap = [] # 1 is empty 0 is full
symbols = []
symbols_size = {"void": 0, "int": 4, "char": 2, "double": 8, "bool": 1}
def get_size(self, type_of_var):
if type_of_var in self.symbols_size:
return self.symbols_size[type_of_var]
return -1
def get_more_size(self):
for i in range(100):
self.bitmap.append(1)
def set_function_name(self, func_name):
self.function = func_name
def make_full_bitmap(self, start, size):
for i in range(start, start + size):
self.bitmap[i] = 0
def find_empty_in_bitmap(self, size):
empty_counter = 0
for i in range(0, len(self.bitmap)):
if self.bitmap[i] == 1:
empty_counter += 1
if empty_counter == size:
return i - size + 1
else:
empty_counter = 0
self.get_more_size()
return self.find_empty_in_bitmap(size)
def find_empty_in_bitmap_for_temp(self, size):
empty_counter = 0
for i in range(10000, len(self.bitmap)):
if self.bitmap[i] == 1:
empty_counter += 1
if empty_counter == size:
return i - size + 1
else:
empty_counter = 0
self.get_more_size()
return self.find_empty_in_bitmap_for_temp(size)
def clear_bitmap(self, start, size):
for i in range(start, start + size):
self.bitmap[i] = 1
def new_temp(self, type_of_temp):
size = self.get_size(type_of_temp)
address = self.find_empty_in_bitmap_for_temp(size)
name = "_" + str(address)
temp = Symbol(name, type_of_temp, "temp", self.function, self.scope, size, size, address)
self.symbols.append(temp)
self.make_full_bitmap(address, size)
return name
def new_array(self, name, type_of_var, array_size):
type_size = self.get_size(type_of_var)
size = int(type_size) * int(array_size)
address = self.find_empty_in_bitmap(size)
array = Symbol(name, type_of_var, "array", self.function, self.scope, type_size, size, address)
self.symbols.append(array)
self.make_full_bitmap(address, size)
def new_variable(self, name, type_of_var, scope_update=0):
if name == "xyz" and len(self.symbols) == 2:
print(self.symbols[0].var_name)
print(self.symbols[0].address)
print(self.symbols[1].var_name)
print(self.symbols[1].address)
size = self.get_size(type_of_var)
address = self.find_empty_in_bitmap(size)
var = Symbol(name, type_of_var, "var", self.function, self.scope + scope_update, size, size, address)
self.symbols.append(var)
self.make_full_bitmap(address, size)
def get_in_method(self, func_name):
self.function = func_name
def one_scope_in(self):
self.scope += 1
def one_scope_out(self):
for symbol in list(self.symbols):
if symbol.function == self.function and symbol.scope == self.scope:
self.clear_bitmap(symbol.address, symbol.size)
self.symbols.remove(symbol)
self.scope -= 1
if self.scope == 0:
self.function = "Global"
def get_all_var_size(self):
count = 0
objs = []
for var in self.symbols:
if var.function == self.function:
count += var.size
objs.append([var.address, var.size, var.type_size])
return count, objs
def get_var(self, var_name):
best_var_scope = -1
best_symbol_address = -1
var = {}
for symbol in self.symbols:
if symbol.var_name == var_name and symbol.function == self.function and symbol.scope > best_var_scope:
best_symbol_address = symbol.address
best_var_scope = symbol.scope
var = symbol
if best_symbol_address == -1:
for symbol in self.symbols:
if symbol.var_name == var_name and symbol.function == "Global":
var = symbol
best_symbol_address = symbol.address
if best_symbol_address == -1:
error_handler("Syntax Error", str(var_name) + " is not declared")
return var
def get_function(self):
return self.function
def get_var_address(self, var_name):
var = self.get_var(var_name)
return var.address
def get_var_type_size(self, var_name):
var = self.get_var(var_name)
return var.type_size
def get_var_size(self, var_name):
var = self.get_var(var_name)
return var.size
def get_var_type(self, var_name):
try:
int(var_name)
return "int"
except ValueError:
var = self.get_var(var_name)
return var.type_of_var
def is_array(self, name):
if self.get_var(name).type_of_data == "array":
return True
return False
def is_var_declared(self, var_name):
for symbol in self.symbols:
if symbol.var_name == var_name and symbol.function == self.function and self.scope == symbol.scope:
return True
return False