-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcup.cup
More file actions
811 lines (744 loc) · 21.6 KB
/
cup.cup
File metadata and controls
811 lines (744 loc) · 21.6 KB
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
import java_cup.runtime.*;
import java.io.*;
import java.util.*;
action code
{:
Hashtable st,localST,politipos;
int politipoActual=0;
char charPolitipo = 'a';
private void imprimirPrompt(){
System.out.print("MLOR> ");
}
private String nombrePolitipo(Attrs p){ //entro solo con politipo, y devuevlo 'a, 'b... eso
String aux = (String)politipos.get(p.getPolytype());
if (aux!=null){
return aux;
}
else{
if (p.getType().equals("politipo numerico")){
p.setType("int");
return "int";
}
else if (p.getType().equals("politipo con igualdad")){
politipos.put(p.getPolytype(),"''" + charPolitipo);
charPolitipo++;
return (String)politipos.get(p.getPolytype());
}
else{
politipos.put(p.getPolytype(),"'" + charPolitipo);
charPolitipo++;
return (String)politipos.get(p.getPolytype());
}
}
}
//lo cambie para que devuelva el string, porque va a ser mas facil cuando apliquemos recursivamente
private String obtenerStringTipo(Attrs tipo){
ArrayList<Attrs> list;
String aux;
//System.out.println("entre a obtenerStringTipo" + tipo);
if (tipo.getType().equals("list")){
// System.out.println("estoy en lista");
if (tipo.getListType().getType().equals("fun"))
aux = "(" + obtenerStringTipo(tipo.getListType()) + ")";
else
aux = obtenerStringTipo(tipo.getListType());
// System.out.println(aux);
return aux+" list";
}
else if (tipo.getType().equals("tuple")){
//System.out.println("estoy en tupla");
list = tipo.getTupleTypes();
aux = "(";
for (int i=0;i<list.size();i++){
if (list.get(i).getType().equals("fun"))
aux += "(" + obtenerStringTipo(list.get(i)) + ")";
else
aux += obtenerStringTipo(list.get(i));
//System.out.println(aux);
//aux+=obtenerStringTipo(list.get(i));
if (i!=list.size()-1){
aux+=" * ";
}
}
aux+=")";
// System.out.println(aux);
return aux;
}
else if (tipo.getType().equals("fun")){
//System.out.println("estoy en fun");
if (tipo.getDomain().getType().equals("fun"))
aux = "(" + obtenerStringTipo(tipo.getDomain()) + ")";
else
aux = obtenerStringTipo(tipo.getDomain());
//System.out.println(aux);
return aux + " -> " + obtenerStringTipo(tipo.getRange());
}
else if (tipo.getType().startsWith("politipo")){ //si es cualquier politipo
return nombrePolitipo(tipo);
}
else{
//System.out.println("estoy en tipo simple");
return tipo.getType();
}
}
private boolean esTipoSimple(Attrs type1){
String t1 = type1.getType();
return (t1.equals("int") || t1.equals("real") || t1.equals("boolean") || t1.equals("string"));
}
private boolean esTipoTextOrNum(Attrs type1){
String t1 = type1.getType();
return (t1.equals("int") || t1.equals("real") || t1.equals("string"));
}
private void imprimirTipo(Attrs tipo){
System.out.print(obtenerStringTipo(tipo));
}
private Attrs obtenerTipo(String id) throws Exception{
Attrs t;
if (localST != null){
t = (Attrs) localST.get(id);
if(t != null)
return (t);
}
t = (Attrs) st.get(id);
if(t != null){
return t;
}else
throw new Exception("Undeclared identifier (" + id +")");
}
private boolean putLocalID(String id) throws Exception{
if (localST.get(id)==null){
Attrs aux =crearPolitipo("politipo");
localST.put(id,aux);
//System.out.println("insertando " + id + " a la tabla local con " + aux.getType());
return true;
}
else{
throw new Exception("Multiple declaration of value "+ id);
}
}
private Attrs crearPolitipo(String poli){
Attrs aux = new Attrs(poli);
aux.setPolytype(politipoActual);
politipoActual++;
return aux;
}
private void clonarTipos(Attrs type1,Attrs type2){ //type2 es el mas especifico
Object[] aux;
type2.mergeInvolvedIDs(type1.getInvolvedIDs());
aux = type2.getInvolvedIDs().toArray();
if (localST!=null){ //cambio la referencia cuando estoy en ambiente de fun
for (int i=0;i<aux.length;i++){
cambiarReferencia((String)aux[i],type2);
}
}
}
private void corregirTupla(Attrs tupla){ //corrige las referencias de la tupla para que apunten a los lugares nuevos, en el caso que alguna variable haya cambiado de tipo
ArrayList<Attrs> auxList = new ArrayList<Attrs>();
ArrayList<Attrs> oldList = tupla.getTupleTypes();
Attrs aux;
Object[] ids;
String s;
if (localST!=null){
for (int i = 0;i< tupla.getTupleLenght();i++){
ids = oldList.get(i).getInvolvedIDs().toArray();
if (ids.length>0){
s = (String)ids[0];
if (Character.isLetter(s.charAt(0))){
aux = (Attrs)localST.get((String)ids[0]);
if (aux!=null)
auxList.add(aux);
else
auxList.add(oldList.get(i));
}
else
auxList.add(oldList.get(i));
}
else
auxList.add(oldList.get(i));
}
tupla.setTupleTypes(auxList);
}
}
private void corregirPolitiposNum(){
// Object[] entries = localST.entrySet().toArray();
Attrs aux;
for (Enumeration e = localST.elements();e.hasMoreElements() ;){
aux = ((Attrs)e.nextElement());
if (aux.getType().equals("politipo numerico")){
aux.setType("int");
}
}
}
private void cambiarReferencia(String s,Attrs t){
int posPar,pos;
if (Character.isLetter(s.charAt(0)))
localST.put(s,t);
else{
if (s.charAt(0)=='>'){
// System.out.println("se cambia el rango de una funcion");
cambiarReferencia2(s.substring(1)).setRange(t);
}
else if (s.charAt(0)=='-'){
cambiarReferencia2(s.substring(1)).setDomain(t);
}
else if (s.charAt(0)=='['){
cambiarReferencia2(s.substring(1)).setListType(t);
}
else if (s.charAt(0)=='('){
posPar = s.indexOf(')');
pos = Integer.parseInt(s.substring(1,posPar));
t.getTupleTypes().remove(pos);
cambiarReferencia2(s.substring(posPar+1)).getTupleTypes().add(pos,t);
}
}
}
private Attrs cambiarReferencia2(String s){
int posPar,pos;
Attrs t;
if (s.charAt(0)=='>')
return cambiarReferencia2(s.substring(1)).getRange();
else if (s.charAt(0)=='-'){
return cambiarReferencia2(s.substring(1)).getDomain();
}
else if (s.charAt(0)=='['){
return cambiarReferencia2(s.substring(1)).getListType();
}
else if (s.charAt(0)=='('){
posPar = s.indexOf(')');
pos = Integer.parseInt(s.substring(1,posPar));
t = cambiarReferencia2(s.substring(posPar+1));
return t.getTupleTypes().get(pos);
}
else
return (Attrs)localST.get(s); //se llego al nivel de la tabla local
}
private void apuntarVariables(Attrs t, String prefix, HashSet<String> set){
Object[] aux = set.toArray();
// System.out.println("entro a apuntar Variables");
for (int i=0;i<aux.length;i++){
t.agregarID(prefix+aux[i]);
// System.out.println("se apunto " + prefix+aux[i]);
}
}
private void apuntarTipos(Attrs type1,Attrs type2){
String t2 = type2.getType();
ArrayList<Attrs> list;
if (t2.equals("fun")){
apuntarVariables(type2.getRange(),">",type1.getInvolvedIDs());
apuntarVariables(type2.getDomain(),"-",type1.getInvolvedIDs());
}
else if (t2.equals("list")){
apuntarVariables(type2.getListType(),"[",type1.getInvolvedIDs());
}
else if (t2.equals("tuple")){
list = type2.getTupleTypes();
// System.out.println("entre a apuntar tipos tupla " +list.size());
for (int i = 0; i<list.size();i++)
apuntarVariables(list.get(i),"("+i+")",type1.getInvolvedIDs());
}
}
//devuelve el tipo más específico, y CAMBIA los valores de los tipos para que sean iguales.
//de no haber compatibilidad, devuelve null
private Attrs tipoMasEspecifico(Attrs type1, Attrs type2){
String t1,t2;
ArrayList listT1,listT2,result;
Attrs tResult;
t1 = type1.getType();
t2 = type2.getType();
// System.out.println("entra veron con " + t1 +" y "+ t2);
if ((esTipoSimple(type1)) && (esTipoSimple(type2)) && t1.equals(t2)){
//si ambos son tipos simples o iguales, todo OK
// System.out.println("entro a tipo simple ");
return type1;
}
else{
if (t1.equals(t2)){ //los tipo de t1 y 2 son iguales, pero no son tipos basicos
if (t1.equals("tuple")){
//ambos son tuplas
if (type1.getTupleLenght()==type2.getTupleLenght()){
//ambos son tuplas de la misma longitud
listT1 = type1.getTupleTypes();
listT2 = type2.getTupleTypes();
result = new ArrayList();
for (int i=0;i<listT1.size();i++){
tResult = tipoMasEspecifico((Attrs) listT1.get(i),(Attrs) listT2.get(i));
if (tResult!=null)
result.add(tResult);
else
return null;
}
type2.setTupleTypes(result);
type1.setTupleTypes(result);
return type1;
}
//ambas son tuplas, pero las longitudes no coindicen
return null;
}
else if (t1.equals("list")){
//ambas son listas
tResult = tipoMasEspecifico(type1.getListType(),type2.getListType());
if (tResult!=null){
type1.setListType(tResult);
type2.setListType(tResult);
return type1;
}
else
return null;
}
//ambos son el mismo tipo de politipo
else if (t1.equals("politipo") || t1.equals("politipo con igualdad") || t1.equals("politipo numerico")){
clonarTipos(type2,type1);
return type1;
}
else if (t1.equals("fun")){
type1.setDomain(tipoMasEspecifico(type1.getDomain(),type2.getDomain()));
type1.setRange(tipoMasEspecifico(type1.getRange(),type2.getRange()));
clonarTipos(type2,type1);
return type1;
}
else
return null;
}else{ //los tipos de t1 y 2 son distintos
if (t1.equals("politipo") || t1.equals("politipo con igualdad")){
//si uno de los tipos es un politipo, lo clonamos al otro
/////////////////////////////
if (localST!=null) //solo apunto si estoy en ambiente de fun
apuntarTipos(type1,type2);
////////////////////////////
clonarTipos(type1,type2); //puede ser necesario borrar las listas
return type2;
}
else if (t2.equals("politipo") || t2.equals("politipo con igualdad")){
//si uno de los tipos es un politipo, lo clonamos al otro
/////////////////////////////
if (localST!=null) //solo apunto si estoy en ambiente de fun
apuntarTipos(type2,type1);
////////////////////////////
clonarTipos(type2,type1); //puede ser necesario borrar las listas
return type1;
}
else if (t1.equals("politipo numerico" )){
//si uno es un politipo numerico, y el otro es int o real, asignamos el politipo
if (t2.equals("int") || t2.equals("real")){
clonarTipos(type1,type2); //puede ser necesario borrar las listas
return type2;
}
else
return null;
}
else if (t2.equals("politipo numerico" )){
//si uno es un politipo numerico, y el otro es int o real, asignamos el politipo
if (t1.equals("int") || t1.equals("real")){
clonarTipos(type2,type1);
return type1; //puede ser necesario borrar las listas
}
else
return null;
}
else // si no es ningun politipo, y son distintos, se rompio
return null;
}
}
}
:}
parser code
{:
public static void main(String args[]) throws Exception //le puse esto para que dejara de decir "null" cuando hay un pete y que tire un error mas copado
{
InputStream in = null;
// imprimirPrompt();
System.out.print("MLOR> ");
try{
if (args.length==1){
in = new FileInputStream(args[0]);
// archivo = true;
}
else
in = System.in;
}
catch (FileNotFoundException e){
System.err.println("File not found");
System.exit(1);
}
try
{
new parser(new Yylex(in)).parse();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
:}
terminal Token PUNTO, PTOCOMA, IGUAL, COMA, OPREL, CONS, OPADD, ORELSE, CONCAT, MUL, DIVREAL, DIVINT, MOD, ANDALSO, NOT, MINUS, PARABRE, PARCIERRA, CORABRE, CORCIERRA, NIL, FUN, VAL, ID, INT, REAL, STRING, DISTINTO;
non terminal Programa, Lista_sentencias;
non terminal Attrs Sentencia, MatchingFunc, CurryingList, TuplePattern, TuplePattern2, Exp_Rel, Exp_cons, Exp_add, Exp_mul, Exp_un, Exp_fun, Exp_atom, Tuple, Tuple2, List, List2;
Programa ::= {:
st=new Hashtable();
politipos = new Hashtable();
Attrs b = new Attrs ("boolean");
st.put("true",b);
st.put("false",b);
:}
Lista_sentencias
{:
System.out.println("Succesfully parsed! Press CTRL+C to exit.");
:}
PUNTO
;
Lista_sentencias ::= Lista_sentencias
Sentencia
{:imprimirPrompt();:}
PTOCOMA
|
Sentencia
{:imprimirPrompt();:}
PTOCOMA
;
Sentencia ::= VAL
ID:id
IGUAL
Exp_Rel:exp
{:
st.put(id.getLexeme(),exp);
System.out.print("val "+id.getLexeme()+": ");
imprimirTipo(exp);
System.out.println("");
// System.out.print("MLOR> ");
charPolitipo = 'a'; //reinicio el contador de politipos
:}
|
Exp_Rel:exp
{: st.put("it",exp);
System.out.print("val it: ");
imprimirTipo(exp);
System.out.println("");
charPolitipo = 'a'; //reinicio el contador de politipos
:}
|
FUN
{:
localST = new Hashtable();
// System.out.print("MLOR> ");
:}
MatchingFunc
{:
localST = null;
// System.out.print("MLOR> ");
:}
;
MatchingFunc ::= ID:f ID:param {:putLocalID(param.getLexeme());:} //pone el parametro con un politipo
CurryingList:list
{:Attrs aux = new Attrs("fun",(Attrs)localST.get(param.getLexeme()), list);
corregirPolitiposNum();
st.put(f.getLexeme(),aux);
System.out.print("val "+ f.getLexeme()+": ");
// politipos = new Hashtable();
imprimirTipo(aux);
politipos = new Hashtable();
charPolitipo = 'a'; //reinicio el contador de politipos
System.out.println(" = fn;");
:}
|
ID:f
TuplePattern:tuple
IGUAL
Exp_Rel:exp
{:
corregirTupla(tuple);
corregirPolitiposNum();
Attrs aux = new Attrs("fun",tuple,exp);
st.put(f.getLexeme(), aux);
System.out.print("val "+ f.getLexeme()+": ");
imprimirTipo(aux);
politipos = new Hashtable();
charPolitipo = 'a'; //reinicio el contador de politipos
System.out.println(" = fn;");
:};
CurryingList ::= ID:id {:putLocalID(id.getLexeme());:}
CurryingList:list {:RESULT = new Attrs("fun",(Attrs)localST.get(id.getLexeme()),list);:}
| IGUAL Exp_Rel:exp {:RESULT = exp;:};
TuplePattern ::= PARABRE TuplePattern2:tp PARCIERRA{:RESULT = tp; :};
TuplePattern2 ::= TuplePattern2:t COMA ID:id
{:
putLocalID(id.getLexeme());
Attrs aux = crearPolitipo("politipo");
aux.agregarID(id.getLexeme());
t.addTupleTypeFinal(aux);
RESULT = t;
:}
|
ID:id
{:
putLocalID(id.getLexeme());
Attrs t = new Attrs("tuple", new ArrayList());
Attrs aux = crearPolitipo("politipo");
aux.agregarID(id.getLexeme());
t.addTupleTypeFinal(aux);
RESULT = t;
:};
Exp_Rel ::= Exp_Rel:e1 OPREL Exp_cons:e2
{:
Attrs t = tipoMasEspecifico(e1,e2);
if (t!=null){
if (esTipoTextOrNum(t)){
RESULT = new Attrs("boolean");
}else{
throw new Exception("String or num expected.");
}
}else{
throw new Exception("String or num expected.");
}
:}
|
Exp_Rel:e1 DISTINTO Exp_cons:e2
{:
Attrs t = tipoMasEspecifico(e1,e2);
if (t!=null){
t = tipoMasEspecifico(t,crearPolitipo("politipo con igualdad"));
if (t!=null && !t.getType().equals("fun")){
RESULT = new Attrs("boolean");
}else{
throw new Exception("Function does not admit equality.");
}
}else{
throw new Exception("Types are not compatibles.");
}
:}
|
Exp_Rel:e1 IGUAL Exp_cons:e2
{:
Attrs t = tipoMasEspecifico(e1,e2);
if (t!=null){
t = tipoMasEspecifico(t,crearPolitipo("politipo con igualdad"));
if (t!=null && !t.getType().equals("fun")){
RESULT= new Attrs("boolean");
}
else{
throw new Exception("Function does not admit equality.");
}
}else{
throw new Exception("Types are not compatibles.");
}
:}
|
Exp_cons:e {: RESULT = e; :};
Exp_cons ::= Exp_add:e1 CONS Exp_cons:e2
{:
Attrs t = tipoMasEspecifico(new Attrs("list",e1),e2);
if (t!=null){
RESULT = t;
}
else{
throw new Exception("The list has elements of different types.");
}
:}
|
Exp_add:e {: RESULT = e; :};
Exp_add ::= Exp_add:e1 OPADD Exp_mul:e2
{:
Attrs t = tipoMasEspecifico(e1,e2);
if (t!=null){
t = tipoMasEspecifico(t,crearPolitipo("politipo numerico"));
if (t!=null){
RESULT = t;
}else{
throw new Exception("Integer or real expected.");//lo cambio... estaba en "Integer or real expected."
}
}else{
throw new Exception("Incompatible types.");
}
:}
|
Exp_add:e1 ORELSE Exp_mul:e2
{:
Attrs t = tipoMasEspecifico(e1,e2);
if (t!=null){
t = tipoMasEspecifico(t,new Attrs ("boolean"));
if (t!=null){
RESULT = t;
}else{
throw new Exception("Boolean expected.");
}
}else{
throw new Exception("Boolean expected.");
}
:}
|
Exp_add:e1 CONCAT Exp_mul:e2
{:
Attrs t = tipoMasEspecifico(e1,e2);
if (t!=null){
t = tipoMasEspecifico(t,new Attrs ("string"));
if (t!=null){
RESULT = t;
}else{
throw new Exception("String expected.");
}
}else{
throw new Exception("String expected.");
}
:}
|
Exp_mul:e {: RESULT = e; :};
Exp_mul ::= Exp_mul:e1 MUL Exp_un:e2
{:
Attrs t = tipoMasEspecifico(e1,e2);
if (t!=null){
t = tipoMasEspecifico(t,crearPolitipo("politipo numerico"));
if (t!=null){
RESULT = t;
}else{
throw new Exception("Integer or real expected.");
}
}else{
throw new Exception("Incompatible types.");
}
:}
|
Exp_mul:e1 DIVREAL Exp_un:e2
{:
Attrs t = tipoMasEspecifico(e1,e2);
if (t!=null){
t = tipoMasEspecifico(t,new Attrs("real"));
if (t!=null){
RESULT = t;
}else{
throw new Exception("Real expected.");
}
}else{
throw new Exception("Real expected.");
}
:}
|
Exp_mul:e1 DIVINT Exp_un:e2
{:
Attrs t = tipoMasEspecifico(e1,e2);
if (t!=null){
t = tipoMasEspecifico(t,new Attrs("int"));
if (t!=null){
RESULT = t;
}else{
throw new Exception("Integer expected.");
}
}else{
throw new Exception("Integer expected.");
}
:}
|
Exp_mul:e1 MOD Exp_un:e2
{:
Attrs t = tipoMasEspecifico(e1,e2);
if (t!=null){
t = tipoMasEspecifico(t,new Attrs("int"));
if (t!=null){
RESULT = t;
}else{
throw new Exception("Integer expected.");
}
}else{
throw new Exception("Integer expected.");
}
:}
|
Exp_mul:e1 ANDALSO Exp_un:e2
{:
Attrs t = tipoMasEspecifico(e1,e2);
if (t!=null){
t = tipoMasEspecifico(t,new Attrs("boolean"));
if (t!=null){
RESULT = t;
}else{
throw new Exception("Boolean expected.");
}
}else{
throw new Exception("Boolean expected.");
}
:}
|
Exp_un:e {: RESULT = e; :};
Exp_un ::= NOT Exp_fun:fun
{: Attrs t = tipoMasEspecifico(fun, new Attrs("boolean"));
if (t!=null){
RESULT = t;
}else{
throw new Exception("Boolean expected.");
}
:}
|
MINUS Exp_fun:fun
{:
Attrs t = tipoMasEspecifico(fun, new Attrs("politipo numerico"));
if (t != null){
RESULT = t;
}else{
throw new Exception("~ operator can only be applied to integer and real values.");//lo cambie! era "Integer or real expected"
}
:}
|
Exp_fun:e {: RESULT = e; :};
Exp_fun ::= Exp_fun:f Exp_atom:atom
{:
Attrs aux = tipoMasEspecifico(f,new Attrs("fun",atom,crearPolitipo("politipo")));
if (aux!=null){
RESULT = aux.getRange();
}
else{
throw new Exception("Not a valid function.");
}
:}
|
Exp_atom:atom {: RESULT = atom; :};
Exp_atom ::= INT {: RESULT = new Attrs("int"); :}
|
REAL {: RESULT = new Attrs("real") ;:}
|
STRING {: RESULT = new Attrs("string") ;:}
|
ID:id {:Attrs aux = obtenerTipo(id.getLexeme());
if (aux!=null){
if (!esTipoSimple(aux)){ //agrego a la lista de ids involucrados cualquier cosa que no tenga su tipo definido
aux.agregarID(id.getLexeme());
}
RESULT = aux;
}
else
throw new Exception("Undeclared identifier (" + id.getLexeme() +")");
:}
|
NIL {: RESULT = new Attrs("list",crearPolitipo("politipo")) ;:}
|
PARABRE
Exp_Rel:e {:// e.deleteInvolvedIDs(); // se supone que los tipos de los id estan resueltos
RESULT = e; :}
PARCIERRA
|
Tuple:t {:RESULT = t; :}
|
List:l {:RESULT = l; :};
Tuple ::= PARABRE Exp_Rel:e COMA Tuple2:t2 PARCIERRA {:
t2.addTupleTypeBegin(e);
corregirTupla(t2);
RESULT = t2;
:};
Tuple2 ::= Tuple2:t COMA Exp_Rel:e
{:
t.addTupleTypeFinal(e);
RESULT = t;
:}
|
Exp_Rel:e {:
Attrs t = new Attrs("tuple", new ArrayList());
t.addTupleTypeFinal(e);
RESULT = t;
:};
List ::= CORABRE List2:l CORCIERRA {:RESULT = l;:};
List2 ::= List2:list COMA Exp_Rel:exp {:
Attrs t = tipoMasEspecifico(list.getListType(),exp);
if (t!=null)
RESULT = tipoMasEspecifico(list,new Attrs("list",t));
else
throw new Exception("The list has elements of different types.");
:}
|
Exp_Rel:exp {:RESULT = new Attrs("list",exp);:};