-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.h
1191 lines (1111 loc) · 33.3 KB
/
ast.h
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
/*
Traductores e Interpretadores (CI-3725)
Autores:
Guillermo Betancourt, carnet 11-10103
Gabriel Giménez, carnet 12-11006
ast.h:
Contiene la definicion del arbol sintactico asi como la de la tabla de simbolos utilizadas
en el proyecto
*/
#include <stdio.h>
#include <iostream>
#include <stdexcept>
#include <map>
#include <vector>
#include <ctype.h>
#include <cstdlib>
#include "arb_def.h"
#include "robot.h"
using namespace std;
#define NUMEROS 1
#define BOOLEANOS 2
#define CHARACTERS 3
#define CONDICION 4
/* Definiciones externas (parser.y)
que permiten compartir el codigo.
*/
extern tabla_simbolos * head_table;
extern int yylineno;
extern char error_strp[1000];
extern map<string,Robot *> robots;
extern int yyparse();
extern map<int, map<int, variable *> > matriz_bot;
extern Robot * working_bot;
/* Definicion de la clase numero */
class numero : public ArbolSintactico {
public:
int valor;
numero(): ArbolSintactico(NUMEROS) {is_type = 1;}
numero(int v) : valor(v), ArbolSintactico(NUMEROS){}
virtual void imprimir(int i) {
if (!is_type){
for (int j = 0; j < i; j++) cout << " ";
cout << "numero: " << valor << endl;
}
}
virtual int * get_value(){
return &valor;
}
};
/* Definicion de la clase booleano */
class booleano : public ArbolSintactico {
public:
bool valor;
booleano(): ArbolSintactico(BOOLEANOS) {is_type = 1;}
booleano(bool v) : valor(v), ArbolSintactico(BOOLEANOS) {}
virtual void imprimir(int i) {
if (!is_type){
for (int j = 0; j < i; j++) cout << " ";
cout << "booleano: " << valor << endl;
}
}
virtual bool * get_bool(){
return &valor;
}
};
/* Definicion de la clase caracter */
class character : public ArbolSintactico {
public:
char valor;
character() : ArbolSintactico(CHARACTERS) {is_type = 1;}
character(char v) : valor(v), ArbolSintactico(CHARACTERS) {}
virtual void imprimir(int i) {
if (!is_type){
for (int j = 0; j < i; j++) cout << " ";
cout << "character: " << valor << endl;
}
}
virtual char * get_character(){
return &valor;
}
};
/* Definicion de la clase raiz */
class raiz : public ArbolSintactico {
public:
ArbolSintactico * declaraciones;
ArbolSintactico * ejecucion;
tabla_simbolos table;
raiz(ArbolSintactico * l) : ejecucion(l), table() {}
raiz(ArbolSintactico * d, ArbolSintactico * l) : declaraciones(d), ejecucion(l), table(){}
virtual void imprimir(int i){
for (int j = 0; j < i; j++) cout << " ";
for (int j = 0; j < 15; j++) cout << "-";
cout << endl;
for (int j = 0; j < i; j++) cout << " ";
if(declaraciones != NULL){
cout << "DECLARACIONES:" << endl;
declaraciones -> imprimir(i+1);
}
for (int j = 0; j < i; j++) cout << " ";
if(ejecucion != NULL){
cout << "SECUENCIACION:" << endl;
ejecucion -> imprimir(i+1);
}
}
virtual void ejecutar(){
//cout << "Ejecutando raiz" << endl;
head_table = new tabla_simbolos(&table);
//head_table->print_rob();
ejecucion -> ejecutar();
if (head_table->padre != NULL){
head_table = head_table->padre;
}
}
};
/* Definicion de la clase instruccion
es utilizada para generar una lista de instrucciones */
class instruccion : public ArbolSintactico {
public:
ArbolSintactico * left;
ArbolSintactico * rigth;
instruccion(ArbolSintactico * r) : rigth(r) {}
instruccion(ArbolSintactico * l, ArbolSintactico * r) : left(l), rigth(r) {}
virtual void imprimir(int i){
if (left != NULL ) {
left -> imprimir(i);
}
if (rigth != NULL ) {
rigth -> imprimir(i);
}
}
virtual void add_variable(int tipo, bool doble) {
if (rigth != NULL){
rigth->add_variable(tipo, doble);
}
if (left != NULL){
left->add_variable(tipo, doble);
}
}
virtual void check(){
rigth->check();
if (left != NULL){
left->check();
}
}
virtual void ejecutar(){
if (left != NULL){
left->ejecutar();
}
rigth->ejecutar();
}
virtual void activate(){
if (left != NULL){
left->activate();
}
rigth->activate();
}
virtual void deactivate(){
if (left != NULL){
left->deactivate();
}
rigth->deactivate();
}
virtual bool advance(){
if (left != NULL){
if (left->advance()){
return true;
}
}
if (rigth->advance()){
return true;
}
return false;
}
virtual void add_comportamiento(ArbolSintactico * comp){
if (left != NULL){
left->add_comportamiento(comp);
}
rigth->add_comportamiento(comp);
}
virtual void add_value(variable * value){
if (left != NULL){
left->add_value(value);
}
rigth->add_value(value);
}
};
/* Definicion de la clase nodo de movimientos */
class intr_movimiento : public ArbolSintactico {
enum mov { UP, DOWN, LEFT, RIGTH };
public:
ArbolSintactico * down;
mov movimiento;
intr_movimiento(ArbolSintactico * l, int m) : down(l), movimiento(static_cast<mov>(m)) {check();}
intr_movimiento(int m) : movimiento(static_cast<mov>(m)) {}
virtual void imprimir(int i){
for (int j = 0; j < i; j++) cout << " ";
switch(movimiento){
case UP:
cout << "UP:" << endl;
break;
case DOWN:
cout << "DOWN:" << endl;
break;
case LEFT:
cout << "LEFT:" << endl;
break;
case RIGTH:
cout << "RIGTH:" << endl;
break;
}
if (down != NULL){
down -> imprimir(i + 1);
}
}
/* Chequea que la expresion recibida sea un entero
Se debera luego chequear que sea mayor a 0 */
virtual void check(){
if (down->ident != NUMEROS){
sprintf(error_strp,"Error de tipo, se esperaba un booleano [LINEA: %d]", yylineno);
throw error_strp;
}
}
virtual void ejecutar(){
int mov = 1;
if (down != NULL){
mov = * down->get_value();
if (mov < 0){
sprintf(error_strp,"Error se esta moviendo en la matriz con un numero negativo. [LINEA: %d]", yylineno);
throw error_strp;
}
}
switch(movimiento){
case UP:
working_bot->posicion[0] += mov;
break;
case DOWN:
working_bot->posicion[0] -= mov;
break;
case LEFT:
working_bot->posicion[1] -= mov;
break;
case RIGTH:
working_bot->posicion[1] += mov;
break;
}
// cout << "Posicion nueva:" << working_bot->posicion[0] << ", " << working_bot->posicion[1] << " ADDED : " << mov << endl;
}
};
/* Definicion de la clase guardia, contiene instrucciones
que requieren la evaluacion de una condicion */
class intr_guardia : public ArbolSintactico {
enum inst { IF, IF_ELSE, WHILE };
public:
ArbolSintactico * condicion;
ArbolSintactico * cuerpo;
ArbolSintactico * cuerpo_else;
inst instruccion;
intr_guardia(ArbolSintactico * l, ArbolSintactico * d, int m) : condicion(l), cuerpo(d), instruccion(static_cast<inst>(m)) {check();}
intr_guardia(ArbolSintactico * l, ArbolSintactico * d, ArbolSintactico * ce, int m) :
condicion(l), cuerpo(d), cuerpo_else(ce), instruccion(static_cast<inst>(m)) {check();}
virtual void imprimir(int i){
for (int j = 0; j < i; j++) cout << " ";
switch(instruccion){
case IF:
cout << "IF:" << endl;
break;
case IF_ELSE:
cout << "IF:" << endl;
break;
case WHILE:
cout << "WHILE:" << endl;
break;
}
for (int j = 0; j < i; j++) cout << " ";
cout << "GUARDIA: " << endl;
condicion -> imprimir(i + 1);
for (int j = 0; j < i; j++) cout << " ";
cout << "CUERPO: " << endl;
cuerpo -> imprimir(i + 1);
if (cuerpo_else != NULL){
for (int j = 0; j < i; j++) cout << " ";
cout << "ELSE: " << endl;
cuerpo_else -> imprimir(i + 1);
}
}
/* Se verifica que la condicion sea del tipo BOOLEANA */
virtual void check(){
if(condicion->ident != BOOLEANOS){
sprintf(error_strp,"Error de tipo, se esperaba un booleano [LINEA: %d]", yylineno);
throw error_strp;
}
}
virtual void ejecutar(){
switch(instruccion){
case IF:
if (* condicion->get_bool()) {
cuerpo -> ejecutar();
}
break;
case IF_ELSE:
if (* condicion->get_bool()) {
cuerpo -> ejecutar();
} else {
cuerpo_else -> ejecutar();
}
break;
case WHILE:
while (* condicion->get_bool()) {
cuerpo -> ejecutar();
}
break;
}
}
};
/* Definicion de la clase que engloba las expresiones aritmeticas y devuelven un numero */
class expr_aritmetica : public ArbolSintactico {
enum inst { SUMA, RESTA, MULT, DIV, MOD, PARENTESIS, NEGATIVO, NUMERO};
public:
ArbolSintactico * numero_izq;
ArbolSintactico * numero_der;
inst instruccion;
expr_aritmetica(ArbolSintactico * i,ArbolSintactico * d, int m) : numero_izq(i), numero_der(d), instruccion(static_cast<inst>(m)), ArbolSintactico(NUMEROS) {check();}
expr_aritmetica(ArbolSintactico * d, int m) : numero_der(d), instruccion(static_cast<inst>(m)), ArbolSintactico(NUMEROS) {check();}
virtual void imprimir(int i){
if (numero_izq != NULL){
numero_izq -> imprimir(i + 1);
}
for (int j = 0; j < i; j++) cout << " ";
switch(instruccion){
case SUMA:
cout << "SUMA:" << endl;
break;
case RESTA:
cout << "RESTA:" << endl;
break;
case MULT:
cout << "MULT:" << endl;
break;
case DIV:
cout << "DIV:" << endl;
break;
case MOD:
cout << "MOD:" << endl;
break;
case PARENTESIS:
cout << "PARENTESIS:" << endl;
break;
case NEGATIVO:
cout << "NEGATIVO: " << endl;
break;
case NUMERO:
cout << "NUMERO: " << endl;
break;
}
if (numero_der != NULL){
numero_der -> imprimir(i + 1);
}
}
/* Se chequea que este bien compuesta la expresion, chequeando si son numeros */
virtual void check(){
if (instruccion == PARENTESIS){
ident = numero_der->ident;
}
if (instruccion != NEGATIVO && instruccion != NUMERO && instruccion != PARENTESIS){
if (numero_der->ident != NUMEROS ||
numero_izq->ident != NUMEROS){
sprintf(error_strp,"Error de tipo, se esperaban dos int [LINEA: %d]", yylineno);
throw error_strp;
}
} else {
if (numero_der->ident != NUMEROS && instruccion != PARENTESIS){
sprintf(error_strp,"Error de tipo, se esperaba un int [LINEA: %d]", yylineno);
throw error_strp;
}
}
}
virtual int * get_value(){
switch(instruccion){
case SUMA:
return new int(*numero_der->get_value() + *numero_izq->get_value());
case RESTA:
return new int(*numero_izq->get_value() - *numero_der->get_value());
case MULT:
return new int(*numero_der->get_value() * *numero_izq->get_value());
case DIV:
if(*numero_der->get_value() == 0) {
sprintf(error_strp,"Error division por cero. [LINEA: %d]", yylineno);
throw error_strp;
}
return new int(*numero_izq->get_value() / *numero_der->get_value());
case MOD:
return new int(*numero_der->get_value() % *numero_izq->get_value());
case PARENTESIS:
return numero_der->get_value();
case NEGATIVO:
return new int(-*numero_der->get_value());
case NUMERO:
return numero_der->get_value();
}
}
virtual bool * get_bool(){
return numero_der->get_bool();
}
};
/* Definicion de la clase que engloba instrucciones que
calificamos como "extra" */
class intr_extra : public ArbolSintactico {
enum mov { STORE, DROP };
public:
ArbolSintactico * down;
mov movimiento;
intr_extra(ArbolSintactico * l, int m) : down(l), movimiento(static_cast<mov>(m)) {}
virtual void imprimir(int i){
for (int j = 0; j < i; j++) cout << " ";
switch(movimiento) {
case STORE:
cout << "STORE:" << endl;
break;
case DROP:
cout << "DROP:" << endl;
break;
}
down -> imprimir(i + 1);
}
virtual void ejecutar(){
switch(movimiento){
case STORE:
switch(head_table->valores["me"]->tipo){
case NUMEROS: {
if (down -> ident != NUMEROS){
sprintf(error_strp,"Error de tipo al hacer STORE, se esperaba un int [LINEA: %d]", yylineno);
throw error_strp;
} else {
head_table->valores["me"]->init = true;
static_cast<variable_int * >(head_table->valores["me"])->valor = static_cast<expr_aritmetica *>(down)->get_value();
}
break;
}
case CHARACTERS: {
if (down -> ident != CHARACTERS){
sprintf(error_strp,"Error de tipo al hacer STORE, se esperaba un character [LINEA: %d]", yylineno);
throw error_strp;
} else {
head_table->valores["me"]->init = true;
static_cast<variable_char * >(head_table->valores["me"])->valor = static_cast<character *>(down)->get_character();
}
break;
}
case BOOLEANOS: {
if (down -> ident != BOOLEANOS){
sprintf(error_strp,"Error de tipo al hacer STORE, se esperaba un booleano [LINEA: %d]", yylineno);
throw error_strp;
} else {
head_table->valores["me"]->init = true;
static_cast<variable_bool * >(head_table->valores["me"])->valor = static_cast<booleano *>(down)->get_bool();
}
break;
}
}
break;
case DROP:
//cout <<"DROPEO" << endl;
switch(down -> ident){
case NUMEROS: {
variable_int * tmp = new variable_int(NUMEROS, down->get_value(), true);
matriz_bot[working_bot->posicion[0]][working_bot->posicion[1]] = tmp;
break;
}
case CHARACTERS: {
variable_char * tmp = new variable_char(CHARACTERS, down->get_character(), true);
matriz_bot[working_bot->posicion[0]][working_bot->posicion[1]] = tmp;
break;
}
case BOOLEANOS: {
variable_bool * tmp = new variable_bool(BOOLEANOS, down->get_bool(), true);
matriz_bot[working_bot->posicion[0]][working_bot->posicion[1]] = tmp;
break;
}
}
break;
}
}
};
/* Definicion de la clase que engloba las expresiones booleanas y devuelven un booleano */
class expr_booleana : public ArbolSintactico {
enum inst { IGUAL, MENOR, MAYOR, MENORIGUAL, MAYORIGUAL, PARENTESIS, DISYUNCION, CONJUNCION, NEGACION, CONSTANTE};
public:
ArbolSintactico * bool_izq;
ArbolSintactico * bool_der;
inst instruccion;
expr_booleana(ArbolSintactico * i,ArbolSintactico * d, int m) : bool_izq(i), bool_der(d), instruccion(static_cast<inst>(m)), ArbolSintactico(BOOLEANOS) {check();}
expr_booleana(ArbolSintactico * d, int m) : bool_der(d), instruccion(static_cast<inst>(m)), ArbolSintactico(BOOLEANOS) {check();}
virtual void imprimir(int i){
if (bool_izq != NULL){
bool_izq -> imprimir(i + 1);
}
for (int j = 0; j < i; j++) cout << " ";
switch(instruccion){
case IGUAL:
cout << "IGUAL:" << endl;
break;
case MENOR:
cout << "MENOR:" << endl;
break;
case MAYOR:
cout << "MAYOR:" << endl;
break;
case MENORIGUAL:
cout << "MENORIGUAL:" << endl;
break;
case MAYORIGUAL:
cout << "MAYORIGUAL:" << endl;
break;
case PARENTESIS:
cout << "PARENTESIS:" << endl;
break;
case DISYUNCION:
cout << "DISYUNCION:" << endl;
break;
case CONJUNCION:
cout << "CONJUNCION:" << endl;
break;
case NEGACION:
cout << "NEGACION:" << endl;
break;
case CONSTANTE:
cout << "COSTANTE:" << endl;
break;
}
if (bool_der != NULL){
bool_der -> imprimir(i + 1);
}
}
/* Se chequea que este bien compuesta la expresion. */
virtual void check(){
switch(instruccion){
case IGUAL:
if (bool_der->ident != bool_izq->ident){
sprintf(error_strp,"Error de tipo, los tipos no son iguales [LINEA: %d]", yylineno);
throw error_strp;
}
break;
case MENOR:
case MAYOR:
case MENORIGUAL:
case MAYORIGUAL:
if (bool_der->ident != NUMEROS ||
bool_izq->ident != NUMEROS) {
sprintf(error_strp,"Error de tipo, se esperaban dos int [LINEA: %d]", yylineno);
throw error_strp;
}
break;
case PARENTESIS:
case NEGACION:
if (bool_der->ident != BOOLEANOS){
sprintf(error_strp,"Error de tipo, se esperaba un booleano [LINEA: %d]", yylineno);
throw error_strp;
}
break;
case DISYUNCION:
case CONJUNCION:
if (bool_der->ident != BOOLEANOS ||
bool_izq->ident != BOOLEANOS){
sprintf(error_strp,"Error de tipo, se esperaban dos booleanos [LINEA: %d]", yylineno);
throw error_strp;
}
break;
}
}
virtual bool * get_bool(){
switch(instruccion){
case IGUAL:
return new bool(*bool_izq->get_value() == *bool_der->get_value());
case MENOR:
return new bool(*bool_izq->get_value() < *bool_der->get_value());
case MAYOR:
// cout << *bool_izq->get_value() << " > " << *bool_der->get_value() << endl;
return new bool(*bool_izq->get_value() > *bool_der->get_value());
case MENORIGUAL:
return new bool(*bool_izq->get_value() <= *bool_der->get_value());
case MAYORIGUAL:
return new bool(*bool_izq->get_value() >= *bool_der->get_value());
case PARENTESIS:
return bool_der->get_bool();
case DISYUNCION:
return new bool(*bool_der->get_bool() || *bool_izq->get_bool() );
case CONJUNCION:
return new bool(*bool_der->get_bool() && *bool_izq->get_bool() );
case NEGACION:
return new bool(!*bool_der->get_bool());
case CONSTANTE:
return bool_der->get_bool();
}
}
};
/* Definicion de la clase que engloba las condiciones de los comportamientos del robot*/
class on_condicion : public ArbolSintactico {
public:
enum inst { ACTIVATION, DEACTIVATION, DEFAULT, EXPR};
inst condicion;
ArbolSintactico * expr;
on_condicion(int v) : ArbolSintactico(CONDICION), condicion(static_cast<inst>(v)){ add_variable(0,0); }
on_condicion(int v, ArbolSintactico * l) : expr(l), ArbolSintactico(CONDICION), condicion(static_cast<inst>(v)){check();}
virtual void imprimir(int i) {
for (int j = 0; j < i; j++) cout << " ";
switch(condicion){
case ACTIVATION:
cout << "ACTIVATION: " << endl;
break;
case DEACTIVATION:
cout << "DEACTIVATION: " << endl;
break;
case DEFAULT:
cout << "DEFAULT: " << endl;
break;
case EXPR:
cout << "EXPRESION: " << endl;
expr->imprimir(i);
break;
}
}
/* Permite llevar un control del numero de Activation, Deactivation, Default declarados.*/
virtual void add_variable(int i, int doble) {
string nombre;
switch(condicion){
case ACTIVATION:
nombre = "activation";
break;
case DEACTIVATION:
nombre = "deactivation";
break;
case DEFAULT:
nombre = "default";
break;
}
if (head_table->mapa.count(nombre) > 0) {
const char * c = nombre.c_str();
sprintf(error_strp,"%s ya habia sido declarada antes. [LINEA: %d]", c, yylineno);
throw error_strp;
}
if (condicion == EXPR){
check();
}
head_table->mapa[nombre] = 0;
}
/* Verifica que el ultimo comportamiento sea el default */
virtual void check(){
if (head_table->mapa.count("default") > 0) {
sprintf(error_strp,"default debe ser el ultimo comportamiento. [LINEA: %d]", yylineno);
throw error_strp;
}
}
virtual bool * get_bool(){
return expr->get_bool();
}
};
/* Definicion de la clase identificador */
class identificador : public ArbolSintactico {
public:
string valor;
tabla_simbolos * tabla;
/* Chequea si es una variable solo local o tiene mayor alcance */
identificador(string v) : valor(v) {
if (head_table->mapa.count("me") > 0 ){
if (head_table->mapa.count(v) > 0){
ident = head_table->mapa.at(v);
} else {
ident = 0;
}
tabla = head_table;
} else {
tabla_simbolos * rtmp;
rtmp = head_table;
while (head_table != NULL){
if (head_table->mapa.count(v) > 0){
ident = head_table->mapa.at(v);
tabla = head_table;
break;
}
head_table = head_table->padre;
}
head_table = rtmp;
}
}
virtual void imprimir(int i) {
string ty;
switch (ident){
case NUMEROS:
ty = "numero";
break;
case BOOLEANOS:
ty = "booleano";
break;
case CHARACTERS:
ty = "character";
break;
}
for (int j = 0; j < i; j++) cout << " ";
cout << "identificador: " << valor.c_str() << " tipo: " << ty.c_str() << endl;
}
/* Se verifica que la variabla este declarada en el contexto y su alcance */
void check() {
if ( ident == 0 ) {
const char * c = valor.c_str();
sprintf(error_strp,"%s no ha sido declarado [LINEA: %d]", c, yylineno);
throw error_strp;}
}
/* Se agrega la variable a las tablas de simbolos correspondientes */
void add_variable(int tipo, bool doble){
variable * temp;
switch(tipo){
case NUMEROS:
temp = new variable_int(tipo);
break;
case BOOLEANOS:
temp = new variable_bool(tipo);
break;
case CHARACTERS:
temp = new variable_char(tipo);
break;
}
if (doble){
if (head_table->padre->mapa.count(valor) > 0) {
const char * c = valor.c_str();
sprintf(error_strp,"%s ya habia sido declarada antes. [LINEA: %d]", c, yylineno);
throw error_strp;
}
(head_table->padre->robots)[valor] = new Robot(tipo, head_table);
// Declaracion de simbolos
head_table->padre->mapa[valor] = tipo;
head_table->mapa["me"] = tipo;
// Declaracion de valores
head_table->valores["me"] = temp;
head_table->padre->valores[valor] = head_table->valores["me"];
} else {
// if (head_table->mapa.count(valor) > 0) {
// const char * c = valor.c_str();
// sprintf(error_strp,"%s ya habia sido declarada antes. [LINEA: %d]", c, yylineno);
// throw error_strp;
// }
head_table->valores[valor] = temp;
head_table->mapa[valor] = tipo;
}
ident = tipo;
tabla = head_table;
}
virtual int * get_value(){
tabla_simbolos * tmp = head_table;
while (tmp->mapa.count(valor) == 0){
tmp = head_table->padre;
}
if (working_bot != NULL){
return static_cast<variable_int * >(tmp->valores[valor])->valor;
}
if (tmp->valores[valor]->init && (tmp->robots)[valor]->activated){
return static_cast<variable_int * >(tmp->valores[valor])->valor;
} else {
sprintf(error_strp,"Error se esta utilizando la variable sin inicializar o activar el robot");
throw error_strp;
}
}
virtual bool * get_bool(){
tabla_simbolos * tmp = head_table;
while (tmp->mapa.count(valor) == 0){
tmp = head_table->padre;
}
if (tmp->valores[valor]->init && (tmp->padre->robots)[valor]->activated){
return static_cast<variable_bool * >(tmp->valores[valor])->valor;
} else {
sprintf(error_strp,"Error se esta utilizando la variable sin inicializar o activar el robot");
throw error_strp;
}
}
virtual char * get_character(){
tabla_simbolos * tmp = head_table;
while (tmp->mapa.count(valor) == 0){
tmp = head_table->padre;
}
if (tmp->valores[valor]->init && (tmp->padre->robots)[valor]->activated){
return static_cast<variable_char * >(tmp->valores[valor])->valor;
} else {
sprintf(error_strp,"Error se esta utilizando la variable sin inicializar o activar el robot");
throw error_strp;
}
}
virtual void activate(){
tabla_simbolos * tmp = head_table;
while(tmp->robots.count(valor) == 0){
tmp = head_table->padre;
}
(tmp->robots)[valor]->activate();
}
virtual void deactivate(){
tabla_simbolos * tmp = head_table;
while(tmp->robots.count(valor) == 0){
tmp = head_table->padre;
}
(tmp->robots)[valor]->deactivate();
}
virtual bool advance(){
tabla_simbolos * tmp = head_table;
while(tmp->robots.count(valor) == 0){
tmp = head_table->padre;
}
(tmp->robots)[valor]->advance();
return false;
}
virtual void add_comportamiento(ArbolSintactico * comp){
(head_table->padre->robots)[valor]->comportamientos = comp;
}
virtual void add_value(variable * value){
head_table->valores[valor]->init = true;
head_table->valores[valor] = value->clone();
}
};
/* Definicion de la clase que engloba las instrucciones del robot */
class intr_robot : public ArbolSintactico {
enum inst { T_ACTIVATE, T_DEACTIVATE, T_ADVANCE, T_COLLECT, T_READ, SEND};
public:
ArbolSintactico * declaraciones;
inst instruccion;
intr_robot(ArbolSintactico * l, int m) : declaraciones(l), instruccion(static_cast<inst>(m)) {}
intr_robot(int m) : instruccion(static_cast<inst>(m)) {}
virtual void imprimir(int i){
for (int j = 0; j < i; j++) cout << " ";
switch(instruccion){
case T_ACTIVATE:
cout << "ACTIVATE:" << endl;
break;
case T_DEACTIVATE:
cout << "DEACTIVATE:" << endl;
break;
case T_ADVANCE:
cout << "ADVANCE:" << endl;
break;
case T_COLLECT:
cout << "COLLECT:" << endl;
break;
case T_READ:
cout << "READ:" << endl;
break;
case SEND:
cout << "SEND." << endl;
break;
}
if (declaraciones != NULL){
declaraciones -> imprimir(i + 1);
}
}
virtual void ejecutar(){
switch(instruccion){
case T_ACTIVATE:
declaraciones->activate();
break;
case T_DEACTIVATE:
declaraciones->deactivate();
break;
case T_ADVANCE:
declaraciones->advance();
break;
case SEND:
if ( head_table->valores["me"]->init ) {
switch(head_table->valores["me"]->tipo){
case NUMEROS: {
int * numero = static_cast<variable_int * >(head_table->valores["me"])->valor;
cout << *numero;
break;
}
case CHARACTERS: {
char * character = static_cast<variable_char * >(head_table->valores["me"])->valor;
cout << *character;
break;
}
case BOOLEANOS: {
bool * valor = static_cast<variable_bool * >(head_table->valores["me"])->valor;
cout << * valor;
break;
}
}
} else {
sprintf(error_strp,"Error se esta utilizando la variable sin inicializar el bot. [LINEA: %d]", yylineno);
throw error_strp;
}
break;
case T_READ:{
switch(head_table->valores["me"]->tipo){
case NUMEROS: {
int leidos, valor;
char numero[11];
leidos = scanf("%s", numero);
string tmp(numero);
for(int i = 0; i < tmp.length(); i++){
if (!isdigit(numero[i]) && (i == 0) && !(numero[i] == '-')){
sprintf(error_strp,"Error de lectura en el terminal, se esperaba un entero.");
throw error_strp;
}
}
valor = atoi(numero);
if (declaraciones == NULL){
head_table->valores["me"]->init = true;
static_cast<variable_int * >(head_table->valores["me"])->valor = new int(valor);
} else {
variable_int * tmp = new variable_int(NUMEROS, new int(valor), true);
declaraciones->add_value(tmp);
}
break;
}
case CHARACTERS: {
char valor;
bool assigned = false;
char numero[11];
scanf("%s", numero);
string tmp(numero);
if (!tmp.compare("\\n") && !assigned){
assigned = true;
valor = '\n';
}
if (!tmp.compare("\\t") && !assigned){
assigned = true;
valor = '\t';
}
if (!tmp.compare("\\'") && !assigned){
assigned = true;
valor = '\'';
}
if (!assigned){
if(tmp.length() > 1){
sprintf(error_strp,"Error de lectura en el terminal, se esperaba un caracter");
throw error_strp;
}
valor = tmp[0];
}
if (declaraciones == NULL){
head_table->valores["me"]->init = true;
static_cast<variable_char * >(head_table->valores["me"])->valor = new char(valor);