-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisitor.java
More file actions
1278 lines (1105 loc) · 62.9 KB
/
Visitor.java
File metadata and controls
1278 lines (1105 loc) · 62.9 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
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 SymTableClasses.SymFunction;
import SymTableClasses.SymItem;
import SymTableClasses.SymVariable;
import minipython.analysis.DepthFirstAdapter;
import minipython.node.*;
import java.util.*;
public class Visitor extends DepthFirstAdapter {
private Hashtable<String, SymItem> methodTable;
private Hashtable<String, SymItem> variableTable;
private Stack<String> operatorStack = new Stack<>();
//private Stack<HashMap<String,String>> stack = new Stack<>();
private Stack<String> returnStackType = new Stack<>();
private Stack<SymFunction> stackFunctions = new Stack<>();
private int globalLine;
int functionCallLine = 0;
// Constructor for Visitor
public Visitor(Hashtable<String, SymItem> methodTable, Hashtable<String, SymItem> variableTable) {
this.methodTable = methodTable;
this.variableTable = variableTable;
}
private boolean inFuncionCallStatement = false;
private boolean inFunctionDef = false;
private boolean inAssignStatement = false;
private boolean isLeftSideIdentifier;
private boolean inFunctionCallId = false;
private boolean inFunctionStatement = false;
private boolean activeLineTracker = true;
//1. Check if a variable is defined before usage
@Override
public void inAAssignPlusStatement(AAssignPlusStatement node) {
inAssignStatement = true;
isLeftSideIdentifier = true;
}
@Override
public void outAStatFunctionOrStatement(AStatFunctionOrStatement node){
returnStackType.clear();
}
@Override
public void outAAssignPlusStatement(AAssignPlusStatement node) {
inAssignStatement = false;
}
@Override
public void inAAssignMinusStatement(AAssignMinusStatement node) {
inAssignStatement = true;
isLeftSideIdentifier = true;
}
@Override
public void outAAssignMinusStatement(AAssignMinusStatement node) {
inAssignStatement = false;
}
@Override
public void inAAssignMultStatement(AAssignMultStatement node) {
inAssignStatement = true;
isLeftSideIdentifier = true;
}
@Override
public void outAAssignMultStatement(AAssignMultStatement node) {
inAssignStatement = false;
}
@Override
public void inAAssignDivStatement(AAssignDivStatement node) {
inAssignStatement = true;
isLeftSideIdentifier = true;
}
@Override
public void outAAssignDivStatement(AAssignDivStatement node) {
inAssignStatement = false;
}
@Override
public void inAAssignEqStatement(AAssignEqStatement node) {
inAssignStatement = true;
isLeftSideIdentifier = true;
}
@Override
public void outAAssignEqStatement(AAssignEqStatement node) {
inAssignStatement = false;
}
@Override
public void inAFunction(AFunction node) {
inFunctionDef = true;
}
@Override
public void outAFunction(AFunction node) {
inFunctionDef = false;
}
@Override
public void inAFunctionCall(AFunctionCall node) {
inFunctionCallId = true;
inFuncionCallStatement = true;
}
@Override
public void outAFunctionCall(AFunctionCall node) {
if (!stackFunctions.empty()) stackFunctions.pop();
//stack.pop();
if (stackFunctions.empty()){
//if (stack.empty()){
inFuncionCallStatement = false;
activeLineTracker = true;
}
}
@Override
public void inAFCallStatement(AFCallStatement node) {
inFunctionStatement = true;
}
@Override
public void outAFCallStatement(AFCallStatement node) {
inFunctionStatement = false;
}
@Override
public void caseAFunction(AFunction node){
inAFunction(node);
outAFunction(node);
}
@Override
public void inAPrintStatement(APrintStatement node) {
int line = node.getPrint().getLine();
if (activeLineTracker && line >= globalLine) globalLine = line;
if (ParserTest1.debug) System.out.println("\nGlobal Line: " + globalLine +
"\nLocal Line: " + line +
"\ninFunctionDef: " + inFunctionDef);
}
@Override
public void caseAIdentifier(AIdentifier node) {
inAIdentifier(node);
if (node.getId() != null) {
node.getId().apply(this);
}
/**
* isLefSideIdentifier: Checks if is an assignement -> z = whatever
* inFuctionDef: Checks if we are inside a Function Definition
* inFunctionCallId: Cheks if we are inside function call, but don't want to check funtion identifier. --> x()
*/
int line = node.getId().getLine();
if (activeLineTracker && line >= globalLine) globalLine = line;
if (ParserTest1.debug) System.out.println("\nGlobal Line: " + globalLine +
"\nLocal Line: " + line +
"\ninFunctionDef: " + inFunctionDef);
// System.out.println(" WHO AM I ON LINE: "+line+" ");
// System.out.println("isLeftSideIdentifier: "+isLeftSideIdentifier);
// System.out.println("inFunctionDef: "+inFunctionDef);
// System.out.println("inFunctionCallId: "+inFunctionCallId);
// System.out.println("---------------------------------------------");
if ( !( inFunctionDef || inFunctionCallId )) {
String varName = node.getId().toString().trim();
if (ParserTest1.debug) System.out.println("Checking id: " + varName + ".");
if (!this.variableTable.containsKey(varName.trim())) {
System.out.println("\n!!! Error in line:" + line + ": Variable '" + varName + "' used before declaration.");
ParserTest1.errors += 1;
return;
}
if (ParserTest1.debug) {
System.out.println("===========================");
System.out.println(" TEST 1 PASSED FOR VAR : "+ varName);
System.out.println("===========================");
}
inFunctionCallId = false;
isLeftSideIdentifier = false;
}
outAIdentifier(node);
}
// Fill up the variables table
@Override
public void caseAAssignEqStatement(AAssignEqStatement node) {
inAAssignEqStatement(node);
String variableName = "";
if (node.getIdentifier() != null) {
variableName = getAId(node.getIdentifier()).getId().toString().trim();
int line = getAId(node.getIdentifier()).getId().getLine();
if (activeLineTracker && line >= globalLine) globalLine = line;
if (ParserTest1.debug) System.out.println("\nGlobal Line: " + globalLine +
"\nLocal Line: " + line +
"\ninFunctionDef: " + inFunctionDef);
// Δημιουργία μεταβλητής και αποθήκευση στον πίνακα συμβόλων
if(!inFunctionDef){
SymVariable variable = new SymVariable(variableName, line, tableFillVisitor.determineExpressionType(node.getExpression()));
if (node.getExpression() != null && !(node.getExpression().toString()).contains(variableName)) {
if (ParserTest1.debug) System.out.println("VARIABLE TO STORE: "+variableName);
variableTable.put(variableName, variable);
}
}
}
// Έλεγχος της έκφρασης μετά το "="
if (node.getExpression() != null) {
if (ParserTest1.debug) System.out.println("Expression: "+(node.getExpression().toString()).contains(variableName));
node.getExpression().apply(this);
} else {
ParserTest1.errors += 1;
System.err.println("\n!!! Error in line:" + getAId(node.getIdentifier()).getId().getLine() + " after '=' ");
}
outAAssignEqStatement(node);
}
@Override
public void caseAAssignPlusStatement(AAssignPlusStatement node) {
inAAssignPlusStatement(node);
String variableName = "";
if (node.getIdentifier() != null) {
variableName = getAId(node.getIdentifier()).getId().toString().trim();
int line = getAId(node.getIdentifier()).getId().getLine();
if (activeLineTracker && line >= globalLine) globalLine = line;
if (ParserTest1.debug) System.out.println("\nGlobal Line: " + globalLine +
"\nLocal Line: " + line +
"\ninFunctionDef: " + inFunctionDef);
// Δημιουργία μεταβλητής και αποθήκευση στον πίνακα συμβόλων
if(!inFunctionDef){
if ( variableTable.containsKey(variableName) ){
String leftType = variableTable.get(variableName).getType();
String rightType = tableFillVisitor.determineExpressionType(node.getExpression());
if ( leftType == rightType ) {
SymVariable variable = new SymVariable(variableName, line, rightType);
if (ParserTest1.debug) System.out.println("Updating variable: "+variableName);
variableTable.put(variableName, variable);
} else {
ParserTest1.errors += 1;
System.err.println("\n!!! Error in line " + getAId(node.getIdentifier()).getId().getLine()
+ ": Can't do += because left Type is " + leftType + " and right Type is " + rightType);
}
} else {
ParserTest1.errors += 1;
System.err.println("\n!!! Error in line " + getAId(node.getIdentifier()).getId().getLine()
+ ": Can't do += because variable " + variableName + " is not yet defined.");
}
}
}
// Έλεγχος της έκφρασης μετά το "+="
if (node.getExpression() != null) {
if (ParserTest1.debug) System.out.println("Expression: "+(node.getExpression().toString()).contains(variableName));
node.getExpression().apply(this);
} else {
ParserTest1.errors += 1;
System.err.println("\n!!! Error in line:" + getAId(node.getIdentifier()).getId().getLine() + " after '+=' ");
}
outAAssignPlusStatement(node);
}
@Override
public void caseAAssignMinusStatement(AAssignMinusStatement node) {
inAAssignMinusStatement(node);
String variableName = "";
if (node.getIdentifier() != null) {
variableName = getAId(node.getIdentifier()).getId().toString().trim();
int line = getAId(node.getIdentifier()).getId().getLine();
if (activeLineTracker && line >= globalLine) globalLine = line;
if (ParserTest1.debug) System.out.println("\nGlobal Line: " + globalLine +
"\nLocal Line: " + line +
"\ninFunctionDef: " + inFunctionDef);
// Δημιουργία μεταβλητής και αποθήκευση στον πίνακα συμβόλων
if(!inFunctionDef){
if ( variableTable.containsKey(variableName) ){
String leftType = variableTable.get(variableName).getType();
String rightType = tableFillVisitor.determineExpressionType(node.getExpression());
if ( leftType == rightType ) {
SymVariable variable = new SymVariable(variableName, line, rightType);
if (ParserTest1.debug) System.out.println("Updating variable: "+variableName);
variableTable.put(variableName, variable);
} else {
ParserTest1.errors += 1;
System.err.println("\n!!! Error in line " + getAId(node.getIdentifier()).getId().getLine()
+ ": Can't do -= because left Type is " + leftType + " and right Type is " + rightType);
}
} else {
ParserTest1.errors += 1;
System.err.println("\n!!! Error in line " + getAId(node.getIdentifier()).getId().getLine()
+ ": Can't do -= because variable " + variableName + " is not yet defined.");
}
}
}
// Έλεγχος της έκφρασης μετά το "+="
if (node.getExpression() != null) {
if (ParserTest1.debug) System.out.println("Expression: "+(node.getExpression().toString()).contains(variableName));
node.getExpression().apply(this);
} else {
ParserTest1.errors += 1;
System.err.println("\n!!! Error in line:" + getAId(node.getIdentifier()).getId().getLine() + " after '-=' ");
}
outAAssignMinusStatement(node);
}
@Override
public void caseAAssignDivStatement(AAssignDivStatement node) {
inAAssignDivStatement(node);
String variableName = "";
if (node.getIdentifier() != null) {
variableName = getAId(node.getIdentifier()).getId().toString().trim();
int line = getAId(node.getIdentifier()).getId().getLine();
if (activeLineTracker && line >= globalLine) globalLine = line;
if (ParserTest1.debug) System.out.println("\nGlobal Line: " + globalLine +
"\nLocal Line: " + line +
"\ninFunctionDef: " + inFunctionDef);
// Δημιουργία μεταβλητής και αποθήκευση στον πίνακα συμβόλων
if(!inFunctionDef){
if ( variableTable.containsKey(variableName) ){
String leftType = variableTable.get(variableName).getType();
String rightType = tableFillVisitor.determineExpressionType(node.getExpression());
if ( leftType == rightType ) {
SymVariable variable = new SymVariable(variableName, line, rightType);
if (ParserTest1.debug) System.out.println("Updating variable: "+variableName);
variableTable.put(variableName, variable);
} else {
ParserTest1.errors += 1;
System.err.println("\n!!! Error in line " + getAId(node.getIdentifier()).getId().getLine()
+ ": Can't do /= because left Type is " + leftType + " and right Type is " + rightType);
}
} else {
ParserTest1.errors += 1;
System.err.println("\n!!! Error in line " + getAId(node.getIdentifier()).getId().getLine()
+ ": Can't do /= because variable " + variableName + " is not yet defined.");
}
}
}
// Έλεγχος της έκφρασης μετά το "+="
if (node.getExpression() != null) {
if (ParserTest1.debug) System.out.println("Expression: "+(node.getExpression().toString()).contains(variableName));
node.getExpression().apply(this);
} else {
ParserTest1.errors += 1;
System.err.println("\n!!! Error in line:" + getAId(node.getIdentifier()).getId().getLine() + " after '/=' ");
}
outAAssignDivStatement(node);
}
@Override
public void caseAAssignMultStatement(AAssignMultStatement node) {
inAAssignMultStatement(node);
String variableName = "";
if (node.getIdentifier() != null) {
variableName = getAId(node.getIdentifier()).getId().toString().trim();
int line = getAId(node.getIdentifier()).getId().getLine();
if (activeLineTracker && line >= globalLine) globalLine = line;
if (ParserTest1.debug) System.out.println("\nGlobal Line: " + globalLine +
"\nLocal Line: " + line +
"\ninFunctionDef: " + inFunctionDef);
// Δημιουργία μεταβλητής και αποθήκευση στον πίνακα συμβόλων
if(!inFunctionDef){
if ( variableTable.containsKey(variableName) ){
String leftType = variableTable.get(variableName).getType();
String rightType = tableFillVisitor.determineExpressionType(node.getExpression());
if ( leftType == rightType ||
(leftType == "String" && rightType == "Number") ||
(leftType == "Number" && rightType == "String"))
{
SymVariable variable = new SymVariable(variableName, line, rightType);
if (ParserTest1.debug) System.out.println("Updating variable: "+variableName);
variableTable.put(variableName, variable);
} else {
ParserTest1.errors += 1;
System.err.println("\n!!! Error in line " + getAId(node.getIdentifier()).getId().getLine()
+ ": Can't do /= because left Type is " + leftType + " and right Type is " + rightType);
}
} else {
ParserTest1.errors += 1;
System.err.println("\n!!! Error in line " + getAId(node.getIdentifier()).getId().getLine()
+ ": Can't do *= because variable " + variableName + " is not yet defined.");
}
}
}
// Έλεγχος της έκφρασης μετά το "*="
if (node.getExpression() != null) {
if (ParserTest1.debug) System.out.println("Expression: "+(node.getExpression().toString()).contains(variableName));
node.getExpression().apply(this);
} else {
ParserTest1.errors += 1;
System.err.println("\n!!! Error in line:" + getAId(node.getIdentifier()).getId().getLine() + " after '*=' ");
}
outAAssignMultStatement(node);
}
@SuppressWarnings("rawtypes")
public void caseAFunctionCall(AFunctionCall node)
{
inAFunctionCall(node);
String matchedKey = null;
if(node.getIdentifier() != null)
{
node.getIdentifier().apply(this);
}
activeLineTracker = false;
String calledFunctionName = getAId(node.getIdentifier()).getId().toString().trim();
int line = getAId(node.getIdentifier()).getId().getLine();
if( line >= globalLine) globalLine = line;
LinkedList arglist = node.getArglist();
int calledFunctionArgs= 0;
if (arglist.size() != 0) {
calledFunctionArgs+= 1;
calledFunctionArgs+= ((AArglist)node.getArglist().get(0)).getAdditionalExpression().size();
}
//Check if function arguments are defined before proceeding
for (Object arg : arglist) {
if (arg instanceof AArglist) {
AArglist aArglist = (AArglist) arg;
// Check the main expression
PExpression expr = aArglist.getExpression();
checkIfExpressionIsDefined(expr, line);
// Check additional expressions
for (Object additionalExprObj : aArglist.getAdditionalExpression()) {
AAdditionalExpression additionalExpr = (AAdditionalExpression) additionalExprObj;
checkIfExpressionIsDefined(additionalExpr.getExpression(), line);
}
}
}
String functionNameWithArguments = calledFunctionArgs + calledFunctionName;
// System.out.println("CHECKING FUNTION WITH: " + calledFunctionArgs + " ARGUMENTS.");
boolean foundMatch = true;
if (methodTable.containsKey(functionNameWithArguments)) {
// Check for exact match 2add(2,3) == 2add(x,y)
// System.out.println("ALL IS GOOD. RELEVANT FUNCTION DEFINITION FOUND.");
matchedKey = functionNameWithArguments;
} else {
// If not found then we have to search for a function def that has default values.
// These are stored in this format <minArguments><FunctionName>.
// So add(x,y,z=3) would be stored as 2add whilst add(x,y,z,p) would be 4add
ArrayList<String> possibleMatches = new ArrayList<String>(); // We will add possible matches here.
Set<String> setOfKeys = methodTable.keySet();
for (String key : setOfKeys) {
String possibleFunction = key.substring(1); // Holds the name without the number
int minArguments = Character.getNumericValue(key.charAt(0)); // Holds the number
// System.out.println("\nChecking " + key + " and name is :" + possibleFunction + " and min is " + minArguments);
// If the name mathces and the number of arguments is more than the minimum args of the defined function
if ( possibleFunction.equals(calledFunctionName) && ( minArguments < calledFunctionArgs) ) {
possibleMatches.add(key); // add it to possible matches
}
}
// If the list has available matches.
if ( possibleMatches.size() > 0 ) {
for ( String possibleKey : possibleMatches) {
int methodArguments = 0;
int defaultArguments = 0;
SymFunction possibleMatch = (SymFunction)methodTable.get(possibleKey);
LinkedList<AArgument> args = possibleMatch.getArguments();
if (args.size() != 0) {
AArgument arguments = args.get(0);
methodArguments += 1;
if (arguments.getArgumentAssign().size() != 0) {
defaultArguments += 1;
}
methodArguments += arguments.getArgumentAdditionalAssign().size();
ListIterator it = arguments.getArgumentAdditionalAssign().listIterator();
while ( it.hasNext() ) {
AArgumentAdditionalAssign arg = (AArgumentAdditionalAssign)it.next();
if (arg.getArgumentAssign().size() != 0) {
defaultArguments += 1;
}
}
}
if (ParserTest1.debug) {
System.out.println("\nExisting function arguments: " + methodArguments);
System.out.println("Existing function default arguments: " + defaultArguments);
}
if ( (calledFunctionArgs > methodArguments) || (calledFunctionArgs < methodArguments - defaultArguments) ) {
ParserTest1.errors +=1;
System.out.println("\n!!!! Error in line " + line + ": " +" Function Call "
+ calledFunctionName + " doesn't match any defined function\n"
+ "Check function definition " + calledFunctionName + " in line " + possibleMatch.getDefLine());
foundMatch = false;
continue;
}
matchedKey = possibleKey;
// Check for return statement.
if ( ((SymFunction)methodTable.get(matchedKey)).isReturns() && inFunctionStatement ) {
ParserTest1.errors +=1;
System.out.println("\n!!!! Error in line " + line + ": " +" Function "
+ calledFunctionName + " returns a value but is not used anywhere!\n");
}
}
} else {
// No possible matches were found!
foundMatch = false;
ParserTest1.errors +=1;
System.out.println("\n!!!! Error in line " + line + ": " +" Function "
+ calledFunctionName + " with " + calledFunctionArgs + " arguments isn't defined!\n");
}
}
if (matchedKey != null){
if (ParserTest1.debug) System.out.println("\nmatchedKey" + matchedKey);
// Ανάκτηση της συνάρτησης από τον πίνακα με τις μεθόδους
SymFunction function = (SymFunction) methodTable.get(matchedKey);
// Δημιουργία HashMap για την αντιστοίχιση ορισμάτων της συνάρτησης και τιμών κλήσης
HashMap<String, String> defArgsCallArgs = new HashMap<>();
// Ανάκτηση της λίστας των ορισμάτων της δήλωσης της συνάρτησης
LinkedList<AArgument> defArgs = function.getArguments();
LinkedList<String> defArgsList = new LinkedList<>();
HashMap<String, String> defaultValues = new HashMap<>();
if(!defArgs.isEmpty()){
String argName = defArgs.getFirst().getIdentifier().toString().trim();
defArgsList.add(argName);
// Αν υπάρχει προκαθορισμένη τιμή στο πρώτο όρισμα
if (!defArgs.getFirst().getArgumentAssign().isEmpty()) {
AArgumentAssign assign = (AArgumentAssign) defArgs.getFirst().getArgumentAssign().get(0);
PValue assingValue = (PValue) assign.getValue();
// Προσδιορισμός του τύπου της προκαθορισμένης τιμής
String defaultValue = "Unknown";
if (assingValue instanceof ANumberValue) {
defaultValue = "Number";
} else if (assingValue instanceof ASqStringValue || assingValue instanceof ADqStringValue) {
defaultValue = "String";
} else if (assingValue instanceof ANoneValue) {
defaultValue = "None";
}
defaultValues.put(argName, defaultValue);
}
// Έλεγχος για προκαθορισμένες τιμές στα ορίσματα της συνάρτησης
// Αν υπάρχουν επιπλέον ορίσματα με προκαθορισμένες τιμές
if (!defArgs.getFirst().getArgumentAdditionalAssign().isEmpty()){
LinkedList<AArgumentAdditionalAssign> addArgAssings = defArgs.getFirst().getArgumentAdditionalAssign();
for (Object addAssing : addArgAssings) {
AArgumentAdditionalAssign addAssingArgs = (AArgumentAdditionalAssign) addAssing;
argName = addAssingArgs.getIdentifier().toString().trim();
defArgsList.add(argName);
if (!addAssingArgs.getArgumentAssign().isEmpty()){
AArgumentAssign defaulAddAssing = (AArgumentAssign) addAssingArgs.getArgumentAssign().get(0);
PValue assingAddValue = (PValue) defaulAddAssing.getValue();
String defaultAddValue = "Unknown";
if (assingAddValue instanceof ANumberValue) {
defaultAddValue = "Number";
} else if (assingAddValue instanceof ASqStringValue || assingAddValue instanceof ADqStringValue) {
defaultAddValue = "String";
} else if (assingAddValue instanceof ANoneValue) {
defaultAddValue = "None";
}
defaultValues.put(argName, defaultAddValue);
}
}
}
// Αντιγραφή των προκαθορισμένων τιμών στον χάρτη αντιστοίχισης
defArgsCallArgs.putAll(defaultValues);
// Αν η λίστα των ορισμάτων κλήσης δεν είναι κενή
if (!arglist.isEmpty()) {
AArglist callArgs = (AArglist) (Object) arglist.get(0);
PExpression callarg1Ex = (PExpression) callArgs.getExpression();
LinkedList<PExpression> callArgsList = new LinkedList<>();
callArgsList.add(callarg1Ex);
// Ανάκτηση των υπόλοιπων ορισμάτων κλήσης
for (Object arg : callArgs.getAdditionalExpression()) {
AAdditionalExpression addEx = (AAdditionalExpression) arg;
PExpression ex = (PExpression) addEx.getExpression();
callArgsList.add(ex);
}
Iterator<String> defArgsIterator = defArgsList.iterator();
Iterator<PExpression> callArgsIterator = callArgsList.iterator();
while (defArgsIterator.hasNext() && callArgsIterator.hasNext()) {
String defArgName = defArgsIterator.next();
PExpression callArg = callArgsIterator.next();
callArg.apply(this);
// Προσδιορισμός του τύπου του ορίσματος κλήσης
String valueType = tableFillVisitor.determineExpressionType(callArg);
defArgsCallArgs.put(defArgName, valueType);
function.setMap(defArgsCallArgs);
}
}
//stack.add(defArgsCallArgs);
stackFunctions.add(function);
}
// Ανάκτηση του τύπου της έκφρασης επιστροφής της συνάρτησης
function.getStatement().apply(this);
}
outAFunctionCall(node);
}
//function to check if an argument is defined in the variables table
private void checkIfExpressionIsDefined(PExpression exp, int line){
if(exp instanceof AIdentifierExpression){
AIdentifier id = (AIdentifier) ((AIdentifierExpression) exp).getIdentifier();
String argName = id.getId().toString().trim();
if(!variableTable.containsKey(argName)){
System.err.println("!!! Error in line " + line + ": Variable: '" + argName + "' used before declaration.");
ParserTest1.errors+=1;
}
}
}
@Override
public void caseAAdditionExpression(AAdditionExpression node) {
boolean hasFCallExpressions = false;
if (!inFunctionDef) {
inAAdditionExpression(node);
operatorStack.push("Addition");
if((node.getE1() != null ) && (node.getE2() != null))
{
// Επίσκεψη στην αριστερή και δεξιά πλευρά του τελεστή '+'
if( ParserTest1.debug) System.out.println("\n\n***************************\n");
PExpression leftExpr = node.getE1(); // Αριστερή πλευρά
PExpression rightExpr = node.getE2(); // Δεξιά πλευρά
leftExpr.apply(this);
rightExpr.apply(this);
if (!returnStackType.empty()) hasFCallExpressions = true;
if( ParserTest1.debug) System.out.println("Return Types: " + returnStackType);
if( ParserTest1.debug) System.out.println("inFunctionCallStatement: " + inFuncionCallStatement);
String leftType = null;
String rightType = null;
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
HashMap<String, String> defArgsCallArgs = function.getMap();
leftType = determineExpressionType(leftExpr, defArgsCallArgs);
rightType = determineExpressionType(rightExpr, defArgsCallArgs);
if( ParserTest1.debug) System.out.println("IN HERE");
}else{
// Λήψη των τύπων των εκφράσεων
leftType = tableFillVisitor.determineExpressionType(leftExpr);
rightType = tableFillVisitor.determineExpressionType(rightExpr);
}
if( ParserTest1.debug) System.out.println("inFunctionCallStatement: " + inFuncionCallStatement);
if( ParserTest1.debug) {
System.out.println(" set type.");
System.out.println("rightType: "+ rightType);
System.out.println("leftType: "+ leftType);
}
if (!returnStackType.isEmpty()){
if ((rightType.equals("Unknown") || (rightType.equals("Void")))){
rightType = returnStackType.pop();
if( ParserTest1.debug) System.out.println("rightType: "+ rightType);
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
HashMap<String, String> defArgsCallArgs = function.getMap();
defArgsCallArgs.put(rightExpr.toString().trim(), rightType);
}
}
if ((leftType.equals("Unknown") || (leftType.equals("Void")))){
leftType = returnStackType.pop();
if( ParserTest1.debug) System.out.println("leftType: "+ leftType);
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
HashMap<String, String> defArgsCallArgs = function.getMap();
defArgsCallArgs.put(leftExpr.toString().trim(), leftType);
}
}
}
// Έλεγχος αν οι τύποι είναι οι ίδιοι
if (!( ((leftType.equals("Number") && rightType.equals("Double"))) ||
((leftType.equals("Double") && rightType.equals("Number"))) ) &&
(!leftType.equals(rightType))) // Οι τύποι είναι διαφορετικοί. αλλα όχι αριμθοί.
{
System.err.println("\n!!! Error in line "+ globalLine + ": Mismatched types in addition expression. Left side is "
+ leftType + " and right side is " + rightType);
ParserTest1.errors += 1;
} else {
returnStackType.push(leftType);
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
function.setType(leftType);
}
}
if (leftType.equals("None") || rightType.equals("None")) {
System.err.println("\n!!! Error in line " + globalLine + ": Cannot perform addition with None.");
ParserTest1.errors += 1;
}
}
operatorStack.pop();
outAAdditionExpression(node);
} else { // If inside function defintion
inAAdditionExpression(node);
if(node.getE1() != null)
{
node.getE1().apply(this);
}
if(node.getE2() != null)
{
node.getE2().apply(this);
}
outAAdditionExpression(node);
}
}
@Override
public void caseASubtractionExpression(ASubtractionExpression node) {
if (!inFunctionDef) {
inASubtractionExpression(node);
operatorStack.push("Subtraction");
if((node.getE1() != null ) && (node.getE2() != null))
{
// Επίσκεψη στην αριστερή και δεξιά πλευρά του τελεστή '-'
PExpression leftExpr = node.getE1(); // Αριστερή πλευρά
PExpression rightExpr = node.getE2(); // Δεξιά πλευρά
String leftType = null;
String rightType = null;
leftExpr.apply(this);
rightExpr.apply(this);
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
HashMap<String, String> defArgsCallArgs = function.getMap();
leftType = determineExpressionType(leftExpr, defArgsCallArgs);
rightType = determineExpressionType(rightExpr, defArgsCallArgs);
}else{
// Λήψη των τύπων των εκφράσεων
leftType = tableFillVisitor.determineExpressionType(leftExpr);
rightType = tableFillVisitor.determineExpressionType(rightExpr);
}
if (!returnStackType.isEmpty()){
if ((rightType.equals("Unknown") || (rightType.equals("Void")))){
rightType = returnStackType.pop();
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
HashMap<String, String> defArgsCallArgs = function.getMap();
defArgsCallArgs.put(rightExpr.toString().trim(), rightType);
}
}
if ((leftType.equals("Unknown") || (leftType.equals("Void")))){
leftType = returnStackType.pop();
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
HashMap<String, String> defArgsCallArgs = function.getMap();
defArgsCallArgs.put(leftExpr.toString().trim(), leftType);
}
}
}
// Η πράξη είναι λάθος όταν:
if ((!(((leftType.equals("Number") && rightType.equals("Double"))) || // Αν ένας είναι Number και ο άλλος Double, είναι αποδεκτό
((leftType.equals("Double") && rightType.equals("Number")))) && // Αν ένας είναι Double και ο άλλος Number, είναι αποδεκτό
(!leftType.equals(rightType))) || // Αν οι τύποι είναι διαφορετικοί και δεν είναι αριθμοί, απόρριψη.
(leftType.equals(rightType) && (!leftType.equals("Number")) && (!leftType.equals("Double")))) // Οι τύποι είναι διαφορετικοί.
{
ParserTest1.errors += 1;
System.err.println("!!! Error in line "+ globalLine + " Mismatched types in subtraction expression. Left side is "
+ leftType + " and right side is " + rightType);
} else {
returnStackType.push(leftType);
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
function.setType(leftType);
}
}
if (leftType.equals("None") || rightType.equals("None")) {
System.err.println("\n!!! Error in line " + globalLine + ": Cannot perform subtraction with None.");
ParserTest1.errors += 1;
}
}
operatorStack.pop();
outASubtractionExpression(node);
} else { // If inside function defintion
inASubtractionExpression(node);
if(node.getE1() != null)
{
node.getE1().apply(this);
}
if(node.getE2() != null)
{
node.getE2().apply(this);
}
outASubtractionExpression(node);
}
}
@Override
public void caseAMultExpression(AMultExpression node) {
if(!inFunctionDef) {
inAMultExpression(node);
operatorStack.push("Multiplication");
if((node.getE1() != null ) && (node.getE2() != null))
{
// Επίσκεψη στην αριστερή και δεξιά πλευρά του τελεστή '*'
PExpression leftExpr = node.getE1(); // Αριστερή πλευρά
PExpression rightExpr = node.getE2(); // Δεξιά πλευρά
String leftType = null;
String rightType = null;
leftExpr.apply(this);
rightExpr.apply(this);
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
//HashMap<String, String> defArgsCallArgs = stack.peek();
HashMap<String, String> defArgsCallArgs = function.getMap();
leftType = determineExpressionType(leftExpr, defArgsCallArgs);
rightType = determineExpressionType(rightExpr, defArgsCallArgs);
}else{
// Λήψη των τύπων των εκφράσεων
leftType = tableFillVisitor.determineExpressionType(leftExpr);
rightType = tableFillVisitor.determineExpressionType(rightExpr);
}
if (!returnStackType.isEmpty()){
if ((rightType.equals("Unknown") || (rightType.equals("Void")))){
rightType = returnStackType.pop();
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
HashMap<String, String> defArgsCallArgs = function.getMap();
defArgsCallArgs.put(rightExpr.toString().trim(), rightType);
}
}
if ((leftType.equals("Unknown") || (leftType.equals("Void")))){
leftType = returnStackType.pop();
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
HashMap<String, String> defArgsCallArgs = function.getMap();
defArgsCallArgs.put(leftExpr.toString().trim(), leftType);
}
}
}
// Έλεγχος αν οι τύποι είναι οι ίδιοι
if ((!((leftType.equals("Number") && rightType.equals("Double")) || // Αν ένας είναι Number και ο άλλος Double, είναι αποδεκτό
(leftType.equals("Double") && rightType.equals("Number")) ||
(leftType.equals("Number") && rightType.equals("String")) ||
(leftType.equals("String") && rightType.equals("Number"))) && // Αν ένας είναι Double και ο άλλος Number, είναι αποδεκτό
(!leftType.equals(rightType)))
|| // Αν οι τύποι είναι διαφορετικοί και δεν είναι αριθμοί, απόρριψη.
(leftType.equals(rightType) && (!leftType.equals("Number")) && (!leftType.equals("Double")))) // Οι τύποι είναι διαφορετικοί.
{
ParserTest1.errors += 1;
System.err.println("!!! Error in line "+ globalLine + " Mismatched types in multiplication expression. Left side is "
+ leftType + " and right side is " + rightType);
} else {
returnStackType.push(leftType);
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
function.setType(leftType);
}
}
if (leftType.equals("None") || rightType.equals("None")) {
System.err.println("\n!!! Error in line " + globalLine + ": Cannot perform multiplication with None.");
ParserTest1.errors += 1;
}
}
operatorStack.pop();
outAMultExpression(node);
} else { // If inside function defintion
inAMultExpression(node);
if(node.getE1() != null)
{
node.getE1().apply(this);
}
if(node.getE2() != null)
{
node.getE2().apply(this);
}
outAMultExpression(node);
}
}
@Override
public void caseADivExpression(ADivExpression node) {
if (!inFunctionDef) {
inADivExpression(node);
operatorStack.push("Division");
if((node.getE1() != null ) && (node.getE2() != null))
{
// Επίσκεψη στην αριστερή και δεξιά πλευρά του τελεστή '/'
PExpression leftExpr = node.getE1(); // Αριστερή πλευρά
PExpression rightExpr = node.getE2(); // Δεξιά πλευρά
String leftType = null;
String rightType = null;
leftExpr.apply(this);
rightExpr.apply(this);
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
HashMap<String, String> defArgsCallArgs = function.getMap();
leftType = determineExpressionType(leftExpr, defArgsCallArgs);
rightType = determineExpressionType(rightExpr, defArgsCallArgs);
}else{
// Λήψη των τύπων των εκφράσεων
leftType = tableFillVisitor.determineExpressionType(leftExpr);
rightType = tableFillVisitor.determineExpressionType(rightExpr);
}
if (!returnStackType.isEmpty()){
if ((rightType.equals("Unknown") || (rightType.equals("Void")))){
rightType = returnStackType.pop();
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
HashMap<String, String> defArgsCallArgs = function.getMap();
defArgsCallArgs.put(rightExpr.toString().trim(), rightType);
}
}
if ((leftType.equals("Unknown") || (leftType.equals("Void")))){
leftType = returnStackType.pop();
if (!stackFunctions.isEmpty()){
SymFunction function = stackFunctions.peek();
HashMap<String, String> defArgsCallArgs = function.getMap();