-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompiler.py
316 lines (234 loc) · 7.49 KB
/
compiler.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
from typing import Union
# TODO : Interpreter
class Compiler:
def __init__(self, instruction_names: Union[dict, tuple], var_types:dict, other_instructions:list, stdscr, translations: dict, translate_method, tab_char:str= "\t"):
"""
Initializes a new compiler.
:param instruction_names: Dictionaries containing the translation of the instructions
Keys : ('for', 'if', 'while', 'switch', 'arr', 'case', 'default', 'fx', 'proc', 'const')
:param var_types: Dictionaries containing the translation of the variable names
Keys : ('int', 'float', 'string', 'bool', 'char')
"""
# Dictionaries containing the translation of the variable names and the translation of the instructions
self.instruction_names = instruction_names
self.var_types = var_types
self.other_instructions = other_instructions
# Compilation-related variables
self.instructions_list = [] # The list of instructions to be compiled
self.instructions_stack = [] # The stack of the instructions (indicates the number of tabs and the last instruction block's name)
# Use variables
self.stdscr = stdscr
self.errored = False
self.tab_char = tab_char
self.translations = translations
self.translate_method = translate_method
def compile(self, instructions_list:list):
"""
Dispatches the compilation to the correct functions based on the instruction params.
:param instructions_list: The list of instructions, a list of strings.
"""
# Resets the errored state
self.errored = False
# Calls the pre-compilation cleaning method
self.prepare_new_compilation()
# Keeps as an attribute the list of instructions
self.instructions_list = instructions_list
# Creates the instruction names
instruction_names = self.instruction_names
if isinstance(self.instruction_names, dict):
instruction_names = instruction_names.keys()
# Interprets each instruction one by one
for i, line in enumerate(self.instructions_list):
# Checks if no error occurred
if self.errored: break
line = line.split(' ')
instruction_name = line[0]
instruction_params = line[1:]
# Based on the instruction's name, dispatches to the correct functions
if instruction_name in (*instruction_names, *self.other_instructions):
# Turns the fx_name into a callback function : The analyze_%name% method of this class.
try: fx_name = getattr(self, f"analyze_{instruction_name}")
except Exception: raise NotImplementedError(f"Function {instruction_name} not implemented")
# Calls the callback function and gives it the instruction's name and params, along with the line number
fx_name(instruction_name, instruction_params, i)
# Defines a variable if wanted
elif instruction_name in self.var_types or (
instruction_name and instruction_name[-1] == "*" and instruction_name[:-1] in self.var_types
):
self.define_var(line, i)
# Reassigns a variable if wanted
elif len(instruction_params) != 0:
if instruction_params[0].endswith("="):
self.var_assignation(line, i)
# Makes the final trimming to the line
self.final_trim(instruction_name, i)
# Also checks if an error occurred
if self.errored:
return None
# Makes the final adjustments to each line and puts everything together
final_compiled_code = self.final_touches()
# Finally returns the compiled code
return final_compiled_code
def prepare_new_compilation(self):
"""
Gets called before compilation so the compiler can clean itself.
"""
pass
def analyze_const(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_for(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_end(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_while(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_if(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_else(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_elif(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_switch(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_case(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_default(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_print(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_input(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_fx(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_precond(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_data(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_datar(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_result(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_return(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_desc(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_fx_start(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a constant.
"""
pass
def analyze_vars(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes local variables.
"""
pass
def analyze_arr(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes local variables.
"""
pass
def analyze_CODE_RETOUR(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes the return code.
"""
pass
def analyze_struct(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a structure.
"""
pass
def analyze_init(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a structure initialization.
"""
pass
def analyze_delete(self, instruction_name:str, instruction_params:list, line_number:int):
"""
Analyzes a structure initialization.
"""
raise NotImplementedError
def final_trim(self, instruction_name:str, line_number:int):
"""
Makes the final trim to the line.
"""
pass
def final_touches(self):
"""
Makes the final touches to the line.
"""
pass
def define_var(self, instruction:list, line_number:int):
"""
Is called when a variable is defined.
"""
pass
def var_assignation(self, instruction:list, line_number:int):
"""
Is called when a variable is defined.
"""
pass
def error(self, message:str="Error."):
"""
Errors out to the user.
"""
self.stdscr.clear()
self.stdscr.addstr(0, 0, message)
self.stdscr.getch()
self.errored = True