-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisitor2.java
105 lines (92 loc) · 3.81 KB
/
Visitor2.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
import minipython.analysis.*;
import minipython.node.*;
import java.util.*;
public class Visitor2 extends DepthFirstAdapter
{
private Hashtable symtable;
private Hashtable symtable_var;
private Error error;
Visitor2(Hashtable symtable, Hashtable symtable_var)
{
error = Error.getInstance();
this.symtable = symtable;
this.symtable_var = symtable_var;
}
//Find out if this function we are calling exists
public void inAFunctioncall (AFunctioncall node)
{
boolean errorOccurred = false;
String fName = node.getId().toString();
int line = ((TId) node.getId()).getLine();
// checking if the function call has ever been declared
if(symtable.containsKey(fName)){
LinkedList args = node.getArglist();
AFunction other = (AFunction)symtable.get(fName);
LinkedList other_args = other.getArgument();
//System.out.println("here");
//otan perimenei orismata kai emeis dinoume ligotera i perissotera
if(other_args.size() != 0 && args.size() != 0)
{
//prepei na metrisoume posa arguments 8eloume
int other_args_length = 1; // 3eroume oti arxizei apo 1
AArgument arg1 = (AArgument) other_args.get(0);
other_args_length += arg1.getCommaid().size();
//System.out.println(other_args_length);
//prepei na metrisoume posa arguments dinoume
int args_length = 1; // panta arxizei apo ena (afou periexei ena expression panta)
args_length += ((AArglist) args.get(0)).getCommaexp().size();
// System.out.println(args_length);
if(args_length > other_args_length){
errorOccurred = error.printError("Line " + line + ": " +" FunctionCall " + fName + "the number of parametes doesn't much the number of arguments","aek2");
return;
}
//gia na broume ta default parameters stis sinartiseis pou exoume orisei
int index_of_first_defaultParam_on_other_args = 0;
if (arg1.getEqvalue().size() == 0){
index_of_first_defaultParam_on_other_args = 1;
LinkedList comma_ids = arg1.getCommaid();
for(int i = 0; i < comma_ids.size(); i++){
LinkedList eqval = ((ACommaid)comma_ids.get(i)).getEqvalue();
if(eqval.size() == 0){
index_of_first_defaultParam_on_other_args++;
break;
}
}
}
//System.out.println(index_of_first_defaultParam_on_other_args);
if(!(args_length <= other_args_length && args_length >= index_of_first_defaultParam_on_other_args)) // checking how many parameters can have as an input without the default parameters
{
errorOccurred = error.printError("Line " + line + ": " +" FunctionCall " + fName +"the number of parametes doesn't much the number of arguments","aek3");
return;
}
}else if (other_args.size() != 0 && args.size() == 0)
{
AArgument arg1 = (AArgument) other_args.get(0);
int countNoneDefault = 0;
if (arg1.getEqvalue().size() == 0)
countNoneDefault++;
LinkedList comma_ids = arg1.getCommaid();
for(int i = 0; i < comma_ids.size(); i++){
LinkedList eqval = ((ACommaid)comma_ids.get(i)).getEqvalue();
if(eqval.size() == 0){
countNoneDefault++;
}
}
if (countNoneDefault != 0)
{
errorOccurred = error.printError("Line " + line + ": " +" FunctionCall " + fName +"the number of parametes doesn't much the number of arguments","aek4");
}
}else if (other_args.size() == 0 && args.size() != 0)
{
errorOccurred = error.printError("Line " + line + ": " +" FunctionCall " + fName +"the number of parametes doesn't much the number of arguments","aek5");
}
}else
{
errorOccurred = error.printError("Line " + line + ": " +" FunctionCall " + fName + "is not defined ", "aek42");
}
}
public int getErrorCount()
{
return error.getErrorCount();
}
}