-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1566 lines (1275 loc) · 47 KB
/
main.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Clase main en la cual se tiene toda la parte de gramatica y fase de compilacion
#importacion de todas las clases necesarias para
from ClassesTable import ClassesTable
from Program import Program
from ply.yacc import yacc
from VarsTable import VarsTable
from SemanticCube import SemanticCube
from Function import Function
from FunctionsTable import FunctionsTable
from QuadruplesList import QuadruplesList
from Parameter import Parameter
from Lexer import *
from Error import Error
from VirtualMemory import VirtualMemory
#inicializacion de dichos objetos para su uso
#creacion de pila de tablas de variables y funciones
semanticCube = SemanticCube()
classesTable = ClassesTable()
varsTablesPile = []
functionsTablesPile = []
quadrupleList = QuadruplesList()
#rangos de memoria global ademas de constantes
GI = [1000, 1999]
GF = [2000, 2999]
GC = [3000, 3999]
GB = [4000, 4999]
CI = [18000, 18999]
CF = [19000, 19999]
CC = [20000, 20999]
CB = [21000, 21999]
#contadores para saber cuanta memoria usa cada funcion y clase
global ci_counter
ci_counter = 0
cf_counter = 0
cc_counter = 0
cb_counter = 0
#creacion de tabla de constantes vacia
constantsTable = {}
LI = [10000, 10999]
LF = [11000, 11999]
LC = [12000, 12999]
LB = [13000, 13999]
#gramaticas con sus puntos neuralgicos
def p_main(p):
'''
main : CLASS MAIN np_generate_goto_main LEFTCURLYBRACE np_start_global_memory_counter GLOBAL VARS np_create_varsTable np_set_var_scope_global LEFTCURLYBRACE var_dec RIGHTCURLYBRACE np_destroy_varsTable np_stop_global_memory_counter CLASSES LEFTCURLYBRACE class_dec RIGHTCURLYBRACE FUNCTIONS np_create_functionsTable LEFTCURLYBRACE func_dec RIGHTCURLYBRACE np_destroy_functionsTable np_fill_goto_main_quad np_reset_temp_counter np_set_temp_global_flag block RIGHTCURLYBRACE np_create_program
'''
p[0] = ('rule main: ', p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15], p[16], p[17])
def p_class_dec(p):
'''
class_dec : CLASS ID np_get_class_name np_check_class_exists LEFTCURLYBRACE VARS np_create_varsTable np_set_var_scope_class LEFTCURLYBRACE var_dec RIGHTCURLYBRACE np_destroy_varsTable FUNCTIONS np_create_functionsTable LEFTCURLYBRACE func_dec RIGHTCURLYBRACE np_destroy_functionsTable RIGHTCURLYBRACE np_save_class class_dec2
| empty
'''
# p[0] = ('rule class_dec: ', p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13])
def p_class_dec2(p):
'''
class_dec2 : class_dec
| empty
'''
p[0] = ('rule class_dec2: ', p[1])
def p_param(p):
'''
param : s_type ID np_get_func_parameter np_add_parameter_to_list param2
'''
p[0] = ('rule param: ', p[1], p[2], p[3])
def p_param2(p):
'''
param2 : COMMA param
| empty
'''
if (len(p) == 3):
p[0] = ('rule param2: ', p[1], p[2])
else:
p[0] = ('rule param2: ', p[1])
def p_func_dec(p):
'''
func_dec : FUNC np_reset_temp_counter func_dec2 ID np_get_func_name np_start_local_memory_counter np_push_func_id_globals LEFTPARENTHESIS np_create_varsTable param RIGHTPARENTHESIS LEFTCURLYBRACE VARS np_set_var_scope_function LEFTCURLYBRACE var_dec RIGHTCURLYBRACE np_destroy_varsTable np_init_func_tempTable np_save_function block RETURN h_exp RIGHTCURLYBRACE np_generate_return_func np_pop_varsTable np_generate_endfunc_quad func_dec3
| empty
'''
# p[0] = ('rule func_dec: ', p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15],p[16])
def p_func_dec2(p):
'''
func_dec2 : s_type np_get_func_type
| VOID
'''
p[0] = ('rule func_dec2: ', p[1])
def p_func_dec3(p):
'''
func_dec3 : func_dec
| empty
'''
p[0] = ('rule func_dec3: ', p[1])
def p_var_dec(p):
'''
var_dec : VAR var_dec6 SEMICOLON np_save_var var_dec8
'''
p[0] = ('rule var_dec: ', p[1], p[2], p[3])
def p_var_dec2(p):
'''
var_dec2 : c_type var_dec4
'''
p[0] = ('rule var_dec2: ', p[1], p[2])
def p_var_dec3(p):
'''
var_dec3 : s_type var_dec4
'''
p[0] = ('rule var_dec3: ', p[1], p[2])
def p_var_dec4(p):
'''
var_dec4 : ID np_get_var_name COMMA np_save_var var_dec4
| ID np_get_var_name var_dec5
'''
if (len(p) == 4):
p[0] = ('rule var_dec4: ', p[1], p[2], p[3])
elif(len(p) == 3):
p[0] = ('rule var_dec4: ', p[1], p[2])
else:
p[0] = ('rule var_dec4: ', p[1])
def p_var_dec5(p):
'''
var_dec5 : LEFTBRACKET CTEI RIGHTBRACKET np_set_DIM_array var_dec9
| LEFTBRACKET CTEI RIGHTBRACKET LEFTBRACKET CTEI RIGHTBRACKET np_set_DIM_matrix var_dec9
| empty
'''
if (len(p) == 7):
p[0] = ('rule var_dec5: ', p[1], p[2], p[3], p[4], p[5], p[6], p[7])
elif (len(p) == 4):
p[0] = ('rule var_dec5: ', p[1], p[2], p[3], p[4])
else:
p[0] = ('rule var_dec5: ', p[1])
def p_var_dec6(p):
'''
var_dec6 : var_dec2 var_dec7
| var_dec3 var_dec7
'''
p[0] = ('rule var_dec6: ', p[1], p[2])
def p_var_dec7(p):
'''
var_dec7 : var_dec6
| empty
'''
p[0] = ('rule var_dec7: ', p[1])
def p_var_dec8(p):
'''
var_dec8 : var_dec
| empty
'''
p[0] = ('rule var_dec8: ', p[1])
def p_var_dec9(p):
'''
var_dec9 : COMMA np_save_var var_dec4
| empty
'''
if (len(p) == 3):
p[0] = ('rule param2: ', p[1], p[2])
else:
p[0] = ('rule param2: ', p[1])
def p_factor(p):
'''
factor : LEFTPARENTHESIS np_create_fake_void h_exp RIGHTPARENTHESIS np_eliminate_fake_void
| CTEI np_saveConstantI np_push_ctei
| CTEF np_saveConstantF np_push_ctef
| variable
| call
'''
if (len(p) == 4):
p[0] = ('rule factor: ', p[1], p[2], p[3])
elif(len(p) == 2):
p[0] = ('rule factor: ', p[1])
def p_t(p):
'''
t : factor np_solve_times_divide_operator t2
'''
p[0] = ('rule term: ', p[1], p[2])
def p_t2(p):
'''
t2 : MULTIPLICATION np_push_operator_times_divide t
| DIVISION np_push_operator_times_divide t
| empty
'''
if (len(p) == 3):
p[0] = ('rule term2: ', p[1], p[2])
else:
p[0] = ('rule term2: ', p[1])
def p_exp(p):
'''
exp : t np_solve_plus_minus_operator exp2
'''
p[0] = ('rule exp: ', p[1], p[2])
def p_exp2(p):
'''
exp2 : PLUS np_push_operator_plus_minus exp
| MINUS np_push_operator_plus_minus exp
| empty
'''
if (len(p) == 3):
p[0] = ('rule exp2: ', p[1], p[2])
else:
p[0] = ('rule exp2: ', p[1])
def p_s_exp(p):
'''
s_exp : exp s_exp2
'''
p[0] = ('rule s_exp2: ', p[1], p[2])
def p_s_exp2(p):
'''
s_exp2 : LT np_push_operator_sexp np_define_LOperand_sexp exp np_define_ROperand_sexp
| GT np_push_operator_sexp np_define_LOperand_sexp exp np_define_ROperand_sexp
| LTOE np_push_operator_sexp np_define_LOperand_sexp exp np_define_ROperand_sexp
| GTOE np_push_operator_sexp np_define_LOperand_sexp exp np_define_ROperand_sexp
| NE np_push_operator_sexp np_define_LOperand_sexp exp np_define_ROperand_sexp
| EQUALITY np_push_operator_sexp np_define_LOperand_sexp exp np_define_ROperand_sexp
| empty
'''
if (len(p) == 3):
p[0] = ('rule s_exp2: ', p[1], p[2])
else:
p[0] = ('rule s_exp2: ', p[1])
def p_h_exp(p):
'''
h_exp : s_exp h_exp2
'''
p[0] = ('rule h_exp: ', p[1],p[2])
def p_h_exp2(p):
'''
h_exp2 : AND np_push_operator_hexp np_define_LOperand_hexp s_exp np_define_ROperand_hexp h_exp2
| OR np_push_operator_hexp np_define_LOperand_hexp s_exp np_define_ROperand_hexp h_exp2
| empty
'''
if(len(p) == 4):
p[0] = ('rule h_exp2: ', p[1],p[2],p[3])
else:
p[0] = ('rule h_exp2: ', p[1])
def p_variable(p):
'''
variable : ID np_push_id_type variable2
'''
p[0] = ('rule variable: ', p[1], p[2])
def p_variable2(p):
'''
variable2 : LEFTBRACKET exp np_create_dimensional_quads RIGHTBRACKET variable3 np_sum_baseA_array
| empty
'''
if (len(p) == 5):
p[0] = ('rule variable2: ', p[1], p[2], p[3],p[4])
elif(len(p) == 2):
p[0] = ('rule variable2: ', p[1])
def p_variable3(p):
'''
variable3 : LEFTBRACKET np_DIM_plus exp np_create_dimensional_quads RIGHTBRACKET
| empty
'''
if (len(p) == 4):
p[0] = ('rule variable2: ', p[1], p[2], p[3])
elif (len(p) == 2):
p[0] = ('rule variable2: ', p[1])
def p_call_obj(p):
'''
call_obj : ID PERIOD ID LEFTPARENTHESIS call_obj2 RIGHTPARENTHESIS
'''
p[0] = ('rule call_obj: ', p[1], p[2], p[3],p[4],p[5],p[6])
def p_call_obj2(p):
'''
call_obj2 : call_obj3
| empty
'''
p[0] = ('rule call_obj2: ', p[1])
def p_call_obj3(p):
'''
call_obj3 : h_exp call_obj4
| empty
'''
if (len(p) == 3):
p[0] = ('rule call_obj3: ', p[1], p[2])
else:
p[0] = ('rule call_obj3: ', p[1])
def p_call_obj4(p):
'''
call_obj4 : COMMA h_exp call_obj4
| empty
'''
if (len(p) == 4):
p[0] = ('rule call_obj4: ', p[1], p[2], p[3])
else:
p[0] = ('rule call_obj4: ', p[1])
def p_call(p):
'''
call : ID np_check_func_exists LEFTPARENTHESIS np_generate_ERA_quad_func_call call2 RIGHTPARENTHESIS np_check_func_params np_generate_goSub_function_call np_assign_global_to_temporal_func_call
'''
p[0] = ('rule call: ', p[1], p[2], p[3],p[4])
def p_call2(p):
'''
call2 : exp np_generate_quad_parameter np_check_func_params_counter call3
| empty
'''
if (len(p) == 3):
p[0] = ('rule call2:', p[1], p[2])
else:
p[0] = ('rule call2:', p[1])
def p_call3(p):
'''
call3 : COMMA exp np_generate_quad_parameter np_check_func_params_counter call3
| empty
'''
if (len(p) == 4):
p[0] = ('rule call3:', p[1], p[2], p[3])
else:
p[0] = ('rule call3:', p[1])
def p_c_type(p):
'''
c_type : CL COLON ID
'''
p[0] = ('rule c_type: ', p[1], p[2], p[3])
def p_s_type(p):
'''
s_type : INT np_get_var_type
| FLOAT np_get_var_type
| CHAR np_get_var_type
| BOOL np_get_var_type
'''
p[0] = ('rule s_type: ', p[1])
def p_assignment(p):
'''
assignment : variable EQUAL np_push_assignation_operator assignment2
'''
p[0] = ('rule assignment: ', p[1],p[2])
def p_assignment2(p):
'''
assignment2 : exp np_result_assignation
| NEW ID
'''
if(len(p) == 2):
p[0] = ('rule assignment2 : ', p[1])
else:
p[0] = ('rule assignment2 : ', p[1],p[2])
def p_read(p):
'''
read : READ LEFTPARENTHESIS ID np_generate_read_quadruple RIGHTPARENTHESIS
'''
p[0] = ('rule read : ', p[1],p[2])
def p_write(p):
'''
write : WRITE LEFTPARENTHESIS write2 RIGHTPARENTHESIS
'''
p[0] = ('rule write :',p[1],p[2],p[3],p[4])
def p_write2(p):
'''
write2 : h_exp np_generate_write_quadruple write3
| SIGNBOARD np_push_signboard np_generate_write_quadruple write3
'''
p[0] = ('rule write2 :',p[1],p[2])
def p_write3(p):
'''
write3 : COMMA h_exp write3 np_generate_write_quadruple
| COMMA SIGNBOARD np_push_signboard write3 np_generate_write_quadruple
| empty
'''
if(len(p) == 3):
p[0] = ('rule write3 :',p[1],p[2])
else:
p[0] = ('rule write3 :',p[1])
def p_condition(p):
'''
condition : IF LEFTPARENTHESIS h_exp RIGHTPARENTHESIS np_generate_gotoF_condition block condition2 SEMICOLON np_fill_gotoF_condition_if
'''
p[0] = ('rule condition: ', p[1],p[2],p[3],p[4],p[5],p[6],p[7])
def p_condition2(p):
'''
condition2 : ELSE np_generate_goto_condition block
| empty
'''
if(len(p) == 3):
p[0] = ('rule condition2 : ', p[1],p[2])
else:
p[0] = ('rule condition2 : ', p[1])
def p_loop_w(p):
'''
loop_w : WHILE LEFTPARENTHESIS np_while_push_jumpStack h_exp np_while_generate_gotoF RIGHTPARENTHESIS DO block np_while_generate_goto SEMICOLON
'''
p[0] = ('rule loopW : ', p[1],p[2],p[3],p[4],p[5],p[6],p[7])
def p_loop_f(p):
'''
loop_f : FOR LEFTPARENTHESIS ID np_for_push_id EQUAL exp np_for_FIRSTexp TO exp np_for_SECONDexp RIGHTPARENTHESIS DO block SEMICOLON np_for_changesVC
'''
p[0] = ('rule loopF : ', p[1],p[2],p[3],p[4],p[5],p[6],p[7],p[8],p[9],p[10])
def p_statement(p):
'''
statement : assignment
| call
| call_obj
| read
| write
| condition
| loop_w
| loop_f
'''
p[0] = ('rule statement : ', p[1])
def p_block(p):
'''
block : LEFTCURLYBRACE statement block2 RIGHTCURLYBRACE
'''
p[0] = ('rule block : ', p[1],p[2],p[3],p[4])
def p_block2(p):
'''
block2 : statement block2
| empty
'''
if(len(p) == 3):
p[0] = ('rule block2 : ', p[1],p[2])
else:
p[0] = ('rule block2 : ', p[1])
def p_empty(p):
'empty :'
pass
def p_error(p):
print(f'Syntax error at {p.value!r} on line {p.lineno} of type {p}')
exit()
##### NEURALGIC POINTS ######
#funcion para añadir constantes a la tabla de constantes
def addConstantsTable(constantAdd):
global ci_counter
if constantAdd not in constantsTable:
constantsTable[constantAdd] = CI[0] + ci_counter
ci_counter += 1
else:
pass
#generar cuadruplo para ir a main, se genera vacio
def p_np_generate_goto_main(p):
'''
np_generate_goto_main : empty
'''
quadrupleList.generateGoToMainQuad()
#crear programa haciendo pop de la pila de las tablas
def p_np_create_program(p):
'''
np_create_program : empty
'''
global program
program = Program(classesTable, varsTablesPile.pop(-1), functionsTablesPile.pop(-1))
#obtener el nombre de la clase
def p_np_get_class_name(p):
'''
np_get_class_name : empty
'''
global current_class_name
current_class_name = p[-1]
#guardar clase en tabla de clases
def p_np_save_class(p):
'''
np_save_class : empty
'''
classesTable.add(current_class_name, functionsTablesPile.pop(-1), varsTablesPile.pop(-1))
#creación de la tabla de funciones
def p_np_create_functionsTable(p):
'''
np_create_functionsTable : empty
'''
global current_functionsTable
current_functionsTable = FunctionsTable()
#se saca la actual tabla de funciones y se mete en la pila de tablas de funciones
def p_np_destroy_functionsTable(p):
'''
np_destroy_functionsTable : empty
'''
functionsTablesPile.append(current_functionsTable)
#se crea una nueva tabla de variables
def p_np_create_varsTable(p):
'''
np_create_varsTable : empty
'''
global current_varsTable
current_varsTable = VarsTable()
#se mete la actual tabla de variables en la pila de tablas de variables
def p_np_destroy_varsTable(p):
'''
np_destroy_varsTable : empty
'''
global current_varsTable
varsTablesPile.append(current_varsTable)
current_varsTable=VarsTable()
#obtener el tipo de variable
def p_np_get_var_type(p):
'''
np_get_var_type : empty
'''
global current_var_type
current_var_type = p[-1]
#si es arreglo llega aqui y marca que al menos es un arrego con un bool
def p_np_set_DIM_array(p):
'''
np_set_DIM_array : empty
'''
global isArray
isArray=True
global current_dimension_size
current_dimension_size = p[-2]
# si llega a ser una matriz se marca con un booleano y se sacan ambas dimensiones de los indices ctei
def p_np_set_DIM_matrix(p):
'''
np_set_DIM_matrix : empty
'''
global isMatrix
global sizesMatrix
sizesMatrix=[]
isMatrix = True
#size1 size2 sizetotal d2
global current_dimension_size
current_dimension_size = p[-5] * p[-2]
sizesMatrix=[p[-5],p[-2]]
#obtener el nombre de variable
def p_np_get_var_name(p):
'''
np_get_var_name : empty
'''
global isArray
isArray = False
global isMatrix
isMatrix = False
global current_var_name
current_var_name = p[-1]
# si esta en variables globales el scope se vuelve global
def p_np_set_var_scope_global(p):
'''
np_set_var_scope_global : empty
'''
global current_var_scope
current_var_scope = 'global'
#el scope se vuelve de tipo clase si esta en una clase la variable
def p_np_set_var_scope_class(p):
'''
np_set_var_scope_class : empty
'''
global current_var_scope
current_var_scope = 'class'
#el scope se vuelve de tipo funcion si esta dentro de una funcion
def p_np_set_var_scope_function(p):
'''
np_set_var_scope_function : empty
'''
global current_var_scope
current_var_scope = 'function'
# se guarda la variable en la tabla de variables actual
def p_np_save_var(p):
'''
np_save_var : empty
'''
global DIM
global varAddDimensionalArray
global sizesMatrix
DIM=[]
#checar si es un arreglo o una matriz para darle valores a DIM para la tabla de variables
if not isArray and not isMatrix:
DIM = None
varAddDimensional=0
else:
if isArray: #si es un arreglo darle valor a su campo DIM
DIM.append(current_dimension_size)
varAddDimensional= current_dimension_size-1
if isMatrix: #si es una matriz darle valor a su campo DIM
for i in sizesMatrix:
DIM.append(i)
varAddDimensional= (DIM[0]*DIM[1])-1
if global_memory_counter_flag: #si es global con esta bandera se verifica
#variables para los contadores de memoria global
global current_var_type
global global_memory_counter_int
global global_memory_counter_float
global global_memory_counter_char
global global_memory_counter_bool
global global_memory_counter_array
global global_memory_counter_matrix
#dependiendo del tipo de variable se le suma la dirección base y se aumenta el contador global de ese tipo de variable
if current_var_type == "int":
current_varsTable.add(current_var_name, current_var_type, current_var_scope, GI[0] + global_memory_counter_int,DIM)
global_memory_counter_int += 1 + varAddDimensional
elif current_var_type == "float":
current_varsTable.add(current_var_name, current_var_type, current_var_scope, GF[0] + global_memory_counter_float,DIM)
global_memory_counter_float += 1 + varAddDimensional
elif current_var_type == "char":
current_varsTable.add(current_var_name, current_var_type, current_var_scope, GC[0] + global_memory_counter_char,DIM)
global_memory_counter_char += 1 + varAddDimensional
elif current_var_type == "bool":
current_varsTable.add(current_var_name, current_var_type, current_var_scope, GB[0] + global_memory_counter_bool,DIM)
global_memory_counter_bool += 1 + varAddDimensional
else: #de lo contrario se hace lo mismo pero con contadores y direcciones base locales
#variables para los contadores de memoria local
global local_memory_counter_int
global local_memory_counter_float
global local_memory_counter_char
global local_memory_counter_bool
#dependiendo del tipo de variable se le suma la dirección base y se aumenta el contador local de ese tipo de variable
if current_var_type == "int":
current_varsTable.add(current_var_name, current_var_type, current_var_scope, LI[0] + local_memory_counter_int,DIM)
local_memory_counter_int += 1
elif current_var_type == "float":
current_varsTable.add(current_var_name, current_var_type, current_var_scope, LF[0] + local_memory_counter_float,DIM)
local_memory_counter_float += 1
elif current_var_type == "char":
current_varsTable.add(current_var_name, current_var_type, current_var_scope, LC[0] + local_memory_counter_char,DIM)
local_memory_counter_char += 1
elif current_var_type == "bool":
current_varsTable.add(current_var_name, current_var_type, current_var_scope, LB[0] + local_memory_counter_bool,DIM)
local_memory_counter_bool += 1
current_var_type = current_var_type.translate(str.maketrans('','',' 1234567890[]'))
#obtener el nombre de la funcion
def p_np_get_func_name(p):
'''
np_get_func_name : empty
'''
global current_func_name
global initialQuadruple
initialQuadruple = quadrupleList.cont
current_func_name = str(p[-1])
#obtener el tipo de la funcion
def p_np_get_func_type(p):
'''
np_get_func_type : empty
'''
global current_func_type
current_func_type = str(p[-1][1])
#el id de la funcion se pushea a la tabla de variables globales ya que todas tienen return asi que se mete en la tabla de variables global con su tipo
def p_np_push_func_id_globals(p):
'''
np_push_func_id_globals : empty
'''
global current_var_type
global global_memory_counter_int
global global_memory_counter_float
global global_memory_counter_char
global global_memory_counter_bool
global global_memory_counter_array
global global_memory_counter_matrix
if current_var_type == "int":
varsTablesPile[0].add(current_func_name, current_func_type, "globalFunction", GI[0] + global_memory_counter_int,None)
global_memory_counter_int += 1
elif current_var_type == "float":
varsTablesPile[0].add(current_func_name, current_func_type, "globalFunction", GF[0] + global_memory_counter_float,None)
global_memory_counter_float += 1
elif current_var_type == "char":
varsTablesPile[0].add(current_func_name, current_func_type, "globalFunction", GC[0] + global_memory_counter_char,None)
global_memory_counter_char += 1
elif current_var_type == "bool":
varsTablesPile[0].add(current_func_name, current_func_type, "globalFunction", GB[0] + global_memory_counter_bool,None)
global_memory_counter_bool += 1
#se obtienen los campos de los parametros de una funcion para convertrlo enun objeto parameter
def p_np_get_func_parameter(p):
'''
np_get_func_parameter : empty
'''
global current_parameter
current_parameter=Parameter(str(p[-2][1]),str(p[-1]))
#añadir parametros a la tabla de variables para poder usarlos
def p_np_add_parameter_to_list(p):
'''
np_add_parameter_to_list : empty
'''
global current_parameters_list
global local_memory_counter_int
global local_memory_counter_float
global local_memory_counter_char
global local_memory_counter_bool
temp = "current_parameters_list" in globals()
if (temp):
current_parameters_list.append(current_parameter)
if current_parameter.type == "int":
current_varsTable.add(current_parameter.id,current_parameter.type, current_var_scope, LI[0] + local_memory_counter_int,None)
local_memory_counter_int += 1
elif current_parameter.type == "float":
current_varsTable.add(current_parameter.id,current_parameter.type, current_var_scope, LF[0] + local_memory_counter_float,None)
local_memory_counter_float += 1
elif current_parameter.type == "char":
current_varsTable.add(current_parameter.id,current_parameter.type, current_var_scope, LC[0] + local_memory_counter_char,None)
local_memory_counter_char += 1
elif current_parameter.type == "bool":
current_varsTable.add(current_parameter.id,current_parameter.type, current_var_scope, LB[0] + local_memory_counter_bool,None)
local_memory_counter_bool += 1
else:
current_parameters_list = []
current_parameters_list.append(current_parameter)
if current_parameter.type == "int":
current_varsTable.add(current_parameter.id,current_parameter.type, current_var_scope, LI[0] + local_memory_counter_int,None)
local_memory_counter_int += 1
elif current_parameter.type == "float":
current_varsTable.add(current_parameter.id,current_parameter.type, current_var_scope, LF[0] + local_memory_counter_float,None)
local_memory_counter_float += 1
elif current_parameter.type == "char":
current_varsTable.add(current_parameter.id,current_parameter.type, current_var_scope, LC[0] + local_memory_counter_char,None)
local_memory_counter_char += 1
elif current_parameter.type == "bool":
current_varsTable.add(current_parameter.id,current_parameter.type, current_var_scope, LB[0] + local_memory_counter_bool,None)
local_memory_counter_bool += 1
#guardar funcion en tabla de funciones
def p_np_save_function(p):
'''
np_save_function : empty
'''
current_functionsTable.add(current_func_name,current_func_type, current_parameters_list,varsTablesPile[-1],initialQuadruple, [0,0,0,0])
del globals()["current_parameters_list"]
##############################Quadruples###############################
#############################aritmetic_exp#############################
#pushear id con la condicion de si existe o no, y si tiene dimensiones se pushea a pila de dimensionadas junto con su tipo
def p_np_push_id_type(p):
'''
np_push_id_type : empty
'''
global idPush
test = False
idPush = p[-1]
## Si este esta antes de siguiente da prioridad a variables
for vt in reversed(varsTablesPile):
if idPush in vt.table:
if vt.table[idPush].dim != None:
global DIMid
DIMid=1
quadrupleList.dimensionalOperandsStack.append(idPush)
quadrupleList.typesStack.append(vt.table[idPush].type)
quadrupleList.operatorsStack.append('(')
elif vt.table[idPush].dim == None:
quadrupleList.operandsStack.append(vt.table[idPush].address)
quadrupleList.typesStack.append(vt.table[idPush].type)
return
print(f"Variable {idPush} not declared")
exit()
#pushear a pilaOper ctei
def p_np_push_ctei(p):
'''
np_push_ctei : empty
'''
global cteiPush
cteiPush = p[-2]
if cteiPush in constantsTable:
quadrupleList.operandsStack.append(constantsTable[cteiPush])
quadrupleList.typesStack.append("int")
#pushear a pilaOper cteF
def p_np_push_ctef(p):
'''
np_push_ctef : empty
'''
global ctefPush
ctefPush = p[-2]
if ctefPush in constantsTable:
quadrupleList.operandsStack.append(constantsTable[ctefPush])
quadrupleList.typesStack.append("float")
#pushear operador / a pilaOperadores
def p_np_push_operator_times_divide(p):
'''
np_push_operator_times_divide : empty
'''
global operPush
operPush = p[-1]
quadrupleList.operatorsStack.append(operPush)
#pushear operador +´o -a pilaOperadores
def p_np_push_operator_plus_minus(p):
'''
np_push_operator_plus_minus : empty
'''
global operPush
operPush = p[-1]
quadrupleList.operatorsStack.append(operPush)
#resolver si hay un + o - en top de pilaOperadores
def p_np_solve_plus_minus_operator(p):
'''
np_solve_plus_minus_operator : empty
'''
temporalType = quadrupleList.checkOperatorPlusMinus()
registerTempVariable(temporalType)
#resolver si hay un * o / en top de pilaOperadores
def p_np_solve_times_divide_operator(p):
'''
np_solve_times_divide_operator : empty
'''
temporalType = quadrupleList.checkOperatorTimesDivide()
registerTempVariable(temporalType)
#############################aritmetic_exp#############################
#############################assignation fakevoid bool_operators#############################
#se pushea = a pila de operadores
def p_np_push_assignation_operator(p):
'''
np_push_assignation_operator : empty
'''
global operPush
operPush = p[-1]
quadrupleList.operatorsStack.append(operPush)
#resolver la asignacion, sacar de pilaOperadores con su tipo para hacer comparacion de tipos
def p_np_result_assignation(p):
'''
np_result_assignation : empty
'''
temporalType = quadrupleList.makeAssignationResult()
registerTempVariable(temporalType)
#pushear ( a pilaOperadores que es el fondo falso
def p_np_create_fake_void(p):
'''
np_create_fake_void : empty
'''
global fakeVoid
fakeVoid= p[-1]
quadrupleList.operatorsStack.append(fakeVoid)
#eliminar fondo falso con pop
def p_np_eliminate_fake_void(p):
'''
np_eliminate_fake_void : empty
'''
quadrupleList.eliminateFakeVoid()
#pushear operadores de s_exp
def p_np_push_operator_sexp(p):
'''
np_push_operator_sexp : empty
'''
global operPush
operPush = p[-1]
quadrupleList.operatorsStack.append(operPush)
#pushear AND o OR a pilaOperadores
def p_np_push_operator_hexp(p):
'''
np_push_operator_hexp : empty
'''
global operPush
operPush = p[-1]
quadrupleList.operatorsStack.append(operPush)
#definir operando izquierdo de s_exp es decir Loperand < ROperand como ejemplo
def p_np_define_LOperand_sexp(p):
'''
np_define_LOperand_sexp : empty
'''
global LOperandSexp
LOperandSexp = quadrupleList.operandsStack.pop()
#definir operando derecho de s_exp es decir Loperand < ROperand como ejemplo
def p_np_define_ROperand_sexp(p):
'''
np_define_ROperand_sexp : empty
'''
temporalType = quadrupleList.generate_sExp_quad(LOperandSexp)
registerTempVariable(temporalType)
#definir operando izquierdo de h_exp es decir Loperand && ROperand como ejemplo