-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaScript.java
1565 lines (1509 loc) · 63.2 KB
/
JavaScript.java
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JavaScript {
// PROCESSOR ATRIBUTES
private static AFD afd;
private static Character c; // Character that has been read
private static boolean errorStateLexSint = false;
private static boolean errorState = false;
private static int line = 1; // It must have at least 1 line
private static List<String> keywords;
private static Token sigToken;
private static String tokensStrings[] = {"$", "let", "int", "string", "boolean", "if",
"do", "while", "function", "return", "print", "input", "(", ")", "{","}",";",",",
"=","+=","*","!","<","cadena de caracteres","constante entera","identificador (nombre de una variable o función)"};
private static List<Object> tablaSimGlobal = new ArrayList<Object>();
private static List<Object> tablaSimLocal = new ArrayList<Object>();
private static Map<String,Integer> mapaTSG = new HashMap<String,Integer>();
private static Map<String,Integer> mapaTSL = new HashMap<String,Integer>();
private static List<Object> tablasLocal = new ArrayList<Object>();
private static List<Object> nombreLocales = new ArrayList<Object>();
private static boolean zonaDeclaracion = false;
private static int desplLocal = 0;
private static int desplGlobal = 0;
private static boolean tablaG = true;
private static int contadorFunciones = 1;
private static int funcionAct = 0;
private static String lexema = "";
private static boolean bucle = false;
private static boolean esConstEnt = false;
private static boolean tieneRetorno = false;
private static boolean dentroFun = false;
// Types
private static final String vacio = "vacio";
private static final String ok = "tipo_ok";
private static final String error = "tipo_error";
private static final String fun = "funcion";
private static final String logico = "logico";
private static final String ent = "entero";
private static final String cadena = "cadena";
// RD/WR FILE ATRIBUTES
private static Reader reader;
private static Writer writer;
// CONSTANTS
private static final int SUCCED = 0;
private static final int FAILURE = 1;
private static final int EOF = 65535;
public static Token LexicAnalizer(){
// Lexic analizer atributes
int actualState = 0; // Always starts at initial state 0
Character actionToDo = null; // List of actions that have to be done
Token token = null; // Token to be returned
boolean rdNext = true;
// Auxiliar atributes for token
MTpair pair = null; // Stores the corresponding pair
String cad = ""; // Stores a string token
while(actualState < 7 && !errorStateLexSint){
if (actualState == 5){
while(c != '\n' && c != EOF){
c = reader.read();
}
actualState = 0;
}
if(c == '\n'){
line++;
}
if(c == EOF){
return new Token(EOF,"");
}
pair = afd.getMTpair(actualState, c);
if(pair != null){
actualState = pair.getState();
actionToDo = pair.getAction();
}else{ // Error
switch(actualState){
case 0:
if(c == '"'){
GenError(16, "");
}else{
GenError(1, c.toString());
}
break;
case 4:
GenError(4, "");
break;
case 6:
//GenError(1, c.toString());
GenError(1, cad);
}
// Prepare the next character for the next call to lexic analizer
c = reader.read();
return null;
}
switch(actionToDo){
case 'A':
token = GenToken(12, " ", "parIzq");
break;
case 'B':
token = GenToken(13, " ", "parDrch");
break;
case 'C':
token = GenToken(14, " ", "llaveIzq");
break;
case 'D':
token = GenToken(15, " ", "llaveDrch");
break;
case 'E':
token = GenToken(16, " ", "puntoYcoma");
break;
case 'F':
token = GenToken(17, " ", "coma");
break;
case 'G':
token = GenToken(18, " ", "asign");
break;
case 'H':
token = GenToken(19, " ", "asignSuma");
break;
case 'I':
token = GenToken(20, " ", "mult");
break;
case 'J':
token = GenToken(21, " ", "neg");
break;
case 'K':
token = GenToken(22, " ", "menor");
break;
case 'L': // Read next char
break;
case 'M': // String
if(cad.length() > 64){
GenError(2, "" + cad.length());
}else{
token = GenToken(23, "\"" + cad + "\"", "cadena");
}
break;
case 'N': // Integer
if(Integer.parseInt(cad) > 32767){
GenError(3, "");
}else{
token = GenToken(24, cad, "constante entera");
}
rdNext = false;
break;
case 'O': // Identifiers
lexema = cad;
int index = searchKeyword(lexema);
if(index != -1){ // it is a keyword
token = GenToken(index, " ", "palabra reservada " + lexema);
}else{
boolean contenido = false;
if(zonaDeclaracion){
if(tablaG){
contenido = mapaTSG.containsKey(lexema);
}else{
contenido = mapaTSL.containsKey(lexema);
}
if(contenido){ // si ya esta contenido
int pos;
if(tablaG){
pos = mapaTSG.get(lexema);
}else{
pos = mapaTSL.get(lexema);
}
token = GenToken(25, pos, "identificador " + lexema);
GenError(5, lexema);
}else{ // no esta contenido
List <Object> atributos = new ArrayList<Object>();
atributos.add(0, lexema); // lexema
atributos.add(1, ""); // tipo
atributos.add(2, ""); // desplazamiento
int pos;
if(tablaG){
atributos.add(3, ""); // num parametros
List <Object> tipoParam = new ArrayList<Object>();
atributos.add(4, tipoParam); // tipo de parametros
atributos.add(5, ""); // Tipo devuelto
atributos.add(6, ""); // Etiqueta
pos = tablaSimGlobal.size();
mapaTSG.put(lexema, pos);
tablaSimGlobal.add(atributos);
}else{
pos = tablaSimLocal.size();
mapaTSL.put(lexema, pos);
tablaSimLocal.add(atributos);
}
token = GenToken(25, pos, "identificador " + lexema);
}
}else{
if(!mapaTSG.containsKey(lexema) && !mapaTSL.containsKey(lexema) ){
int pos = tablaSimGlobal.size();
token = GenToken(25, pos, "identificador " + lexema);
}else{
if(mapaTSG.containsKey(lexema)){
token = GenToken(25, mapaTSG.get(lexema), "identificador " + lexema);
}else{
token = GenToken(25, mapaTSL.get(lexema), "identificador " + lexema);
}
}
}
}
rdNext = false;
break;
case 'X': // Concatenation
cad = cad + c;
break;
default:
GenError(actualState, "leido");
break;
}
if(rdNext){
c = reader.read();
}
}
return token;
}
public static void SyntaticAnalizer(){
sigToken = LexicAnalizer();
P();
if(sigToken != null && sigToken.getID() != 65535){
GenError(8, "");
}
}
// PROCEDIMIENTOS
private static void equipara(int t){
if(errorStateLexSint){
try {
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Se han producido errores. Consulte el fichero 'errors.txt' para encontrar los detalles.");
System.exit(0);
}
if(sigToken != null){
if(sigToken.getID() == t){
sigToken = LexicAnalizer();
/*if(sigToken == null) { // Si me devuelve un error escribo y exit 1
try {
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
// System.exit(1);
}*/
}else{
GenError(6, tokensStrings[t]);
}
}
}
private static String E(){
writer.writeParse("1");
String tipoR = R();
String tipoE2 = E2();
if(tipoR.equals(ent) && tipoE2.equals(logico)){
return logico;
}else{
return tipoR;
}
}
@SuppressWarnings("unchecked")
private static String E2(){
if(sigToken != null){
int id = sigToken.getID();
if(id == 22){ // <
writer.writeParse("2");
int pos;
boolean ex;
String tipoId = "";
if(!esConstEnt){
if(dentroFun){
ex = mapaTSL.containsKey(lexema);
if(ex){ // local
pos = mapaTSL.get(lexema);
tipoId = (String)((ArrayList<Object>)(tablaSimLocal.get(pos))).get(1);
}else{
pos = mapaTSG.get(lexema);
tipoId = (String)((ArrayList<Object>)(tablaSimGlobal.get(pos))).get(1);
}
}else{
ex = mapaTSG.containsKey(lexema);
if(ex){
pos = mapaTSG.get(lexema);
tipoId = (String)((ArrayList<Object>)(tablaSimGlobal.get(pos))).get(1);
}
}
}else{
tipoId = ent;
}
if(!tipoId.equals(ent) && !tipoId.equals("")){
GenError(25, "");
equipara(22); // <
R();
E2();
return error;
}
else if(tipoId.equals("")){
equipara(22); // <
R();
E2();
return error;
}
equipara(22); // <
String tipoR = R();
String tipoE2 = E2();
if(tipoR.equals(ent) && (tipoE2.equals(ent) || tipoE2.equals(vacio))){
return logico;
}else{
//System.out.println("error");
GenError(25, ""); // ambos tipo entero
return error;
}
}else if(id == 13 || id == 16 || id == 17 ){ // ) ; ,
writer.writeParse("3");
return vacio;
}else{
GenError(7, tokensStrings[22] + "' | '" + tokensStrings[13] + "' | '" + tokensStrings[16] + "' | '" + tokensStrings[17]);
return error;
}
}
return ok;
}
private static String R(){
writer.writeParse("4");
String tipoU = U();
String tipoR2 = R2();
if(tipoR2.equals(ent) && tipoU.equals(ent)){
return ent;
}else{
return tipoU;
}
}
@SuppressWarnings("unchecked")
private static String R2(){
if(sigToken != null){
int id = sigToken.getID();
if(id == 20){ // *
writer.writeParse("5");
if(!esConstEnt){
String tipoId;
int pos;
boolean existe = mapaTSL.containsKey(lexema);
if(dentroFun){
if(existe){ // coge de local
pos = mapaTSL.get(lexema);
tipoId = (String)((ArrayList<Object>)(tablaSimLocal.get(pos))).get(1);
}else{ // coge de global
if(!mapaTSG.containsKey(lexema)){
GenError(9,lexema);
equipara(20); // *
U();
R2();
return error;
}
pos = mapaTSG.get(lexema);
tipoId = (String)((ArrayList<Object>)(tablaSimGlobal.get(pos))).get(1);
}
}else{
pos = mapaTSG.get(lexema);
tipoId = (String)((ArrayList<Object>)(tablaSimGlobal.get(pos))).get(1);
}
if(!tipoId.equals(ent)){
GenError(27, "");
equipara(20); // *
U();
R2();
return error;
}
}
equipara(20); // *
String tipoU = U();
String tipoR2 = R2();
if(tipoU.equals(ent) && (tipoR2.equals(ent) || tipoR2.equals(vacio))){
return ent;
}else{
GenError(27, "");
return error;
}
}else if(id == 22 || id == 13 || id == 16 || id == 17){ // < ) ; ,
writer.writeParse("6");
return vacio;
}else{
GenError(7, tokensStrings[20] + "'| '" + tokensStrings[22] + "' | '" + tokensStrings[13] + "' | '" + tokensStrings[16] + "' | '" + tokensStrings[17]);
return error;
}
}
return ok;
}
private static String U(){
if(sigToken != null){
int id = sigToken.getID();
if(id == 21){ // !
writer.writeParse("7");
equipara(21); // !
String tipoV = V();
if(tipoV.equals(logico)){
return logico;
}else{
GenError(26, "");
return error;
}
}else if(id == 25 || id == 12 || id == 24 || id == 23){ // id ( ent cad
writer.writeParse("8");
String tipoV = V();
return tipoV;
}else{
GenError(7, tokensStrings[21] + "' | '" + tokensStrings[25] + "' | '" + tokensStrings[12] + "' | '" + tokensStrings[24] + "' | '" + tokensStrings[23]);
return error;
}
}
return ok;
}
@SuppressWarnings("unchecked")
private static String V(){
if(sigToken != null){
int id = sigToken.getID();
if(id == 25){ // id
writer.writeParse("9");
esConstEnt = false;
//equipara(25); // id
int posEx = buscoIdTS(lexema); // 0 si existe, -1 si no
if(posEx == -1){
GenError(9, lexema);
equipara(25); // id
V2(posEx);
return error;
}
int pos = 0;;
String tipoId = "";
boolean existe;
if(dentroFun){
existe = mapaTSL.containsKey(lexema);
if(existe){ // esta en local
pos = mapaTSL.get(lexema);
tipoId = (String)((ArrayList<Object>)tablaSimLocal.get(pos)).get(1);
}else{
pos = mapaTSG.get(lexema);
tipoId = (String)((ArrayList<Object>)tablaSimGlobal.get(pos)).get(1);
}
}else{
existe = mapaTSG.containsKey(lexema);
if(existe){
pos = mapaTSG.get(lexema);
tipoId = (String)((ArrayList<Object>)tablaSimGlobal.get(pos)).get(1);
}
}
equipara(25); // id
String[] tipoV2 = V2(posEx).split(" ");
if(tipoId.equals(fun)){
List<String> tipoParametro;
String numeroParametros;
String tipoDevuelto;
tipoParametro = (ArrayList<String>)((ArrayList<Object>)tablaSimGlobal.get(pos)).get(4);
int numeroParametrosTabla = (Integer)((ArrayList<Object>)tablaSimGlobal.get(pos)).get(3);
numeroParametros = Integer.toString(numeroParametrosTabla);
tipoDevuelto = (String)((ArrayList<Object>)tablaSimGlobal.get(pos)).get(5);
boolean parametros = tipoV2[1].equals(numeroParametros);
if(!parametros){
GenError(28, numeroParametros);
return error;
}
int i = 2;
String tipoFalla = "";
String tipoDebe = "";
while(i < tipoV2.length && parametros ){
if(!(tipoV2[i].equals(tipoParametro.get(i-2)))){
parametros = false;
tipoFalla = tipoV2[i];
tipoDebe = tipoParametro.get(i-2);
}
i++;
}
if(parametros){
return tipoDevuelto;
}else{
GenError(11, tipoFalla + " " + tipoDebe);
return error;
}
}else{
return tipoId;
}
}else if(id == 12){ // (
writer.writeParse("10");
equipara(12); // (
String tipoE = E();
equipara(13); // )
return tipoE;
}else if(id == 24){ // ent**********
writer.writeParse("11");
esConstEnt = true;
equipara(24); // ent
return ent;
}else if(id == 23){ // cad
writer.writeParse("12");
equipara(23); // cad
return cadena;
}else{
GenError(7, tokensStrings[25] + "' | '" + tokensStrings[12] + "' | '" + tokensStrings[24] + "' | '" + tokensStrings[23]);
return error;
}
}
return ok;
}
@SuppressWarnings("unchecked")
private static String V2(int exId){
if(sigToken != null){
int id = sigToken.getID();
if(id == 12){ // (
writer.writeParse("13");
if(exId == -1){ // no declarada
equipara(12); // (
L();
equipara(13); // )
return error;
}
int pos = mapaTSG.get(lexema);
if(!(((ArrayList<Object>)tablaSimGlobal.get(pos)).get(1).equals(fun))){
GenError(17, lexema);
equipara(12); // (
L();
equipara(13); // )
return error;
}
equipara(12); // (
String tipoL = L();
equipara(13); // )
return tipoL;
}else if(id == 20 || id == 22 || id == 13 || id == 16 || id == 17){ // * < ) ; ,
writer.writeParse("14");
if(exId == -1){
return error;
}
return vacio + " 0";
}else{
GenError(7, tokensStrings[12] + "' | '" + tokensStrings[20] + "' | '" + tokensStrings[22] + "' | '" + tokensStrings[13] + "' | '" + tokensStrings[16] + "' | '" + tokensStrings[17]);
return error;
}
}
return ok;
}
@SuppressWarnings("unchecked")
private static String S(){
if(sigToken != null){
int id = sigToken.getID();
if(id == 25){ // id
writer.writeParse("15");
int pos = (Integer)(sigToken.getValue());
int existe = buscoIdTS(lexema);
equipara(25); // id
if(existe == -1){
ArrayList<Object> atribs = new ArrayList<Object>();
atribs.add(lexema); // Lexema
atribs.add(ent); // Tipo
atribs.add(desplGlobal); // Desplazamiento
desplGlobal++;
atribs.add(""); // Num param
atribs.add(new ArrayList<Object>()); // lista param
atribs.add(""); // tipo retorno
atribs.add(""); // etiqueta
tablaSimGlobal.add(pos,atribs);
pos = tablaSimGlobal.size() - 1;
mapaTSG.put(lexema, pos);
}
String tipo = "";
boolean posEx;
if(dentroFun){ // si esta dentro de una funcion
posEx = mapaTSL.containsKey(lexema);
if(posEx){ // esta en localo
pos = mapaTSL.get(lexema);
tipo = (String)((ArrayList<Object>)tablaSimLocal.get(pos)).get(1);
}else{
tipo = (String)((ArrayList<Object>)tablaSimGlobal.get(pos)).get(1);
}
}else{
posEx = mapaTSG.containsKey(lexema);
if(posEx){
tipo = (String)((ArrayList<Object>)tablaSimGlobal.get(pos)).get(1);
}
}
String tipoS2 = S2(tipo);
String[] tiposS2 = tipoS2.split(" ");
//System.out.println(tipo);
if(tipo.equals(fun) && !tiposS2[0].equals(error)){
String numeroParam;
ArrayList<String> tipoParam;
int numeroParametrosTabla = (Integer)((ArrayList<Object>)tablaSimGlobal.get(pos)).get(3);
numeroParam = Integer.toString(numeroParametrosTabla);
tipoParam = (ArrayList<String>)((ArrayList<Object>)tablaSimGlobal.get(pos)).get(4);
boolean coincidenParam = tiposS2[2].equals(numeroParam); // Se reciben el mismo numero de param que se requieren
if(!coincidenParam){
GenError(28, numeroParam);
return error;
}
String tipoFalla = "";
String tipoDebe = "";
for(int i = 3; i < tiposS2.length && coincidenParam; i++){
coincidenParam = tiposS2[i].equals(tipoParam.get(i-3)); // Los tipos son correctos incluido el orden
if(!coincidenParam){
tipoFalla = tiposS2[i];
tipoDebe = tipoParam.get(i-3);
}
}
if(coincidenParam){
return ok;
}else{
GenError(11, tipoFalla + " " + tipoDebe);
return error;
}
}else if(tiposS2[0].equals(tipo)){
return ok;
}else{
return error;
}
}else if(id == 10){ // print
writer.writeParse("16");
equipara(10); // print
String tipoE = E();
if(tipoE.equals(cadena) || tipoE.equals(ent)){
equipara(16); // ;
return ok;
}else{
GenError(12, "");
equipara(16); // ;
return error;
}
}else if(id == 11){ // input
writer.writeParse("17");
equipara(11); // input
if(sigToken.getValue().equals(" ")){ // no es id
GenError(6, "id");
equipara(25); // id
equipara(16); // ;
return error;
}
int pos = (int)sigToken.getValue();
int existe = buscoIdTS(lexema);
equipara(25); // id
if(existe == -1){
ArrayList<Object> atribs = new ArrayList<Object>();
atribs.add(lexema); // Lexema
atribs.add(ent); // Tipo
atribs.add(desplGlobal); // Desplazamiento
desplGlobal++;
atribs.add(""); // Num param
atribs.add(new ArrayList<Object>()); // lista param
atribs.add(""); // tipo retorno
atribs.add(""); // etiqueta
tablaSimGlobal.add(atribs);
pos = tablaSimGlobal.size() - 1;
mapaTSG.put(lexema, pos);
}
String tipo;
if(mapaTSG.containsKey(lexema)){
tipo = (String)((ArrayList<Object>)tablaSimGlobal.get(pos)).get(1);
}else{
tipo = (String)((ArrayList<Object>)tablaSimLocal.get(pos)).get(1);
}
if(tipo.equals(cadena) || tipo.equals(ent)){
equipara(16); // ;
return ok;
}else{
GenError(31, "");
equipara(16); // ;
return error;
}
}else if(id == 9){ // return
writer.writeParse("18");
equipara(9); // return
String tipoX = X();
if(!tablaG){
String tipoRetorno = (String)((ArrayList<Object>)tablaSimGlobal.get(funcionAct)).get(5);
if(!tipoRetorno.equals(vacio)){
tieneRetorno = true;
}
String tipos;
if(tipoRetorno.equals(tipoX)){
tipos = ok + " " + tipoRetorno;
}else{
tipos = tipoX + " " + tipoRetorno;
if(tipoRetorno.equals(vacio)){
GenError(22, "");
}else{
if(!tipoX.equals(error)){
GenError(20, tipos);
}
}
equipara(16); // ;
return error + " " + error;
}
equipara(16); // ;
return tipos;
}else{
equipara(16); // ;
return error + " " + error;
}
}else{
GenError(7, tokensStrings[25] + "' | '" + tokensStrings[10] + "' | '" + tokensStrings[11] + "' | '" + tokensStrings[9]);
return error;
}
}
return ok;
}
@SuppressWarnings("unchecked")
private static String S2(String tipoVar){
if(sigToken != null){
int id = sigToken.getID();
if(id == 18){ // =
writer.writeParse("19");
if(tipoVar.equals(fun)){
GenError(32, "");
equipara(18); // =
E();
equipara(16); // ;
return error;
}
equipara(18); // =
String tipoE = E();
if(!tipoE.equals(tipoVar)){
GenError(23, tipoVar);
equipara(16); // ;
return error;
}
equipara(16); // ;
return tipoE;
}else if(id == 12){ // (
writer.writeParse("20");
int pos = mapaTSG.get(lexema);
String tipoId = (String)((ArrayList<Object>)(tablaSimGlobal.get(pos))).get(1);
if(!tipoId.equals(fun)){
GenError(9, lexema);
equipara(12); // (
L();
equipara(13); // )
equipara(16); // ;
return error;
}
equipara(12); // (
String tipoL = L();
equipara(13); // )
equipara(16); // ;
return fun + " " + tipoL;
}else if(id == 19){ // +=
writer.writeParse("21");
if(tipoVar.equals(fun)){
GenError(32, "");
equipara(19); // +=
E();
equipara(16); // ;
return error;
}
int pos = mapaTSG.get(lexema);
String tipoId = (String)((ArrayList<Object>)(tablaSimGlobal.get(pos))).get(1);
if(!tipoId.equals(ent)){
GenError(24, "");
equipara(19); // +=
E();
equipara(16); // ;
return error;
}
equipara(19); // +=
String tipoE = E();
if(!tipoE.equals(ent)){
GenError(24, "");
equipara(16); // ;
return error;
}
equipara(16); // ;
return tipoE;
}else{
GenError(7, tokensStrings[18] + "' | '" + tokensStrings[12] + "' | '" + tokensStrings[19]);
return error;
}
}
return ok;
}
private static String L(){
if(sigToken != null){
int id = sigToken.getID();
if(id == 21 || id == 25 || id == 12 || id == 24 || id == 23){ // ! id ( ent lexema
writer.writeParse("22");
String tipoE = E();
String[] devueltoQ = Q().split(" ");
if(devueltoQ[0].equals(vacio)){
return "tipo_ok 1 "+ tipoE;
}else{
devueltoQ[1] = "" + (Integer.parseInt(devueltoQ[1]) + 1);
return String.join(" ", devueltoQ) + " " + tipoE;
}
}
else if(id == 13){ // )
writer.writeParse("23");
return vacio + " 0";
}else{
GenError(7, tokensStrings[12]+"' | '"+tokensStrings[13]+"' | '" + tokensStrings[21]+"' | '"+tokensStrings[23]+"' | '"+ tokensStrings[24]+"' | '"+tokensStrings[25]);
return error;
}
}
return ok;
}
private static String Q(){
if(sigToken != null){
int id = sigToken.getID();
if(id == 17){ //Coma
writer.writeParse("24");
equipara(17); // Coma
String tipoE = E();
String[] devueltoQ = Q().split(" ");
if(devueltoQ[0].equals(vacio)){
return "tipo_ok 1 " + tipoE;
}else{
devueltoQ[1] = "" + (Integer.parseInt(devueltoQ[1]) + 1);
return String.join(" ", devueltoQ) + " " + tipoE;
}
}else if(id == 13){ // )
writer.writeParse("25");
return vacio;
}else{
GenError(7, tokensStrings[17]+"' | '"+tokensStrings[13]);
return error;
}
}
return ok;
}
private static String X(){
if(sigToken != null){
int id = sigToken.getID();
if(id == 21 || id == 25 || id == 12 || id == 23 || id == 24){ /// ! id ( ent lexema
writer.writeParse("26");
String tipoE = E();
return tipoE;
}else if(id == 16){ //Punto y coma
writer.writeParse("27");
return vacio;
}else{
GenError(0, tokensStrings[21]+"' | '"+tokensStrings[25]+"' | '" + tokensStrings[12]+"' | '"+tokensStrings[23]+"' | '"+ tokensStrings[24]+"' | '"+tokensStrings[16]);
return error;
}
}
return ok;
}
private static String B(){
if(sigToken != null){
int id = sigToken.getID();
if(id == 5){ // if
writer.writeParse("28");
equipara(5); // if
equipara(12); // parIzzq
String tipoE = E();
if(!tipoE.equals(logico)){
GenError(10, "");
equipara(13); // parDer
S();
return error;
}else{
equipara(13); // parDer
String tipoS = S();
return tipoS.split(" ")[0]; // devuelve tipo
}
}else if(id == 1){ // let
writer.writeParse("29");
zonaDeclaracion = true;
equipara(1); // let
if(!(sigToken.getID()==25)){
GenError(29,"");
equipara(25); // id
T();
equipara(16); // punto y coma
return error;
}
int pos = (Integer)sigToken.getValue(); // posicion en tabla de simbolos
equipara(25); // id
zonaDeclaracion = false;
String[] tipoT = T().split(" ");
if(tablaG){
insertarTipoTS(pos, tipoT[0], tablaSimGlobal);
insertarDespTS(pos, desplGlobal, tablaSimGlobal);
desplGlobal = desplGlobal + Integer.parseInt(tipoT[1]);
}else{
insertarTipoTS(pos, tipoT[0], tablaSimLocal);
insertarDespTS(pos, desplLocal, tablaSimLocal);
desplLocal = desplLocal + Integer.parseInt(tipoT[1]);
}
equipara(16); // punto y coma
return ok;
}else if(id == 25 || id == 9 || id == 10 || id == 11){ // id print input return
writer.writeParse("30");
String tipoS = S();
return tipoS;
}else if(id == 6){// do
writer.writeParse("31");
equipara(6); // do
equipara(14); // llaveizq
if(sigToken != null){ // ----------------------------------------
id = sigToken.getID();
bucle = true;
String tipoC = C();
bucle = false;
equipara(15); // llaveDer
equipara(7); // while
equipara(12); // parIzq
String tipoE = E();;
if(!tipoE.equals(logico)){
GenError(10, "");
equipara(13); // parDer
equipara(16); // ;
return error;
}else{
equipara(13); // parDer
equipara(16);
return tipoC;
}
}
}else{
GenError(7, tokensStrings[5]+"' | '"+tokensStrings[1]+"' | '" + tokensStrings[25]+"' | '"+tokensStrings[9]+"' | '"+ tokensStrings[10]+"' | '"+tokensStrings[11]+"' | '"+tokensStrings[6]);
return error;
}
}
return ok;
}
private static String T(){
if(sigToken != null){
int id = sigToken.getID();
if(id == 2){ // int
writer.writeParse("32");
equipara(2); // int
String tipos = ent + " 1";
return tipos;
}
else if(id == 4){ // boolean
writer.writeParse("33");
equipara(4); // boolean
String tipos = logico + " 1";
return tipos;
}
else if(id == 3){ // string
writer.writeParse("34");
equipara(3); // string
String tipos = cadena + " 64";
return tipos;
}
else{
GenError(7, tokensStrings[2]+"' | '"+tokensStrings[4]+"' | '" + tokensStrings[3]);
String tipos = error + " 0";
return tipos;
}
}
return ok;
}
@SuppressWarnings("unchecked")
private static String F(){
if(sigToken != null){
int id = sigToken.getID();
if(id == 8){ // function
writer.writeParse("35");
dentroFun = true;
zonaDeclaracion = true;
equipara(8); // function
if(sigToken.getValue().equals( " ")){ // No es identificador
GenError(13, "");
equipara(25); // id
H();
equipara(12); // (
A();
equipara(13); // )
zonaDeclaracion = false;
equipara(14); // {
C();
equipara(15); // }