-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeGenerator.py
More file actions
401 lines (303 loc) · 14 KB
/
codeGenerator.py
File metadata and controls
401 lines (303 loc) · 14 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
""" This is AST -> intermediate translation """
import sys
from parser import Command, parser, symbol_table # this import is important, even if symbol_table is not used in this program
class codeCommand:
def __init__(self, cc_type: str, block=None):
self.type = cc_type
self.args = []
self.block = block
class codeProgram:
def __init__(self):
self.code_commands = []
self.label = 1
def __str__(self):
table = ""
for command in self.code_commands:
table += command.type + " " + str(command.args) + "\n"
return table
def add_code_command(self, cc_type):
code_command = codeCommand(cc_type)
code_command.block = -1
self.code_commands.append(code_command)
def add_arg_to_current_command(self, arg):
self.code_commands[-1].args.append(arg)
def bad_inequality(self):
if self.code_commands[-1] == "CODE_JLT" or self.code_commands[-1] == "CODE_JGT":
return True
else:
return False
def switch_condition_at_current_command(self):
current_type = self.code_commands[-1].type
if current_type == "CODE_JNEQ":
self.code_commands[-1].type = "CODE_JEQ"
elif current_type == "CODE_JEQ":
self.code_commands[-1].type = "CODE_JNEQ"
elif current_type == "CODE_JGEQ":
self.code_commands[-1].type = "CODE_JLT"
elif current_type == "CODE_JLEQ":
self.code_commands[-1].type = "CODE_JGT"
elif current_type == "CODE_JGT":
self.code_commands[-1].type = "CODE_JLEQ"
elif current_type == "CODE_JLT":
self.code_commands[-1].type = "CODE_JGEQ"
def set_type_of_current_command(self, cc_type):
self.code_commands[-1].type = cc_type
def change_jump_destination_at_current_command(self, dest):
self.code_commands[-1].args[0] = dest
def add_label(self, label_value):
code_command = codeCommand("CODE_LABEL")
code_command.block = -1
code_command.args.append(label_value)
self.code_commands.append(code_command)
code_program = codeProgram()
def transform_tree_r(command: Command):
if not command: # ending recursion
return
if command.type == "COM_ASSGNOP":
code_program.add_code_command("CODE_COPY")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[1])
elif command.type == "COM_NUM":
code_program.add_arg_to_current_command(command.index)
# code_program.add_arg_to_current_command(None)
elif command.type == "COM_PID":
code_program.add_arg_to_current_command(command.index)
# code_program.add_arg_to_current_command(None)
elif command.type == "COM_ARR":
# code_program.add_code_command("CODE_ARR")
code_program.add_arg_to_current_command(command.commands[0].index)
code_program.add_arg_to_current_command(command.commands[1].index)
elif command.type == "COM_ADD":
code_program.add_code_command("CODE_ADD")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[1])
elif command.type == "COM_SUB":
code_program.add_code_command("CODE_SUB")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[1])
elif command.type == "COM_MUL":
code_program.add_code_command("CODE_MUL")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[1])
elif command.type == "COM_DIV":
code_program.add_code_command("CODE_DIV")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[1])
elif command.type == "COM_MOD":
code_program.add_code_command("CODE_MOD")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[1])
elif command.type == "COM_IF":
label_exit = code_program.label
code_program.label += 1
label_tmp = label_exit
code_program.add_code_command("CODE_UNKNOWN")
code_program.add_arg_to_current_command(label_tmp)
code_program.add_arg_to_current_command(-1)
transform_tree_r(command.commands[0])
if code_program.bad_inequality():
code_program.switch_condition_at_current_command()
label_exit = code_program.label
code_program.label += 1
code_program.add_code_command("CODE_JUMP")
code_program.add_arg_to_current_command(label_exit)
code_program.add_label(label_tmp)
transform_tree_r(command.commands[1]) # this is happening if true
code_program.add_label(label_exit)
elif command.type == "COM_IFELSE":
label_else = code_program.label
code_program.label += 1
label_exit = code_program.label
code_program.label += 1
label_tmp = label_exit
code_program.add_code_command("CODE_UNKNOWN")
code_program.add_arg_to_current_command(label_else)
code_program.add_arg_to_current_command(-1) # separation
transform_tree_r(command.commands[0])
if code_program.bad_inequality():
code_program.switch_condition_at_current_command()
label_else = code_program.label
code_program.label += 1
code_program.add_code_command("CODE_JUMP")
code_program.add_arg_to_current_command(label_else)
code_program.add_label(label_tmp)
transform_tree_r(command.commands[1]) # this is happening if true
code_program.add_code_command("CODE_JUMP")
code_program.add_arg_to_current_command(label_exit)
code_program.add_label(label_else)
transform_tree_r(command.commands[2])
code_program.add_label(label_exit)
elif command.type == "COM_WHILE":
label_start = code_program.label
code_program.label += 1
label_exit = code_program.label
code_program.label += 1
label_tmp = label_exit
code_program.add_label(label_start)
code_program.add_code_command("CODE_UNKNOWN")
code_program.add_arg_to_current_command(label_tmp)
code_program.add_arg_to_current_command(-1) # separation
transform_tree_r(command.commands[0])
if code_program.bad_inequality():
code_program.switch_condition_at_current_command()
label_exit = code_program.label
code_program.label += 1
code_program.add_code_command("CODE_JUMP")
code_program.add_arg_to_current_command(label_exit)
code_program.add_label(label_tmp)
transform_tree_r(command.commands[1])
code_program.add_code_command("CODE_JUMP")
code_program.add_arg_to_current_command(label_start)
code_program.add_label(label_exit)
elif command.type == "COM_REPEAT":
label_start = code_program.label
code_program.label += 1
code_program.add_label(label_start)
transform_tree_r(command.commands[0])
code_program.add_code_command("CODE_UNKNOWN")
code_program.add_arg_to_current_command(label_start)
code_program.add_arg_to_current_command(-1)
transform_tree_r(command.commands[1])
if code_program.bad_inequality():
code_program.switch_condition_at_current_command()
label_exit = code_program.label
code_program.label += 1
code_program.change_jump_destination_at_current_command(label_exit)
code_program.add_code_command("CODE_JUMP")
code_program.add_arg_to_current_command(label_start)
code_program.add_label(label_exit)
elif command.type == "COM_FOR":
label_start = code_program.label
code_program.label += 1
label_exit = code_program.label
code_program.label += 1
if command.commands[0].index == command.commands[2].index or command.commands[0].index == command.commands[
3].index:
print("Incorrect FOR loop.", file=sys.stderr)
sys.exit()
code_program.add_code_command("CODE_COPY")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[2])
code_program.add_code_command("CODE_COPY")
transform_tree_r(command.commands[1])
transform_tree_r(command.commands[3])
code_program.add_code_command("CODE_INC")
transform_tree_r(command.commands[1])
code_program.add_code_command("CODE_ITER_SUB")
transform_tree_r(command.commands[1])
# transform_tree_r(command.commands[1]) # is it necessary?
transform_tree_r(command.commands[2])
code_program.add_label(label_start)
code_program.add_code_command("CODE_JZERO")
code_program.add_arg_to_current_command(label_exit)
code_program.add_arg_to_current_command(-1)
transform_tree_r(command.commands[1])
transform_tree_r(command.commands[4])
code_program.add_code_command("CODE_INC")
transform_tree_r(command.commands[0])
code_program.add_code_command("CODE_DEC")
transform_tree_r(command.commands[1])
code_program.add_code_command("CODE_JUMP")
code_program.add_arg_to_current_command(label_start)
code_program.add_label(label_exit)
elif command.type == "COM_FORDOWN":
label_start = code_program.label
code_program.label += 1
label_exit = code_program.label
code_program.label += 1
if command.commands[0].index == command.commands[2].index or command.commands[0].index == command.commands[
3].index:
print("Incorrect FOR loop.", file=sys.stderr)
sys.exit()
code_program.add_code_command("CODE_COPY")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[2])
code_program.add_code_command("CODE_COPY")
transform_tree_r(command.commands[1])
transform_tree_r(command.commands[2])
code_program.add_code_command("CODE_INC")
transform_tree_r(command.commands[1])
code_program.add_code_command("CODE_ITER_SUB")
transform_tree_r(command.commands[1])
# transform_tree_r(command.commands[1])
transform_tree_r(command.commands[3])
code_program.add_label(label_start)
code_program.add_code_command("CODE_JZERO")
code_program.add_arg_to_current_command(label_exit)
code_program.add_arg_to_current_command(-1)
transform_tree_r(command.commands[1])
transform_tree_r(command.commands[4])
code_program.add_code_command("CODE_DEC")
transform_tree_r(command.commands[0])
code_program.add_code_command("CODE_DEC")
transform_tree_r(command.commands[1])
code_program.add_code_command("CODE_JUMP")
code_program.add_arg_to_current_command(label_start)
code_program.add_label(label_exit)
elif command.type == "COM_EQ":
code_program.set_type_of_current_command("CODE_JNEQ")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[1])
elif command.type == "COM_NEQ":
code_program.set_type_of_current_command("CODE_JEQ")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[1])
elif command.type == "COM_LT":
code_program.set_type_of_current_command("CODE_JGEQ")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[1])
elif command.type == "COM_GT":
code_program.set_type_of_current_command("CODE_JLEQ")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[1])
elif command.type == "COM_LEQ":
code_program.set_type_of_current_command("CODE_JGT")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[1])
elif command.type == "COM_GEQ":
code_program.set_type_of_current_command("CODE_JLT")
transform_tree_r(command.commands[0])
transform_tree_r(command.commands[1])
elif command.type == "COM_READ":
code_program.add_code_command("CODE_READ")
transform_tree_r(command.commands[0])
elif command.type == "COM_WRITE":
code_program.add_code_command("CODE_WRITE")
transform_tree_r(command.commands[0])
elif command.type == "COM_COMMANDS":
for c in command.commands:
transform_tree_r(c)
elif command.type == "COM_PROGRAM":
for c in command.commands:
transform_tree_r(c)
def transfer_tree_to_code(program: Command):
transform_tree_r(program)
return code_program
def catch_errors(code):
""" For now this function only catches changing iterator error inside both FOR loops. """
check = False
iterator_index = -1
label_count = 0
for code_command, next_command, last_command in zip(code.code_commands, code.code_commands[1:],
code.code_commands[2:]):
if code_command.type == "CODE_COPY" and next_command.type == "CODE_COPY" and last_command.type == "CODE_INC":
iterator_index = code_command.args[0]
check = True
continue
if check == True and label_count == 1 and last_command.type == "CODE_LABEL":
label_count = 0
check = False
if check == True and last_command.type == "CODE_LABEL":
label_count += 1
if code_command.type == "CODE_COPY" and check == True and code_command.args[0] == iterator_index:
print("Iterator modification inside loop", file=sys.stderr)
sys.exit()
""" Parsing and intermediate code generating. """
file = open(sys.argv[1], 'r') # reading from file, for debugging you can overwrite easy.
easy = file.read()
result = parser.parse(easy) # Abstract Syntax Tree
intermediate = transfer_tree_to_code(result) # now it is intermediate code
intermediate.code_commands.append(codeCommand(
"EOFCOMMANDS")) # in kompilator, when this is seen it means that everything is fine, and HALT should be put here.
catch_errors(intermediate) # this is check on intermediate code for some errors that can be caught now.
# print(intermediate)