-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_generator.py
executable file
·321 lines (248 loc) · 8.26 KB
/
code_generator.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from generator_sup import *
from generator_sup.PB_helper import PB
from generator_sup.memory_helper import Memory
from generator_sup.special_symbols import SpecialSymbol
from generator_sup.symbol_table_helper import SymbolTable
program_block = PB()
memory = Memory()
symbol_table = SymbolTable()
SS = []
scope_stack = []
break_stack = []
return_stack = []
def ss_top():
return len(SS) - 1
def code_gen(token, action):
# print(action)
return {
"numeric_label": numeric_label,
"break_start": start_break,
"break_end": end_break,
"until": until,
"label": label,
"break_func": break_func,
"pid": pid,
"pnum": pnum,
"start_scope": start_scope,
"end_scope": end_scope,
"push_lexeme": push_lexeme,
"set_var": set_var,
"set_arr": set_arr,
"save_func_add": save_func_add,
"stop_symbol": stop_symbol,
"get_temp": get_temp,
"start_return": start_return,
"end_return": end_return,
"return_address": return_address,
"save_func_atts": save_func_atts,
"func_backpatching": func_backpatching,
"pop": pop,
"save": save,
"jpf": jpf,
"jpf_save": jpf_save,
"jp": jp,
"return_func": return_func,
"assign": assign,
"operation": operation,
"mult": mult,
"output": output,
"call_function": call_function,
"access_array_index": assign_array_index,
"assign_array_index": assign_array_index,
}.get(action)(token)
def pid(identifier):
if identifier == 'output':
SS.append('output')
else:
SS.append(symbol_table.get_address(identifier))
def pnum(num):
SS.append('#' + num)
def start_scope(*args):
scope_stack.append(symbol_table.size)
def end_scope(*args):
# Used to remove all scope-related variables from scope stack
to_keep = scope_stack.pop()
symbol_table.collapse_symbol_table(to_keep)
def push_lexeme(lexeme):
SS.append(lexeme)
def set_var(*args):
lexeme = SS.pop()
program_block.initialize_var(memory, symbol_table, lexeme)
def set_arr(*args):
length = int(SS.pop()[1:])
lexeme = SS.pop()
program_block.initialize_array(memory, symbol_table, lexeme, length)
def save_func_add(*args):
i = program_block.i
program_block.forward()
func_name = SS.pop()
SS.append(i)
SS.append(func_name)
def stop_symbol(*args):
# Add a STOP flag to the symbol table for future use
symbol_table.add_to_table(SpecialSymbol.SYMBOL_TABLE_STOP)
def numeric_label(*args):
SS.append('#' + str(program_block.i))
def label(*args):
SS.append(program_block.i)
def until(*args):
condition = SS.pop()
l = SS.pop()
program_block.add_instruction(program_block.i, 'JPF', condition, l, '')
program_block.forward()
def start_break(*args):
break_stack.append(SpecialSymbol.BREAK_CHECKPOINT)
def break_func(*args):
break_stack.append(program_block.i)
program_block.forward()
def end_break(*args):
for j in break_stack[::-1]:
if j is SpecialSymbol.BREAK_CHECKPOINT:
break_stack.pop()
break
break_stack.pop()
program_block.add_instruction(j, 'JP', str(program_block.i), '', '')
def get_temp(*args):
t = memory.get_temp_address()
SS.append(t)
program_block.add_instruction(program_block.i, 'ASSIGN', '#0', str(t))
program_block.forward()
# todo: ask for zero in PB
def start_return(*args):
return_stack.append((SpecialSymbol.RETURN_STACK_START, "#0"))
def end_return(*args):
top = return_stack.pop()
while top[0] != SpecialSymbol.RETURN_STACK_START:
program_block.add_instruction(top[0], 'ASSIGN', top[1], SS[ss_top()])
program_block.add_instruction(top[0] + 1, 'JP', str(program_block.i))
top = return_stack.pop()
def return_address(*args):
func_name = SS[ss_top() - 3]
if func_name.lower() != 'main':
value = SS[ss_top() - 1]
program_block.add_instruction(program_block.i, 'JP', f'@{value}')
program_block.forward()
def save_func_atts(*args):
atts = []
symbol_table_top = symbol_table.pop_from_table()
while symbol_table_top != SpecialSymbol.SYMBOL_TABLE_STOP:
atts.append(symbol_table_top[2])
symbol_table_top = symbol_table.pop_from_table()
attribute_one = SS.pop()
attribute_two = SS.pop()
tmp_add = SS.pop()
func_name = SS.pop()
atts.append(tmp_add)
atts.reverse()
atts.append(attribute_two)
atts.append(attribute_one)
symbol_table.add_to_table((func_name, 'function', atts))
def func_backpatching(*args):
end_of_func = SS.pop()
func_name = symbol_table.table[len(symbol_table.table) - 1][0]
if func_name.lower() != 'main':
program_block.add_instruction(end_of_func, 'JP', str(program_block.i))
else:
t = memory.get_temp_address()
program_block.add_instruction(end_of_func, 'ADD', '#0', '#0', t)
# program_block.add_instruction(end_of_func, 'JP', str(int(end_of_func) + 1))
program_block.forward()
def pop(*args):
SS.pop()
def save(*args):
SS.append(program_block.i)
program_block.forward()
def jpf(*args):
i = program_block.i
top_if = SS.pop()
after_if = SS.pop()
program_block.add_instruction(int(top_if), 'JPF', after_if, str(i))
def jpf_save(*args):
i = program_block.i
top_if = SS.pop()
after_else = SS.pop()
program_block.add_instruction(int(top_if), 'JPF', after_else, str(i + 1))
SS.append(i)
program_block.forward()
def jp(*args):
after_else = SS.pop()
program_block.add_instruction(int(after_else), 'JP', str(program_block.i))
def return_func(*args):
value = SS.pop()
return_stack.append((program_block.i, value))
program_block.forward()
program_block.forward()
def assign(*args):
value = SS.pop()
assignee = SS[ss_top()]
program_block.add_instruction(program_block.i, 'ASSIGN', value, assignee)
program_block.forward()
def operation(*args):
t = memory.get_temp_address()
i = program_block.i
x2 = SS.pop()
op = SS.pop()
x1 = SS.pop()
if op == '+':
program_block.add_instruction(i, 'ADD', x1, x2, t)
elif op == '-':
program_block.add_instruction(i, 'SUB', x1, x2, t)
elif op == '==':
program_block.add_instruction(i, 'EQ', x1, x2, t)
elif op == '<':
program_block.add_instruction(i, 'LT', x1, x2, t)
SS.append(t)
program_block.forward()
def mult(*args):
t = memory.get_temp_address()
i = program_block.i
x1 = SS.pop()
x2 = SS.pop()
program_block.add_instruction(i, 'MULT', x1, x2, t)
program_block.forward()
SS.append(t)
def output(*args):
name = SS[ss_top() - 1]
if name == 'output':
top = SS.pop()
program_block.add_instruction(program_block.i, 'PRINT', top)
program_block.forward()
def call_function(*args):
if SS[ss_top()] == 'output':
return
atts = []
counter = ss_top()
for i in range(counter, -1, -1):
if isinstance(SS[i], list):
atts = SS[i]
args_length = len(atts) - 3
# args
for i in range(args_length):
program_block.add_instruction(program_block.i, 'ASSIGN', SS[len(SS) - args_length + i], atts[i + 1])
program_block.forward()
# return add
program_block.add_instruction(program_block.i, 'ASSIGN', f'#{program_block.i + 2}', atts[args_length + 1])
program_block.forward()
# jp to func
program_block.add_instruction(program_block.i, 'JP', atts[0] + 1)
program_block.forward()
# pop all from SS
for _ in range(args_length + 1):
SS.pop()
# function output
t = memory.get_temp_address()
program_block.add_instruction(program_block.i, 'ASSIGN', atts[args_length + 2], t)
SS.append(t)
program_block.forward()
def assign_array_index(*args):
index = SS.pop()
address = SS.pop()
t1 = str(memory.get_temp_address())
program_block.add_instruction(program_block.i, 'MULT', '#4', index, t1)
program_block.forward()
t2 = str(memory.get_temp_address())
program_block.add_instruction(program_block.i, 'ASSIGN', address, t2)
program_block.forward()
program_block.add_instruction(program_block.i, 'ADD', t2, t1, t1)
program_block.forward()
SS.append('@' + t1)