-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoaf_sem.py
499 lines (478 loc) · 11.5 KB
/
oaf_sem.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#import the quad to maque the quadric more generic
import oaf_state as state
import re
global_str = "global"
constant_str = "constant"
scope = global_str
var_table = {scope: {}, constant_str: {}}
# Stores the functions information
# [[return type], [signature], [param list], [starting quad, ending quad], function size, [memory map (optional)]]
func_table = {}
operation = None
semantic_cube = {
"=": {
"int": {
"int": "int",
"char": None,
"float": None,
"bool": None,
},
"char": {
"int": "char",
"char": "char",
"float": None,
"bool": None,
},
"float": {
"int": "float",
"char": None,
"float": "float",
"bool": None,
},
"bool": {
"int": None,
"char": None,
"float": None,
"bool": "bool",
}
},
"*": {
"int": {
"int": "int",
"char": None,
"float": "float",
"bool": None,
},
"char": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
"float": {
"int": "float",
"char": None,
"float": "float",
"bool": None,
},
"bool": {
"int": None,
"char": None,
"float": None,
"bool": None,
}
},
"+": {
"int": {
"int": "int",
"char": "char",
"float": "float",
"bool": None,
},
"char": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
"float": {
"int": "float",
"char": None,
"float": "float",
"bool": None,
},
"bool": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
},
"-": {
"int": {
"int": "int",
"char": "char",
"float": "float",
"bool": None,
},
"char": {
"int": None,
"char": "char",
"float": None,
"bool": None,
},
"float": {
"int": "float",
"char": None,
"float": "float",
"bool": None,
},
"bool": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
},
"/": {
"int": {
"int": "float",
"char": None,
"float": "float",
"bool": None,
},
"char": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
"float": {
"int": "float",
"char": None,
"float": "float",
"bool": None,
},
"bool": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
},
">": {
"int": {
"int": "bool",
"char": "bool",
"float": "bool",
"bool": None,
},
"char": {
"int": "bool",
"char": "bool",
"float": None,
"bool": None,
},
"float": {
"int": "bool",
"char": None,
"float": "bool",
"bool": None,
},
"bool": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
},
"<": {
"int": {
"int": "bool",
"char": "bool",
"float": "bool",
"bool": None,
},
"char": {
"int": "bool",
"char": "bool",
"float": None,
"bool": None,
},
"float": {
"int": "bool",
"char": None,
"float": "bool",
"bool": None,
},
"bool": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
},
">=": {
"int": {
"int": "bool",
"char": "bool",
"float": "bool",
"bool": None,
},
"char": {
"int": "bool",
"char": "bool",
"float": None,
"bool": None,
},
"float": {
"int": "bool",
"char": None,
"float": "bool",
"bool": None,
},
"bool": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
},
"<=": {
"int": {
"int": "bool",
"char": "bool",
"float": "bool",
"bool": None,
},
"char": {
"int": "bool",
"char": "bool",
"float": None,
"bool": None,
},
"float": {
"int": "bool",
"char": None,
"float": "bool",
"bool": None,
},
"bool": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
},
"<>": {
"int": {
"int": "bool",
"char": "bool",
"float": "bool",
"bool": None,
},
"char": {
"int": "bool",
"char": "bool",
"float": None,
"bool": None,
},
"float": {
"int": "bool",
"char": None,
"float": "bool",
"bool": None,
},
"bool": {
"int": None,
"char": None,
"float": None,
"bool": "bool",
},
},
"==": {
"int": {
"int": "bool",
"char": "bool",
"float": "bool",
"bool": None,
},
"char": {
"int": "bool",
"char": "bool",
"float": None,
"bool": None,
},
"float": {
"int": "bool",
"char": None,
"float": "bool",
"bool": None,
},
"bool": {
"int": None,
"char": None,
"float": None,
"bool": "bool",
},
},
"||": {
"int": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
"char": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
"float": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
"bool": {
"int": None,
"char": None,
"float": None,
"bool": "bool",
},
},
"&&": {
"int": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
"char": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
"float": {
"int": None,
"char": None,
"float": None,
"bool": None,
},
"bool": {
"int": None,
"char": None,
"float": None,
"bool": "bool",
}
}
}
def fill_symbol_table_function(symbol, attributes):
if(symbol == global_str or symbol == constant_str):
raise NameError("Using reserved word '{0}' as an identifier".format(symbol))
if(func_table.get(symbol) == None):
func_table[symbol] = attributes
else:
raise NameError("Function redeclaration, '{0}' already exists".format(symbol))
def fill_local_variables_table(var, type, attrs, m_list):
if(var == global_str or var == constant_str):
raise NameError("Using reserved word '{0}' as an identifier".format(var))
#verifica si existe el scope dado
if(var_table.get(scope) == None):
var_table[scope] = {}
if(var == scope or var_table[scope].get(var) != None):
raise NameError("Variable redeclaration, '{0}' already exists".format(var))
else:
if(m_list):
if(m_list[0] < 0): # Variable has temporal values
var_table[scope][var] = [type, -1, attrs, 'l', m_list]
else:
var_table[scope][var] = [type, state.local_dir, attrs, 'l', m_list]
else:
var_table[scope][var] = [type, state.local_dir, attrs, 'l']
state.local_dir += attrs[0]
def fill_global_variables_table(var, type, attrs, m_list):
if(var == global_str or var == constant_str):
raise NameError("Using reserved word '{0}' as an identifier".format(var))
# attrs = [size of variable,{each dimension size}]
# verifica si existe el scope dado
if(var_table.get(scope) == None):
var_table[scope] = {}
if(var == scope or var_table[scope].get(var) != None):
raise NameError("Variable redeclaration, '{0}' already exists".format(var))
else:
if(m_list):
var_table[scope][var] = [type, state.global_dir, attrs, 'g', m_list]
else:
var_table[scope][var] = [type, state.global_dir, attrs, 'g']
state.global_dir += attrs[0]
def fill_symbol_table_constant(symbol, type, size):
if(var_table[constant_str].get(str(symbol)) != None):
pass
else:
var_table[constant_str][str(symbol)] = [type, state.constant_dir, [size, 1], 'c']
if(type[0] == "i" or type[0] == "f"):
state.constant_dir += 4
else:
state.constant_dir += 1
def get_function(func_name):
function = func_table.get(func_name)
if(function != None):
return function
else:
raise NameError("Undeclared function '{0}'".format(func_name))
def get_scope():
return scope
def validate_redeclaration_function(validate_scope):
if(var_table.get(validate_scope) != None):
raise NameError("Function redeclaration '{0}'".format(validate_scope))
def is_declared(var):
if(var_table[constant_str].get(str(var)) != None):
return True
elif(var_table.get(scope) != None and var_table[scope].get(var) != None):
return True
elif(var_table[global_str].get(var) != None):
return True
elif(func_table.get(var) != None):
return True
elif(func_table.get(var) != None):
return True
else:
raise NameError("Undeclared variable '{0}'".format(var))
def get_variable(var):
# First looks in scope
if(var_table.get(scope) != None and var_table[scope].get(var) != None):
return [var, var_table[scope].get(var)]
# Then looks in constants
elif(var_table[constant_str].get(str(var)) != None):
return [var, var_table[constant_str].get(str(var))]
# Then looks in functions
elif(func_table.get(var) != None):
type = func_table[var][0][0] # Function return value information
if(type[0] == "i" or type[0] == "f"):
size = 4
else:
size = 1
return [var, [type, state.return_dir_stack[-1], [size, 1], 't']]
# At last looks in globals
else:
return [var, var_table[global_str].get(var)]
def get_type(op, op1, op2):
type = None
if(op == "color"):
type = "color"
elif(isinstance(op1, str)):
type = semantic_cube[op]["char"][op2[1][0]]
else:
if(op1[1] == None):
type1 = func_table[op1[0]][0]
else:
type1 = op1[1][0][:]
if(op2[1] == None):
type2 = func_table[op2[0]][0]
else:
type2 = op2[1][0][:]
try:
type = semantic_cube[op][type1][type2]
except(KeyError): # Types weren't found in dictionary
if(cmp(type1, type2) == 0): # Both variables have the same type and dimensions
type = type1
if(type != None):
return type
else:
raise NameError("Incompatible types '{0}' and '{1}'".format(type1, type2))
def is_char(char):
if(len(char) != 3):
print (len(char))
raise NameError("its not a char ")
def is_signature_valid(func_name, signature):
if(cmp(func_table.get(func_name)[1], signature) == 0):
return True
else:
raise NameError("Wrong signature '{0}'. '{1}' expected, '{2}' received".format(func_name, func_table.get(func_name)[1], signature))
#validate if the return function returns a value if its not void and if the type of return is equal to the var it returns
def validate_return_funtion(var):
if(scope != "main" and func_table[scope][0][0] != var):
raise NameError("Wrong return type in function '{0}'".format(scope))