-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconocedor.y
1028 lines (782 loc) · 31.9 KB
/
reconocedor.y
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
%{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define N 5000 //elementos en las tablas hash
int amb = 0; //variable de ambiente 1
int amb2 = 0; //variable de ambiente 2
int retorno = 0; //variable global de ambiente
// Estructura de los simbolos
typedef struct sym SYM;
struct sym
{
unsigned char name[50];
char value_type;
int int_value;
float float_value;
SYM * sig;
};
// EStructura de nodo del arbol sintactico
typedef struct asr ASR;
struct asr {
int node_type;
unsigned char name[50];
char value_type;
int int_value;
float float_value;
ASR * izquierda;
ASR * derecha;
ASR * sig;
SYM * simb;
};
extern int yylex();
extern FILE *yyin;
extern int line;
int yyerror(char const * s);
ASR * new_tree_node(int, unsigned char [], char, int, float, ASR *, ASR *, ASR *,SYM*);
ASR * search_node_tree(ASR *,unsigned char []);
char revision_tipos(ASR *);
void tabla_simb();
void tabla_simbf();
void tabla_simbf_par();
unsigned int hash(unsigned char []);
SYM * nuevo_nodo_tabla(unsigned char [], int);
SYM * buscar_simbolo(unsigned char []);
void insert_table_node(SYM *);
SYM * nuevo_nodo_tabla_f(unsigned char [], int);
SYM * buscar_simbolo_f(unsigned char []);
void insert_table_node_f(SYM *);
SYM * nuevo_nodo_tabla_fpar(unsigned char [], int);
SYM * buscar_simbolo_fpar(unsigned char []);
void insert_table_node_fpar(SYM *);
void imprimir_sym();
//metodos del interprete
void check_tree(ASR *);
int expr_int_value(ASR * root);
float expr_float_value(ASR * root);
char expr_value_type(ASR * root);
void print_tree(ASR *, int);
void print_table();
//inicializacion tabla y lista de simbolos
ASR *tree = NULL; // arbol del programa
ASR * tree_fun = NULL; //arbol de funciones
ASR *list = NULL; // lista de simbolos
ASR *list_fun = NULL; // lista de id de funciones
SYM *table[N]; // tabla hash de simbolos globales
SYM *tablef[N]; // tabla hash de funciones
SYM *tablefpar[N]; // tabla hash de funciones parametros
void igualar_params(ASR*,ASR*);
%}
%union{
struct asr * yynodo;
unsigned char yyid[50];
int yyint;
float yyfloat;
char yytipo;
}
%token END PROGRAM BEGINI IF ENDIF ELSE FOR STEP DO WHILE READ PRINT SUMA RESTA MULTI DIVIDE PARENI PAREND EQUAL MENORQ MAYORQ MENORIQ MAYORIQ PCOMA DOSPUNTOS COMA OTRO INT FLOAT CONS VAR PYC REPEAT UNTIL ASSIGN THEN FUN CALL RETRN PARS EXEC
%precedence THEN
%precedence ELSE
%token<yyint> NINT
%token<yyfloat> NFLOAT
%token<yyid> IDF
%type <yytipo> type
%type <yynodo> stmt stmt_lst expr term factor expresion opt_stmts opt_exprs expr_lst oparams params param opt_fun_decls fun_decls fun_decl opt_decls decl_lst decl
%start prog
%%
prog : PROGRAM IDF opt_decls opt_fun_decls BEGINI opt_stmts END { list = $3; tree = $6; tree_fun = $4; }
;
opt_decls : //palabra vacia
{ $$ = NULL; amb2++; }
| decl_lst { $$ = $1; amb2++;}
;
decl_lst : decl PCOMA decl_lst { $1 -> sig = $3, $$ = $1; }
| decl { $$ = $1; }
;
decl : VAR IDF DOSPUNTOS type {
if (amb2 == 0){
SYM *n2 = nuevo_nodo_tabla($2, $4);
if (buscar_simbolo($2) != NULL){
yyerror("Esta variable ya existe");
}
insert_table_node(n2);
$$ = new_tree_node(VAR, $2, $4, 0, 0.0, NULL, NULL, NULL,n2);
}else{
SYM *n2 = nuevo_nodo_tabla_fpar($2, $4);
if (buscar_simbolo_fpar($2) != NULL){
yyerror("Esta variable ya existe");
}
insert_table_node_fpar(n2);
$$ = new_tree_node(VAR, $2, $4, 0, 0.0, NULL, NULL, NULL,n2);
}
};
type : INT { $$ = 'i'; }
| FLOAT { $$ = 'f'; }
;
//declaracion de funciones
opt_fun_decls: { $$ = NULL; amb++;}//palabra vacía
| fun_decls { $$ = $1; amb++;};
fun_decls : fun_decls fun_decl { $1 -> sig = $2, $$ = $1; }
| fun_decl { $$ = $1; };
fun_decl : FUN IDF PARENI oparams PAREND DOSPUNTOS type opt_decls BEGINI opt_stmts END {
SYM *n2 = nuevo_nodo_tabla_f($2, $7);
if (buscar_simbolo_f($2) != NULL){
yyerror("Esta funcion ya existe");
}
insert_table_node_f(n2);
$$ = new_tree_node(FUN, $2, $7, 0, 0.0, $4 ,
new_tree_node(EXEC, $2, $7, 0, 0.0, $8 ,$10,NULL,n2),NULL,n2); }
|FUN IDF PARENI oparams PAREND DOSPUNTOS type PCOMA{
SYM *n2 = nuevo_nodo_tabla_f($2, $7);
if (buscar_simbolo_f($2) != NULL){
yyerror("Esta variable ya existe");
}
insert_table_node_f(n2);
$$ = new_tree_node(FUN, $2, $7, 0, 0.0, $4 ,NULL, NULL,n2);
};
oparams: {$$ = NULL;}
| params {$$ = $1;};
params:
param COMA params { $1 -> sig = $3, $$ = $1; }
|param{$$ = $1;};
param: IDF DOSPUNTOS type {
SYM *n4 = nuevo_nodo_tabla_fpar($1, $3);
if (buscar_simbolo_fpar($1) != NULL){
yyerror("Esta variable ya existe");
}
insert_table_node_fpar(n4);
$$ = new_tree_node(PARS, $1, $3, 0, 0.0, NULL ,NULL, NULL,n4);
};
stmt : IDF ASSIGN expr
{
SYM * n = NULL;
if (amb == 0){
n = buscar_simbolo_fpar($1);
}else{
n = buscar_simbolo($1);
}
if (n == NULL) {
yyerror("Variable no declarada");
} if (n -> value_type != revision_tipos($3)) {
yyerror("Tipos incompatibles");
}
$$ = new_tree_node(PYC, ";", '0', 0, 0.0, new_tree_node(ASSIGN, "assign", '0', 0, 0.0, new_tree_node(VAR, $1, '0', 0, 0.0, NULL, NULL, NULL,n), $3, NULL,NULL), NULL, NULL,NULL); }
| IF expresion THEN stmt { $$ = new_tree_node(PYC, ";", '0', 0, 0.0, new_tree_node(IF, "if", '0', 0, 0.0, $2, $4, NULL,NULL), NULL, NULL,NULL); }
| IF expresion THEN stmt ELSE stmt { $$ = new_tree_node(PYC, ";", '0', 0, 0.0, new_tree_node(IF, "if", '0', 0, 0.0, $2, $4, $6,NULL), NULL, NULL,NULL); }
| WHILE PARENI expresion PAREND stmt { $$ = new_tree_node(PYC, ";", '0', 0, 0.0, new_tree_node(WHILE, "while", '0', 0, 0.0, $3,$5,NULL,NULL), NULL, NULL,NULL); }
| REPEAT stmt UNTIL PARENI expresion PAREND { $$ = new_tree_node(PYC, ";", '0', 0, 0.0, new_tree_node(REPEAT, "repeat", '0', 0, 0.0, $5, $2,NULL ,NULL), NULL, NULL,NULL); }
| FOR IDF ASSIGN expr UNTIL expr STEP expr DO stmt
{ SYM * n = NULL;
if (amb == 0){n = buscar_simbolo_fpar($2); }else{n = buscar_simbolo($2); }
if (n == NULL) {
yyerror("Variable no declarada");
}
if (n -> value_type != revision_tipos($4) || n -> value_type != revision_tipos($6) || n -> value_type != revision_tipos($8)) {
yyerror("Tipos incompatibles");
}
$$ = new_tree_node(PYC, ";", '0', 0, 0.0,
new_tree_node(FOR, "for", '0', 0, 0.0,
new_tree_node(PYC, ";", '0', 0, 0.0, new_tree_node(ASSIGN, "assign", '0', 0, 0.0, new_tree_node(VAR, $2, '0', 0, 0.0, NULL, NULL, NULL,n), $4, $6,NULL), NULL, NULL,NULL),$8,$10,NULL),NULL,NULL,NULL);
}
| READ IDF { SYM * n = NULL; if (amb == 0){n = buscar_simbolo_fpar($2); }else{n = buscar_simbolo($2); }if (n == NULL) { yyerror("Variable no declarada"); } $$ = new_tree_node(PYC, ";", '0', 0, 0.0, new_tree_node(READ, "read", '0', 0, 0.0, new_tree_node(VAR, $2, '0', 0, 0.0, NULL, NULL, NULL,n), NULL, NULL,NULL), NULL, NULL,NULL); }
| PRINT expr { $$ = new_tree_node(PYC, ";", '0', 0, 0.0, new_tree_node(PRINT, "print", '0', 0, 0.0, $2, NULL, NULL,NULL), NULL, NULL,NULL); }
| RETRN expr { char c = expr_value_type($2); $$ = new_tree_node(RETRN, "return", c, 0, 0.0, $2, NULL, NULL,NULL); }
| BEGINI opt_stmts END { $$ = $2; }
;
opt_stmts : //palabra vacia
{ $$ = NULL; }
| stmt_lst { $$ = $1; }
;
stmt_lst : stmt { $$ = $1; }
| stmt PCOMA stmt_lst { $1 -> sig = $3, $$ = $1; }
;
expr : expr SUMA term { char c1 = revision_tipos($1); if (c1 != revision_tipos($3)) { yyerror("Tipos incompatibles"); } $$ = new_tree_node(SUMA, "+", c1, 0, 0.0, $1, $3, NULL,NULL); }
| expr RESTA term { char c1 = revision_tipos($1); if (c1 != revision_tipos($3)) { yyerror("Tipos incompatibles"); } $$ = new_tree_node(RESTA, "-", c1, 0, 0.0, $1, $3, NULL,NULL); }
| term {$$ = $1;}
;
term : term MULTI factor { char c1 = revision_tipos($1); if (c1 != revision_tipos($3)) { yyerror("Tipos incompatibles"); } $$ = new_tree_node(MULTI, "*", c1, 0, 0.0, $1, $3, NULL,NULL); }
| term DIVIDE factor { char c1 = revision_tipos($1); if (c1 != revision_tipos($3)) { yyerror("Tipos incompatibles"); } $$ = new_tree_node(DIVIDE, "/", c1, 0, 0.0, $1, $3, NULL,NULL); }
| factor {$$ = $1;}
;
factor : PARENI expr PAREND { $$ = $2; }
| IDF { SYM * n = NULL; if (amb == 0){n = buscar_simbolo_fpar($1); }else{n = buscar_simbolo($1); } if (n == NULL) { yyerror("Variable no declarada."); } $$ = new_tree_node(VAR, $1, '0', 0, 0.0, NULL, NULL, NULL,n); }
| NINT { $$ = new_tree_node(CONS, "int", 'i', $1, 0, NULL, NULL, NULL, NULL); }
| NFLOAT { $$ = new_tree_node(CONS, "float", 'f', 0, $1, NULL, NULL, NULL, NULL); }
| IDF PARENI opt_exprs PAREND { SYM * n = buscar_simbolo_f($1); if (n == NULL) { yyerror("función no declarada."); }
$$ = new_tree_node(CALL, $1, '0', 0, 0.0, $3,NULL, NULL,n); }
;
opt_exprs: {$$ = NULL;}
| expr_lst {$$ = $1;};
expr_lst: expr COMA expr_lst {$1 -> sig = $3, $$ = $1;}
| expr {$$ = $1;};
expresion : expr MENORQ expr { char c1 = revision_tipos($1); if (c1 != revision_tipos($3)) { yyerror("Tipos incompatibles"); } $$ = new_tree_node(MENORQ, "<", c1, 0, 0.0, $1, $3, NULL,NULL); }
| expr MAYORQ expr { char c1 = revision_tipos($1); if (c1 != revision_tipos($3)) { yyerror("Tipos incompatibles"); } $$ = new_tree_node(MAYORQ, ">", c1, 0, 0.0, $1, $3, NULL,NULL); }
| expr EQUAL expr { char c1 = revision_tipos($1); if (c1 != revision_tipos($3)) { yyerror("Tipos incompatibles"); } $$ = new_tree_node(EQUAL, "=", c1, 0, 0.0, $1, $3, NULL,NULL); }
| expr MENORIQ expr { char c1 = revision_tipos($1); if (c1 != revision_tipos($3)) { yyerror("Tipos incompatibles"); } $$ = new_tree_node(MENORIQ, "<=", c1, 0, 0.0, $1, $3, NULL,NULL); }
| expr MAYORIQ expr { char c1 = revision_tipos($1); if (c1 != revision_tipos($3)) { yyerror("Tipos incompatibles"); } $$ = new_tree_node(MAYORIQ, ">=", c1, 0, 0.0, $1, $3, NULL,NULL); }
;
%%
int yyerror(char const * s)
{
fprintf(stderr, "Error en la linea %i: %s\n", line, s);
exit(1);
}
void main(int argc, char *argv[])
{
yyin = fopen(argv[1], "r");
//limpieza de tablas
tabla_simb();
tabla_simbf();
tabla_simbf_par();
yyparse();
//ejecutar arbol
check_tree(tree);
// imprimir_sym(); imprimir tabla de simbolos
exit(0);
}
// Nuevo nodo de arbol
ASR * new_tree_node(int node_type, unsigned char name[], char value_type, int int_value, float float_value, ASR * izquierda, ASR * derecha, ASR * sig,SYM*simb)
{
ASR * aux = (ASR *) malloc(sizeof(ASR));
aux -> node_type = node_type;
strcpy(aux -> name, name);
aux -> value_type = value_type;
aux -> int_value = int_value;
aux -> float_value = float_value;
aux -> izquierda = izquierda;
aux -> derecha = derecha;
aux -> sig = sig;
aux -> simb = simb;
return aux;
}
SYM * nuevo_nodo_tabla(unsigned char name[], int type)
{
SYM * aux = (SYM *) malloc(sizeof(SYM)); //nodo de simbolo
strcpy(aux -> name, name);
aux -> value_type = type;
return aux;
}
void insert_table_node(SYM *t)
{
unsigned int i = hash(t -> name);
if (table[i] == NULL) { table[i] = t; }
else
{
SYM *n = table[i];
while (n -> sig != NULL) {
n = n -> sig;
}
n -> sig = t;
}
}
/*metodos para funciones*/
//tabla de funciones
SYM * nuevo_nodo_tabla_f(unsigned char name[], int type)
{
SYM * aux = (SYM *) malloc(sizeof(SYM)); //nodo de simbolo
strcpy(aux -> name, name);
aux -> value_type = type;
return aux;
}
void insert_table_node_f(SYM *t)
{
unsigned int i = hash(t -> name);
if (tablef[i] == NULL) { tablef[i] = t; }
else
{
SYM *n = tablef[i];
while (n -> sig != NULL) {
n = n -> sig;
}
n -> sig = t;
}
}
// busqueda EN SIMBOLOS de funciones
SYM * buscar_simbolo_f(unsigned char name[])
{
SYM *n = tablef[hash(name)];
while (n != NULL)
{
if (strcmp(n -> name, name) == 0) {
return n;
}
n = n -> sig;
}
return NULL;
}
/*metodos para parametros de funciones*/
//tabla de parametros de funciones
SYM * nuevo_nodo_tabla_fpar(unsigned char name[], int type)
{
SYM * aux = (SYM *) malloc(sizeof(SYM)); //nodo de simbolo
strcpy(aux -> name, name);
aux -> value_type = type;
return aux;
}
void insert_table_node_fpar(SYM *t)
{
unsigned int i = hash(t -> name);
if (tablefpar[i] == NULL) { tablefpar[i] = t; }
else
{
SYM *n = tablefpar[i];
while (n -> sig != NULL) {
n = n -> sig;
}
n -> sig = t;
}
}
// busqueda EN SIMBOLOS de funciones
SYM * buscar_simbolo_fpar(unsigned char name[])
{
SYM *n = tablefpar[hash(name)];
while (n != NULL)
{
if (strcmp(n -> name, name) == 0) {
return n;
}
n = n -> sig;
}
return NULL;
}
// busqueda
SYM * buscar_simbolo(unsigned char name[])
{
SYM *n = table[hash(name)];
while (n != NULL)
{
if (strcmp(n -> name, name) == 0) {
return n;
}
n = n -> sig;
}
return NULL;
}
// Revision de tipos
char revision_tipos(ASR * root)
{
ASR *n = root;
if (n -> node_type == CONS) {
return n -> value_type; // Si es un numero constante
}
else if (n -> node_type == CALL) {
return n -> simb -> value_type;
} // llamada a funciónrevision_tipos(AS
else if (n -> node_type == VAR) {
return n -> simb -> value_type;
}
else if (n -> node_type == PARS) {
return n -> simb -> value_type;
} // parametro de funcion
char a = revision_tipos(n -> izquierda);
char b = revision_tipos(n -> derecha);
if (a == b) { return a; }
else { yyerror("Tipos incompatibles"); }
}
void imprimir_sym()
{
for (int i = 0; i < N; i++) // Iterate array
{
SYM *n = tablefpar[i];
while (n != NULL) // Iterate list
{
printf("nombre: %s,tipo: %c,int:%i, float:%f\n", n -> name, n -> value_type, n -> int_value, n -> float_value); // Content
n = n-> sig;
}
}
}
void tabla_simb()
{
for (int i = 0; i < N; i++) { table[i] = NULL; } // Iterate array
}
void tabla_simbf()
{
for (int i = 0; i < N; i++) { tablef[i] = NULL; } // Iterate array
}
void tabla_simbf_par()
{
for (int i = 0; i < N; i++) { tablefpar[i] = NULL; } // Iterate array
}
unsigned int hash(unsigned char w[])
{
unsigned int hash = 0;
int c;
while ((c = *w++)) { hash = c + (hash << 6) + (hash << 16) - hash; }
return hash % N;
}
ASR * search_node_tree(ASR * root, unsigned char name[]){
ASR *n = root;
while (n != NULL)
{
if (strcmp(n -> name, name) == 0) {
return n;
}
n = n -> sig;
}
return NULL;
}
void igualar_params(ASR* in_par,ASR* params_final){
ASR * in = in_par;
ASR * out = params_final;
while((out != NULL) ){
if(in != NULL ){
if(revision_tipos(out) == 'i'){
if(revision_tipos(in) == 'i'){
out -> simb -> int_value = expr_int_value(in);
}
else{
yyerror("Tipos incompatibles"); }
}
else{
if(revision_tipos(in) == 'f'){
out -> simb -> float_value = expr_float_value(in);
}
else{
yyerror("Tipos incompatibles"); }
}
out = out -> sig; in = in -> sig;
}else{
yyerror("Faltan parametros en la función");
}
}
}
//Execute tree nodes actions
void check_tree(ASR * root)
{
ASR *parent = root; // ; node
// NULL
if (parent == NULL ) { return; }
// Bloque punto y coma
if (parent -> node_type == PYC)
{
ASR *n = parent -> izquierda; //nodo por izquierda
// tipo asignacion
if (n -> node_type == ASSIGN)
{
// SYM *t = buscar_simbolo(n -> izquierda -> name); // nombre de la variable en la tabla de simbolos, se busca pro nombre
SYM *t = n -> izquierda -> simb;
if (t -> value_type == 'i') { t -> int_value = expr_int_value(n -> derecha); } // Int value
else { t -> float_value = expr_float_value(n -> derecha); } // Float value
}
// IF-else
if (n -> node_type == IF)
{
char * op = n -> izquierda -> name; // =, > or <
if (expr_value_type(n -> izquierda) == 'i') // Int type
{
int l_expr = expr_int_value(n -> izquierda -> izquierda); // izquierda expr
int r_expr = expr_int_value(n -> izquierda -> derecha); // derecha expr
if (strcmp(op, "=") == 0) { if (l_expr == r_expr) { check_tree(n -> derecha); } else { check_tree(n -> sig); } } // =
else if (strcmp(op, ">") == 0) { if (l_expr > r_expr) { check_tree(n -> derecha); } else { check_tree(n -> sig); } } // >
else if (strcmp(op, "<") == 0) { if (l_expr < r_expr) { check_tree(n -> derecha); } else { check_tree(n -> sig); } } // <
else if (strcmp(op, "<=") == 0) { if (l_expr <= r_expr) { check_tree(n -> derecha); } else { check_tree(n -> sig); } } // <=
else if (strcmp(op, ">=") == 0) { if (l_expr >= r_expr) { check_tree(n -> derecha); } else { check_tree(n -> sig); } } // >=
}
else // Float type
{
float l_expr = expr_float_value(n -> izquierda -> izquierda); // izquierda expr
float r_expr = expr_float_value(n -> izquierda -> derecha); // derecha expr
if (strcmp(op, "=") == 0) { if (l_expr == r_expr) { check_tree(n -> derecha); } else { check_tree(n -> sig); } } // =
else if (strcmp(op, ">") == 0) { if (l_expr > r_expr) { check_tree(n -> derecha); } else { check_tree(n -> sig); } } // >
else if (strcmp(op, "<") == 0) { if (l_expr < r_expr) { check_tree(n -> derecha); } else { check_tree(n -> sig); } } // <
else if (strcmp(op, "<=") == 0) { if (l_expr <= r_expr) { check_tree(n -> derecha); } else { check_tree(n -> sig); } } // <=
else if (strcmp(op, ">=") == 0) { if (l_expr >= r_expr) { check_tree(n -> derecha); } else { check_tree(n -> sig); } } // >=
}
}
//WHILE
if (n -> node_type == WHILE)
{
char * op = n -> izquierda -> name;
if (expr_value_type(n -> izquierda) == 'i') // Int type
{
int l_expr = expr_int_value(n -> izquierda -> izquierda); // izquierda expr
int r_expr = expr_int_value(n -> izquierda -> derecha); // derecha expr
if (strcmp(op, "=") == 0) // =
{
while (l_expr == r_expr){
check_tree(n -> derecha);
l_expr = expr_int_value(n -> izquierda -> izquierda);
r_expr = expr_int_value(n -> izquierda -> derecha);
}
}
else if (strcmp(op, ">") == 0) // >
{
while (l_expr > r_expr){
check_tree(n -> derecha);
l_expr = expr_int_value(n -> izquierda -> izquierda);
r_expr = expr_int_value(n -> izquierda -> derecha);
}
}
else if (strcmp(op, "<") == 0) // <
{
while (l_expr < r_expr){
check_tree(n -> derecha);
l_expr = expr_int_value(n -> izquierda -> izquierda);
r_expr = expr_int_value(n -> izquierda -> derecha);
}
}
else if (strcmp(op, "<=") == 0) // <
{
while (l_expr <= r_expr){
check_tree(n -> derecha);
l_expr = expr_int_value(n -> izquierda -> izquierda);
r_expr = expr_int_value(n -> izquierda -> derecha);
}
}
else if (strcmp(op, ">=") == 0) // <
{
while (l_expr >= r_expr){
check_tree(n -> derecha);
l_expr = expr_int_value(n -> izquierda -> izquierda);
r_expr = expr_int_value(n -> izquierda -> derecha);
}
}
}
else // Float type
{
float l_expr = expr_float_value(n -> izquierda -> izquierda); // izquierda expr
float r_expr = expr_float_value(n -> izquierda -> derecha); // derecha expr
if (strcmp(op, "=") == 0) // =
{
while (l_expr == r_expr){
check_tree(n -> derecha);
l_expr = expr_float_value(n -> izquierda -> izquierda);
r_expr = expr_float_value(n -> izquierda -> derecha);
}
}
else if (strcmp(op, ">") == 0) // >
{
while (l_expr > r_expr){
check_tree(n -> derecha);
l_expr = expr_float_value(n -> izquierda -> izquierda);
r_expr = expr_float_value(n -> izquierda -> derecha);
}
}
else if (strcmp(op, "<") == 0) // <
{
while (l_expr < r_expr){
check_tree(n -> derecha);
l_expr = expr_float_value(n -> izquierda -> izquierda);
r_expr = expr_float_value(n -> izquierda -> derecha);
}
}
else if (strcmp(op, "<=") == 0) // <
{
while (l_expr <= r_expr){
check_tree(n -> derecha);
l_expr = expr_float_value(n -> izquierda -> izquierda);
r_expr = expr_float_value(n -> izquierda -> derecha);
}
}
else if (strcmp(op, ">=") == 0) // <
{
while (l_expr >= r_expr){
check_tree(n -> derecha);
l_expr = expr_float_value(n -> izquierda -> izquierda);
r_expr = expr_float_value(n -> izquierda -> derecha);
}
}
}
}
// REPEAT UNTIL
if (n -> node_type == REPEAT)
{
char * op = n -> izquierda -> name; // =, > or <
if (expr_value_type(n -> izquierda) == 'i') // Int type
{
int l_expr = expr_int_value(n -> izquierda -> izquierda); // izquierda expr
int r_expr = expr_int_value(n -> izquierda -> derecha); // derecha expr
if (strcmp(op, "=") == 0) // =
{
do{
check_tree(n -> derecha);
l_expr = expr_int_value(n -> izquierda -> izquierda);
r_expr = expr_int_value(n -> izquierda -> derecha);
}while (l_expr == r_expr);
}
else if (strcmp(op, ">") == 0) // >
{
do{
check_tree(n -> derecha);
l_expr = expr_int_value(n -> izquierda -> izquierda);
r_expr = expr_int_value(n -> izquierda -> derecha);
}while (l_expr > r_expr);
}
else if (strcmp(op, "<") == 0) // <
{
do{
check_tree(n -> derecha);
l_expr = expr_int_value(n -> izquierda -> izquierda);
r_expr = expr_int_value(n -> izquierda -> derecha);
}while (l_expr < r_expr);
}
else if (strcmp(op, "<=") == 0) // <
{
do{
check_tree(n -> derecha);
l_expr = expr_int_value(n -> izquierda -> izquierda);
r_expr = expr_int_value(n -> izquierda -> derecha);
}while (l_expr <= r_expr);
}
else if (strcmp(op, ">=") == 0) // <
{
do{
check_tree(n -> derecha);
l_expr = expr_int_value(n -> izquierda -> izquierda);
r_expr = expr_int_value(n -> izquierda -> derecha);
}while (l_expr >= r_expr);
}
}
else // Float type
{
float l_expr = expr_float_value(n -> izquierda -> izquierda); // izquierda expr
float r_expr = expr_float_value(n -> izquierda -> derecha); // derecha expr
if (strcmp(op, "=") == 0) // =
{
do{
check_tree(n -> derecha);
l_expr = expr_float_value(n -> izquierda -> izquierda);
r_expr = expr_float_value(n -> izquierda -> derecha);
}while (l_expr == r_expr);
}
else if (strcmp(op, ">") == 0) // >
{
do{
check_tree(n -> derecha);
l_expr = expr_float_value(n -> izquierda -> izquierda);
r_expr = expr_float_value(n -> izquierda -> derecha);
}while (l_expr > r_expr);
}
else if (strcmp(op, "<") == 0) // <
{
do{
check_tree(n -> derecha);
l_expr = expr_float_value(n -> izquierda -> izquierda);
r_expr = expr_float_value(n -> izquierda -> derecha);
}while (l_expr < r_expr);
}
else if (strcmp(op, "<=") == 0) // <
{
do{
check_tree(n -> derecha);
l_expr = expr_float_value(n -> izquierda -> izquierda);
r_expr = expr_float_value(n -> izquierda -> derecha);
}while (l_expr <= r_expr);
}
else if (strcmp(op, ">=") == 0) // <
{
do{
check_tree(n -> derecha);
l_expr = expr_float_value(n -> izquierda -> izquierda);
r_expr = expr_float_value(n -> izquierda -> derecha);
}while (l_expr >= r_expr);
}
}
}
// FOR
if (n -> node_type == FOR){
check_tree(n -> izquierda);
if (expr_value_type(n -> izquierda -> izquierda -> izquierda) == 'i') // Int type
{
for (int i = expr_int_value(n -> izquierda -> izquierda-> izquierda); i <= expr_int_value(n -> izquierda -> izquierda -> sig); i = i + expr_int_value(n -> derecha) )
{
check_tree(n -> sig);
}
}
else{
for (float j = expr_float_value(n -> izquierda -> izquierda-> izquierda); j <= expr_float_value(n -> izquierda -> izquierda -> sig); j = j + expr_float_value(n -> derecha) )
{
check_tree(n -> sig);
}
}
}
// READ
if (n -> node_type == READ)
{
SYM *t = n -> izquierda -> simb; //buscar_simbolo(n -> izquierda -> name); // Get variable from the symbol table
if (expr_value_type(n -> izquierda) == 'i') { scanf("%i", &(t -> int_value)); } // Int type
else { scanf("%f", &(t -> float_value)); } // Float type
}
// PRINT
if (n -> node_type == PRINT)
{
if (expr_value_type(n -> izquierda) == 'i') { printf("%i\n", expr_int_value(n -> izquierda)); } // Int type
else { printf("%f\n", expr_float_value(n -> izquierda)); } // Float type
}
}
//ejecucion de función
if (parent -> node_type == EXEC)
{
check_tree(parent -> derecha);
}
//retornar
if (parent -> node_type == RETRN)
{
if (expr_value_type(parent -> izquierda) == 'i'){
retorno = expr_int_value(parent -> izquierda);
return;
}
else{
retorno = expr_float_value(parent -> izquierda);
return;
}
}
check_tree(parent -> sig);
}
//valor entero de expr
int expr_int_value(ASR * root)
{
int aux1, aux2;
if (root == NULL) return 0;
if (root -> node_type == SUMA || root -> node_type == RESTA || root -> node_type == MULTI || root -> node_type == DIVIDE)
{
aux1 = expr_int_value(root -> izquierda); // izquierda expr
aux2 = expr_int_value(root -> derecha); // derecha expr
if (root -> node_type == SUMA) { return aux1 + aux2; } // +
if (root -> node_type == RESTA) { return aux1 - aux2; } // -
if (root -> node_type == MULTI) { return aux1 * aux2; } // *
if (root -> node_type == DIVIDE) { return aux1 / aux2; } // /
}
else if (root -> node_type == CONS) { return root -> int_value; } // Constant
else if (root -> node_type == VAR) { return root -> simb -> int_value;}//buscar_simbolo(root -> name) -> int_value; } // Variable
else if (root -> node_type == PARS) { return root -> simb -> int_value;} //buscar_simbolo_fpar(root -> name) -> int_value; } // Variable
else if (root -> node_type == CALL) {
ASR * aux = search_node_tree(tree_fun, root -> simb -> name);
igualar_params(root -> izquierda,aux -> izquierda);
check_tree(aux -> derecha);
return retorno;
}
}
// valor flotante de expr
float expr_float_value(ASR * root)
{
float aux1, aux2;
if (root == NULL) return 0.0;
if (root -> node_type == SUMA || root -> node_type == RESTA || root -> node_type == MULTI || root -> node_type == DIVIDE)
{
aux1 = expr_float_value(root -> izquierda); // izquierda expr
aux2 = expr_float_value(root -> derecha); // derecha expr
if (root -> node_type == SUMA) { return aux1 + aux2; } // +
if (root -> node_type == RESTA) { return aux1 - aux2; } // -
if (root -> node_type == MULTI) { return aux1 * aux2; } // *
if (root -> node_type == DIVIDE) { return aux1 / aux2; } // /
}
else if (root -> node_type == CONS) { return root -> float_value; } // Constant
else if (root -> node_type == VAR) { return root -> simb -> float_value; }//buscar_simbolo(root -> name) -> float_value; } // Variable
else if (root -> node_type == PARS) { return root -> simb -> float_value;}// buscar_simbolo_fpar(root -> name) -> float_value; } // Variable
else if (root -> node_type == CALL) {
ASR * aux = search_node_tree(tree_fun, root -> simb -> name);