-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysecondvisitor.java
1041 lines (909 loc) · 28.7 KB
/
mysecondvisitor.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 minipython.analysis.*;
import minipython.node.*;
import java.util.*;
public class mysecondvisitor extends DepthFirstAdapter
{
private Hashtable symtable;
private List<AFunction> functions;
mysecondvisitor(Hashtable symtable, List<AFunction> functions)
{
this.symtable = symtable;
this.functions = functions;
}
public void inAEqualsStatement(AEqualsStatement node)
{
TId lId = ((TId)((AIdExpression) node.getL()).getId());
String fName = lId.toString();
if(node.getR() instanceof AIdExpression)
{
if(symtable.containsKey(((TId)((AIdExpression) node.getR()).getId()).toString()))
{
if(symtable.get(((TId)((AIdExpression) node.getR()).getId()).toString()) instanceof AEqualsStatement)
{
AEqualsStatement old = (AEqualsStatement)symtable.get(((TId)((AIdExpression) node.getR()).getId()).toString());
PExpression equals = old.getR();
AEqualsStatement update = new AEqualsStatement((AIdExpression) node.getL(), equals);
symtable.put(fName, update);
}
}
}
else if(node.getR() instanceof ATypeExpression)
{
TId id = ((AIdExpression)((ATypeExpression)node.getR()).getExpression()).getId();
String typeName = id.toString();
if(symtable.containsKey(typeName))
{
symtable.put(fName, node.getR());
}
}
else
{
symtable.put(fName, node);
}
}
public void inAArrayAssignStatement(AArrayAssignStatement node)
{
TId id = ((AIdExpression)node.getL()).getId();
String arrName = id.toString();
symtable.put(arrName, node);
}
public void inAIdExpression(AIdExpression node)
{
if(!(node.parent() instanceof AFunction))
{
TId id = node.getId();
String fName = id.toString();
int line = id.getLine();
if(node.parent() instanceof AFunctionCall)
{
boolean exists = false;
for(AFunction fun:functions)
{
fun = (AFunction) fun.clone();
if(fName.equals(((AIdExpression)fun.getExpression()).getId().toString()))
{
exists = true;
}
}
if(!exists)
{
System.out.println("Line " + line + ": " +" Function " + fName +" is not defined");
}
}
else
{
if(!symtable.containsKey(fName))
{
System.out.println("Line " + line + ": " +" Variable " + fName +" is not defined");
}
}
}
}
public void inADivEqualsStatement(ADivEqualsStatement node)
{
TId lId = ((TId)((AIdExpression) node.getL()).getId());
if(symtable.containsKey(lId.toString()))
{
String fName = lId.toString();
AEqualsStatement old = ((AEqualsStatement)symtable.get(lId.toString()));
PExpression oldExp = old.getR();
ADivExpression divExp = new ADivExpression(new AParexprparExpression(old.getR()), node.getR());
ArithmeticData data = checkExpression(divExp);
if(!checkArithmetics(data))
{
AEqualsStatement update = new AEqualsStatement(node.getL(), divExp);
symtable.put(fName, update);
}
else
{
AEqualsStatement update = new AEqualsStatement(node.getL(), oldExp);
symtable.put(fName, update);
}
}
}
public void inAMinusEqualsStatement(AMinusEqualsStatement node)
{
TId lId = ((TId)((AIdExpression) node.getL()).getId());
if(symtable.containsKey(lId.toString()))
{
String fName = lId.toString();
AEqualsStatement old = ((AEqualsStatement)symtable.get(lId.toString()));
PExpression oldExp = old.getR();
ASubtractionExpression divExp = new ASubtractionExpression(new AParexprparExpression(old.getR()), node.getR());
ArithmeticData data = checkExpression(divExp);
if(!checkArithmetics(data))
{
AEqualsStatement update = new AEqualsStatement(node.getL(), divExp);
symtable.put(fName, update);
}
else
{
AEqualsStatement update = new AEqualsStatement(node.getL(), oldExp);
symtable.put(fName, update);
}
}
}
public void inAPlusplusExpression(APlusplusExpression node)
{
if(node.getExpression() instanceof AIdExpression)
{
TId id = ((TId)((AIdExpression) node.getExpression()).getId());
if(symtable.containsKey(id.toString()))
{
String fName = id.toString();
AEqualsStatement old = ((AEqualsStatement)symtable.get(id.toString()));
PExpression oldExp = (PExpression)old.getR().clone();
AAdditionExpression plplExp = new AAdditionExpression(new AParexprparExpression((PExpression)old.getR().clone()), new ANumberExpression(new TNumber("1", id.getLine(), id.getPos())));
ArithmeticData data = checkExpression(plplExp);
if(!checkArithmetics(data))
{
AEqualsStatement update = new AEqualsStatement(node.getExpression(), plplExp);
symtable.put(fName, update);
}
else
{
AEqualsStatement update = new AEqualsStatement(node.getExpression(), oldExp);
symtable.put(fName, update);
}
}
}
else
{
AAdditionExpression plplExp = new AAdditionExpression(new AParexprparExpression((PExpression)node.getExpression().clone()), new ANumberExpression(new TNumber("1")));
checkArithmetics(checkExpression(plplExp));
}
}
public void inAMinusminusExpression(AMinusminusExpression node)
{
if(node.getExpression() instanceof AIdExpression)
{
TId id = ((TId)((AIdExpression) node.getExpression()).getId());
if(symtable.containsKey(id.toString()))
{
String fName = id.toString();
AEqualsStatement old = ((AEqualsStatement)symtable.get(id.toString()));
PExpression oldExp = (PExpression)old.getR().clone();
ASubtractionExpression plplExp = new ASubtractionExpression(new AParexprparExpression((PExpression)old.getR().clone()), new ANumberExpression(new TNumber("1", id.getLine(), id.getPos())));
ArithmeticData data = checkExpression(plplExp);
if(!checkArithmetics(data))
{
AEqualsStatement update = new AEqualsStatement(node.getExpression(), plplExp);
symtable.put(fName, update);
}
else
{
AEqualsStatement update = new AEqualsStatement(node.getExpression(), oldExp);
symtable.put(fName, update);
}
}
}
else
{
ASubtractionExpression plplExp = new ASubtractionExpression(new AParexprparExpression((PExpression)node.getExpression().clone()), new ANumberExpression(new TNumber("1")));
checkArithmetics(checkExpression(plplExp));
}
}
public void inAAdditionExpression(AAdditionExpression node)
{
ArithmeticData data = checkParent(node);
if(data.getTokens().size() > 0) checkArithmetics(data);
}
public void inASubtractionExpression(ASubtractionExpression node)
{
ArithmeticData data = checkParent(node);
if(data.getTokens().size() > 0) checkArithmetics(data);
}
public void inAMultiplicationExpression(AMultiplicationExpression node)
{
ArithmeticData data = checkParent(node);
if(data.getTokens().size() > 0) checkArithmetics(data);
}
public void inADivExpression(ADivExpression node)
{
ArithmeticData data = checkParent(node);
if(data.getTokens().size() > 0) checkArithmetics(data);
}
public void inAModExpression(AModExpression node)
{
ArithmeticData data = checkParent(node);
if(data.getTokens().size() > 0) checkArithmetics(data);
}
public void inAExponentialExpression(AExponentialExpression node)
{
ArithmeticData data = checkParent(node);
if(data.getTokens().size() > 0) checkArithmetics(data);
}
public void inAFunStatement(AFunStatement node)
{
AFunctionCall funCall = (AFunctionCall)node.getFunctionCall();
funCall = (AFunctionCall)funCall.clone();
TId id = ((TId)((AIdExpression) funCall.getExpression()).getId());
String fName = id.toString();
int line = id.getLine();
boolean found = false;
boolean foundCorrect = false;
int numOfArgs = 0;
for(AFunction fun: functions)
{
fun = (AFunction)fun.clone();
String funName = ((AIdExpression)fun.getExpression()).getId().toString();
if(fName.equals(funName))
{
numOfArgs = 0;
found = true;
if(funCall.getArglist().size() != 0)
{
numOfArgs++;
if(((AArglist)funCall.getArglist().get(0)).getR().size() != 0) numOfArgs = numOfArgs + ((AArglist)funCall.getArglist().get(0)).getR().size();
}
if(checkArgSize(line, numOfArgs, fun, true))
{
foundCorrect = true;
symtable.put(fName, runFunction(fun, funCall));
}
}
}
if(found)
{
if(!foundCorrect)
{
for(AFunction fun: functions)
{
fun = (AFunction)fun.clone();
String funName = ((AIdExpression)fun.getExpression()).getId().toString();
if(fName.equals(funName))
{
checkArgSize(line, numOfArgs, fun, false);
}
}
}
}
}
public void inAFunExpression(AFunExpression node)
{
if(!(node.parent().clone() instanceof AArglist))
{
AFunctionCall funCall = (AFunctionCall)node.getFunctionCall();
funCall = (AFunctionCall)funCall.clone();
TId id = ((TId)((AIdExpression) funCall.getExpression()).getId());
String fName = id.toString();
int line = id.getLine();
boolean found = false;
boolean foundCorrect = false;
int numOfArgs = 0;
for(AFunction fun: functions)
{
fun = (AFunction)fun.clone();
String funName = ((AIdExpression)fun.getExpression()).getId().toString();
if(fName.equals(funName))
{
numOfArgs = 0;
found = true;
if(funCall.getArglist().size() != 0)
{
numOfArgs++;
if(((AArglist)funCall.getArglist().get(0)).getR().size() != 0) numOfArgs = numOfArgs + ((AArglist)funCall.getArglist().get(0)).getR().size();
}
if(checkArgSize(line, numOfArgs, fun, true))
{
foundCorrect = true;
symtable.put(fName, runFunction(fun, funCall));
}
}
}
if(found)
{
if(!foundCorrect)
{
for(AFunction fun: functions)
{
fun = (AFunction)fun.clone();
String funName = ((AIdExpression)fun.getExpression()).getId().toString();
if(fName.equals(funName))
{
checkArgSize(line, numOfArgs, fun, false);
}
}
}
}
}
}
public void inAFuncExpression(AFuncExpression node)
{
if(!(node.parent().clone() instanceof AArglist))
{
AFunctionCall funCall = (AFunctionCall)node.getFunctionCall();
funCall = (AFunctionCall)funCall.clone();
TId id = ((TId)((AIdExpression) funCall.getExpression()).getId());
String fName = id.toString();
int line = id.getLine();
boolean found = false;
boolean foundCorrect = false;
int numOfArgs = 0;
for(AFunction fun: functions)
{
fun = (AFunction)fun.clone();
String funName = ((AIdExpression)fun.getExpression()).getId().toString();
if(fName.equals(funName))
{
numOfArgs = 0;
found = true;
if(funCall.getArglist().size() != 0)
{
numOfArgs++;
if(((AArglist)funCall.getArglist().get(0)).getR().size() != 0) numOfArgs = numOfArgs + ((AArglist)funCall.getArglist().get(0)).getR().size();
}
if(checkArgSize(line, numOfArgs, fun, true))
{
foundCorrect = true;
symtable.put(fName, runFunction(fun, funCall));
}
}
}
if(found)
{
if(!foundCorrect)
{
for(AFunction fun: functions)
{
fun = (AFunction)fun.clone();
String funName = ((AIdExpression)fun.getExpression()).getId().toString();
if(fName.equals(funName))
{
checkArgSize(line, numOfArgs, fun, false);
}
}
}
}
}
}
public ArithmeticData checkParent(PExpression node)
{
ArithmeticData arithmeticData = new ArithmeticData();
if((node.parent() instanceof PStatement) && (!(node.parent() instanceof AReturnStatement)))
{
if(!(node.parent().parent() instanceof AFunction))
{
arithmeticData = checkExpression(node);
arithmeticData.setTokens(arithmeticData.getTokens());
arithmeticData.setExpressions(arithmeticData.getExpressions());
}
}
if(node.parent() instanceof AArglist)
{
arithmeticData = checkExpression(node);
arithmeticData.setTokens(arithmeticData.getTokens());
arithmeticData.setExpressions(arithmeticData.getExpressions());
}
if(node.parent() instanceof PComparison)
{
arithmeticData = checkExpression(node);
arithmeticData.setTokens(arithmeticData.getTokens());
arithmeticData.setExpressions(arithmeticData.getExpressions());
}
return arithmeticData;
}
public ArithmeticData checkExpression(PExpression node)
{
List<Token> tokens = new ArrayList<Token>();
List<PExpression> expressions = new ArrayList<PExpression>();
ArithmeticData arithmeticData = new ArithmeticData();
/**
* ----------- ALL TOKENS HERE -----------
*/
if(node instanceof AIdExpression)
{
TId id = ((TId)((AIdExpression) node).getId());
if(!tokens.contains(id))
{
tokens.add(id);
arithmeticData.addTokens(tokens);
}
}
if(node instanceof AParexprparExpression)
{
arithmeticData.addAll(checkExpression(((AParexprparExpression)node).getExpression()));
}
if(node instanceof AFunExpression || node instanceof AFuncExpression)
{
AFunctionCall funCall;
if(node instanceof AFunExpression) funCall = (AFunctionCall)((AFunExpression)node).getFunctionCall();
else funCall = (AFunctionCall)((AFuncExpression)node).getFunctionCall();
funCall = (AFunctionCall)funCall.clone();
TId id = ((TId)((AIdExpression) funCall.getExpression()).getId());
String fName = id.toString();
int line = id.getLine();
boolean found = false;
boolean foundCorrect = false;
int numOfArgs = 0;
for(AFunction fun: functions)
{
fun = (AFunction)fun.clone();
String funName = ((AIdExpression)fun.getExpression()).getId().toString();
if(fName.equals(funName))
{
numOfArgs = 0;
found = true;
if(funCall.getArglist().size() != 0)
{
numOfArgs++;
if(((AArglist)funCall.getArglist().get(0)).getR().size() != 0) numOfArgs = numOfArgs + ((AArglist)funCall.getArglist().get(0)).getR().size();
}
if(checkArgSize(line, numOfArgs, fun, true))
{
foundCorrect = true;
PStatement ret = runFunction(fun, funCall);
ret = (PStatement) ret.clone();
if(ret instanceof AReturnStatement)
{
PExpression retExp = (PExpression)((AReturnStatement)ret).getExpression().clone();
arithmeticData.addAll(checkExpression(retExp));
}
else
{
arithmeticData.addAll(checkExpression(new ANoneExpression(new TNone(id.getLine(), id.getPos()))));
}
}
}
}
if(found)
{
if(!foundCorrect)
{
for(AFunction fun: functions)
{
fun = (AFunction)fun.clone();
String funName = ((AIdExpression)fun.getExpression()).getId().toString();
if(fName.equals(funName))
{
checkArgSize(line, numOfArgs, fun, false);
}
}
}
}
}
if(node instanceof ANoneExpression)
{
TNone id = ((TNone)((ANoneExpression) node).getNone());
if(!tokens.contains(id))
{
tokens.add(id);
arithmeticData.addTokens(tokens);
expressions.add(node);
arithmeticData.addExpressions(expressions);
}
}
if(node instanceof AStringExpression)
{
TString id = ((TString)((AStringExpression) node).getString());
if(!tokens.contains(id))
{
tokens.add(id);
arithmeticData.addTokens(tokens);
}
}
if(node instanceof ANumberExpression)
{
TNumber id = ((TNumber)((ANumberExpression) node).getNumber());
if(!tokens.contains(id))
{
tokens.add(id);
arithmeticData.addTokens(tokens);
}
}
if(node instanceof ATypeExpression)
{
TId id = ((TId)((AIdExpression)((ATypeExpression) node).getExpression()).getId());
if(!tokens.contains(id))
{
expressions.add(node);
arithmeticData.addExpressions(expressions);
}
}
/**
* ----------- ALL ARITHMETICS HERE -----------
*/
if(node instanceof AAdditionExpression)
{
((AAdditionExpression)node).setL(checkElement(((AAdditionExpression)node).getL()));
((AAdditionExpression)node).setR(checkElement(((AAdditionExpression)node).getR()));
PExpression lExp = (PExpression)((AAdditionExpression)node).getL();
PExpression rExp = (PExpression)((AAdditionExpression)node).getR();
ArithmeticData lData = checkExpression(lExp);
ArithmeticData rData = checkExpression(rExp);
tokens.addAll(lData.getTokens());
tokens.addAll(rData.getTokens());
expressions.addAll(lData.getExpressions());
expressions.addAll(rData.getExpressions());
expressions.add(node);
arithmeticData.addTokens(tokens);
arithmeticData.addExpressions(expressions);
}
if(node instanceof ASubtractionExpression)
{
((ASubtractionExpression)node).setL(checkElement(((ASubtractionExpression)node).getL()));
((ASubtractionExpression)node).setR(checkElement(((ASubtractionExpression)node).getR()));
PExpression lExp = (PExpression)((ASubtractionExpression)node).getL();
PExpression rExp = (PExpression)((ASubtractionExpression)node).getR();
ArithmeticData lData = checkExpression(lExp);
ArithmeticData rData = checkExpression(rExp);
tokens.addAll(lData.getTokens());
tokens.addAll(rData.getTokens());
expressions.addAll(lData.getExpressions());
expressions.addAll(rData.getExpressions());
expressions.add(node);
arithmeticData.addTokens(tokens);
arithmeticData.addExpressions(expressions);
}
if(node instanceof AMultiplicationExpression)
{
((AMultiplicationExpression)node).setL(checkElement(((AMultiplicationExpression)node).getL()));
((AMultiplicationExpression)node).setR(checkElement(((AMultiplicationExpression)node).getR()));
PExpression lExp = (PExpression)((AMultiplicationExpression)node).getL();
PExpression rExp = (PExpression)((AMultiplicationExpression)node).getR();
ArithmeticData lData = checkExpression(lExp);
ArithmeticData rData = checkExpression(rExp);
tokens.addAll(lData.getTokens());
tokens.addAll(rData.getTokens());
expressions.addAll(lData.getExpressions());
expressions.addAll(rData.getExpressions());
expressions.add(node);
arithmeticData.addTokens(tokens);
arithmeticData.addExpressions(expressions);
}
if(node instanceof ADivExpression)
{
((ADivExpression)node).setL(checkElement(((ADivExpression)node).getL()));
((ADivExpression)node).setR(checkElement(((ADivExpression)node).getR()));
PExpression lExp = (PExpression)((ADivExpression)node).getL();
PExpression rExp = (PExpression)((ADivExpression)node).getR();
ArithmeticData lData = checkExpression(lExp);
ArithmeticData rData = checkExpression(rExp);
tokens.addAll(lData.getTokens());
tokens.addAll(rData.getTokens());
expressions.addAll(lData.getExpressions());
expressions.addAll(rData.getExpressions());
expressions.add(node);
arithmeticData.addTokens(tokens);
arithmeticData.addExpressions(expressions);
}
if(node instanceof AModExpression)
{
((AModExpression)node).setL(checkElement(((AModExpression)node).getL()));
((AModExpression)node).setR(checkElement(((AModExpression)node).getR()));
PExpression lExp = (PExpression)((AModExpression)node).getL();
PExpression rExp = (PExpression)((AModExpression)node).getR();
ArithmeticData lData = checkExpression(lExp);
ArithmeticData rData = checkExpression(rExp);
tokens.addAll(lData.getTokens());
tokens.addAll(rData.getTokens());
expressions.addAll(lData.getExpressions());
expressions.addAll(rData.getExpressions());
expressions.add(node);
arithmeticData.addTokens(tokens);
arithmeticData.addExpressions(expressions);
}
if(node instanceof AExponentialExpression)
{
((AExponentialExpression)node).setL(checkElement(((AExponentialExpression)node).getL()));
((AExponentialExpression)node).setR(checkElement(((AExponentialExpression)node).getR()));
PExpression lExp = (PExpression)((AExponentialExpression)node).getL();
PExpression rExp = (PExpression)((AExponentialExpression)node).getR();
ArithmeticData lData = checkExpression(lExp);
ArithmeticData rData = checkExpression(rExp);
tokens.addAll(lData.getTokens());
tokens.addAll(rData.getTokens());
expressions.addAll(lData.getExpressions());
expressions.addAll(rData.getExpressions());
expressions.add(node);
arithmeticData.addTokens(tokens);
arithmeticData.addExpressions(expressions);
}
return arithmeticData;
}
public boolean checkArithmetics(ArithmeticData data)
{
boolean additionOnly = true;
boolean multOnly = true;
int line = 0;
for(PExpression expression:data.getExpressions())
{
if(expression instanceof ANoneExpression)
{
line = ((TNone)((ANoneExpression)expression).getNone()).getLine();
System.out.println("Line " + line + ": None expression in arithmetics");
}
else if(expression instanceof ATypeExpression)
{
line = ((TId)((AIdExpression)((ATypeExpression)expression).getExpression()).getId()).getLine();
System.out.println("Line " + line + ": Type expression in arithmetics");
}
else if(expression instanceof ASubtractionExpression)
{
additionOnly = false;
multOnly = false;
}
else if(expression instanceof AMultiplicationExpression)
{
additionOnly = false;
}
else if(expression instanceof ADivExpression)
{
additionOnly = false;
multOnly = false;
}
else if(expression instanceof AModExpression)
{
additionOnly = false;
multOnly = false;
}
else if(expression instanceof AExponentialExpression)
{
additionOnly = false;
multOnly = false;
}
else if(expression instanceof AAdditionExpression)
{
multOnly = false;
}
}
return checkTokens(data.getTokens(), additionOnly, multOnly);
}
public boolean checkTokens(List<Token> tokenIds, boolean addOnly, boolean multOnly)
{
boolean foundString = false;
boolean foundNumber = false;
boolean foundDouble = false;
boolean foundError = false;
int line = 0;
for(Token token:tokenIds)
{
if(token instanceof TString)
{
foundString = true;
line = token.getLine();
}
if(token instanceof TNumber)
{
foundNumber = true;
if(token.getText().contains(".")) foundDouble = true;
line = token.getLine();
}
while(token instanceof TId)
{
if(symtable.containsKey(token.toString()))
{
if(symtable.get(token.toString()) instanceof AEqualsStatement)
{
ArithmeticData d = checkExpression(((AEqualsStatement)symtable.get(token.toString())).getR());
if(d.getTokens().size() >= 1)
{
if(d.getTokens().get(0) instanceof TString)
{
foundString = true;
line = token.getLine();
break;
}
if(d.getTokens().get(0) instanceof TNumber)
{
foundNumber = true;
if(d.getTokens().get(0).getText().contains(".")) foundDouble = true;
line = token.getLine();
break;
}
if(d.getTokens().get(0) instanceof TNone)
{
break;
}
if(d.getTokens().get(0) instanceof TId)
{
if(!symtable.containsKey(d.getTokens().get(0).toString()))
{
break;
}
else
{
token = d.getTokens().get(0);
}
}
}
}
}
else
{
break;
}
}
}
if(addOnly)
{
if(foundNumber && foundString)
{
foundError = true;
System.out.println("Line " + line + ": Found string(s) and number(s) in addition");
}
}
else if(multOnly)
{
if(foundDouble)
{
foundError = true;
System.out.println("Line " + line + ": strings can only be multiplied with integers");
}
if(!foundNumber && foundString)
{
foundError = true;
System.out.println("Line " + line + ": strings can only be multiplied with integers");
}
}
else
{
if(foundString)
{
foundError = true;
System.out.println("Line " + line + ": strings can only be used in additions");
}
}
return foundError;
}
public boolean checkArgSize(int line, int numOfArgs, AFunction node, boolean arithmetics)
{
boolean isCorrect = true; //checks if argsize is correct for the defined function.
boolean isDefault;
AArgument arg;
TypedLinkedList arguments;
int minArgs = 0;
int maxArgs = 0;
arguments = ((TypedLinkedList)node.getArgument());
if(arguments.size() != 0)
{
maxArgs++;
arg = ((AArgument)arguments.get(0));
TypedLinkedList moreIdentifiers = ((TypedLinkedList)arg.getMoreIdentifiers());
isDefault = arg.getEqualsValue().size() == 0;
if(!isDefault) //if it is a default value, it can be skipped during the function call.
{
minArgs++;
}
for(int i=0; i<moreIdentifiers.size(); i++)
{
maxArgs++;
isDefault = ((AMoreIdentifiers)moreIdentifiers.get(i)).getEqualsValue().size() == 0;
if(!isDefault)
{
minArgs++;
}
}
}
minArgs = maxArgs - minArgs;
if(numOfArgs < minArgs || numOfArgs > maxArgs)
{
isCorrect = false;
if(!arithmetics) System.out.println("Line " + line + ": Found " + numOfArgs + " arguments, expected [" + minArgs + ", " + maxArgs + "]");
}
return isCorrect;
}
public PStatement runFunction(AFunction fun, AFunctionCall funCall)
{
fun = (AFunction) fun.clone();
funCall = (AFunctionCall) funCall.clone();
PStatement returnStatement = (PStatement)fun.getStatement().clone();
ArithmeticData data = new ArithmeticData();
if(funCall.getArglist().size()>0)
{
AArglist argFunCall = (AArglist)funCall.getArglist().get(0);
AArgument argFun = (AArgument)fun.getArgument().get(0);
AIdExpression search = (AIdExpression)argFun.getExpression().clone();
PExpression assign = (PExpression)argFunCall.getL().clone();
if(assign instanceof AFunExpression || assign instanceof AFuncExpression)
{
PStatement statement;
if(assign instanceof AFunExpression)
{
statement = runFunction(fun, (AFunctionCall)((AFunExpression)assign).getFunctionCall());
}
else
{
statement = runFunction(fun, (AFunctionCall)((AFuncExpression)assign).getFunctionCall());
}
if(statement instanceof AReturnStatement)
{
assign = (PExpression)((AReturnStatement)statement).getExpression().clone();
data.addAll(checkExpression(assign));
}
else
{
int line = ((TId)search.getId()).getLine();
System.out.println("Line " + line + ": no return statement found");
assign = new ANoneExpression(new TNone(line, ((TId)search.getId()).getPos()));
data.addAll(checkExpression(assign));
}
}
AEqualsStatement update = new AEqualsStatement(search, assign);
symtable.put(search.getId().toString(), update);
if(argFunCall.getR().size()>0)
{
for(int i=0; i<argFunCall.getR().size(); i++)
{
AMoreIdentifiers moreIds = (AMoreIdentifiers)argFun.getMoreIdentifiers().get(i);
assign = (PExpression)((AArglist)argFunCall.clone()).getR().get(i);
search = (AIdExpression)((AMoreIdentifiers)moreIds.clone()).getExpression();
if(assign instanceof AFunExpression || assign instanceof AFuncExpression)
{
PStatement statement;
if(assign instanceof AFunExpression)
{
statement = runFunction(fun, (AFunctionCall)((AFunExpression)assign).getFunctionCall());
}
else
{
statement = runFunction(fun, (AFunctionCall)((AFuncExpression)assign).getFunctionCall());
}
if(statement instanceof AReturnStatement)
{
assign = ((AReturnStatement)statement).getExpression();
data.addAll(checkExpression(assign));
}
else
{
int line = ((TId)search.getId()).getLine();
System.out.println("Line " + line + ": no return statement found");
assign = new ANoneExpression(new TNone(line, ((TId)search.getId()).getPos()));
data.addAll(checkExpression(assign));
}
}
update = new AEqualsStatement(search, assign);
symtable.put(search.getId().toString(), update);
}
}
}
if(fun.getStatement() instanceof AReturnStatement)
{
PExpression returnExp = ((AReturnStatement)fun.getStatement()).getExpression();
data.addAll(checkExpression(returnExp));
checkArithmetics(data);
}
else if(fun.getStatement() instanceof APrintStatement)
{
APrintStatement print = (APrintStatement)fun.getStatement();
checkArithmetics(checkExpression(print.getL()));
if(print.getR().size() > 0)
{