-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyvisitor.java
147 lines (126 loc) · 3.95 KB
/
myvisitor.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
import minipython.analysis.*;
import minipython.node.*;
import java.util.*;
public class myvisitor extends DepthFirstAdapter
{
private Hashtable symtable;
private List<AFunction> functions;
myvisitor(Hashtable symtable, List<AFunction> functions)
{
this.symtable = symtable;
this.functions = functions;
}
public void inAFunction(AFunction node)
{
TId id = ((TId)((AIdExpression) node.getExpression()).getId());
String fName = id.toString();
int line = id.getLine();
int [] minMax = getMinMax(node);
int minArgs = minMax[0];
int maxArgs = minMax[1];
boolean exists = false;
boolean error = false;
for(AFunction fun:functions)
{
fun = (AFunction) fun.clone();
if(((AIdExpression)fun.getExpression()).getId().toString().equals(fName))
{
if(!checkArgSize(line, minArgs, fun))
{
System.out.println("Line " + line + ": " +" Function " + fName +" is already defined");
error = true;
}
else if(!checkArgSize(line, maxArgs, fun))
{
System.out.println("Line " + line + ": " +" Function " + fName +" is already defined");
error = true;
}
exists = true;
}
}
if(!exists || !error && exists)
{
functions.add(node);
}
}
public void inAArgument(AArgument node)
{
AArgument copyArg = (AArgument)node.clone();
TId id = ((TId)((AIdExpression) copyArg.getExpression()).getId());
String fName = id.toString();
if(copyArg.getEqualsValue().size()!=0)
{
AEqualsValue val = (AEqualsValue)copyArg.getEqualsValue().get(0);
AEqualsStatement moreId = new AEqualsStatement(new AIdExpression(id), ((PExpression)val.getExpression()));
symtable.put(fName, moreId);
}
else
{
AEqualsStatement moreId = new AEqualsStatement(new AIdExpression(id), new ANoneExpression(new TNone(id.getLine(), id.getPos())));
symtable.put(fName, moreId);
}
}
public void inAMoreIdentifiers(AMoreIdentifiers node)
{
AMoreIdentifiers copyMoreIds = (AMoreIdentifiers)node.clone();
TId id = ((TId)((AIdExpression) copyMoreIds.getExpression()).getId());
String fName = id.toString();
if(copyMoreIds.getEqualsValue().size()!=0)
{
AEqualsValue val = (AEqualsValue)copyMoreIds.getEqualsValue().get(0);
AEqualsStatement moreId = new AEqualsStatement(copyMoreIds.getExpression(), ((PExpression)val.getExpression()));
symtable.put(fName, moreId);
}
else
{
AEqualsStatement moreId = new AEqualsStatement(copyMoreIds.getExpression(), new ANoneExpression(new TNone(id.getLine(), id.getPos())));
symtable.put(fName, moreId);
}
}
public boolean checkArgSize(int line, int numOfArgs, AFunction node)
{
int [] minMax = getMinMax(node);
int minArgs = minMax[0];
int maxArgs = minMax[1];
if(numOfArgs == minArgs || numOfArgs == maxArgs)
{
System.out.println("Line " + line + ": Found " + numOfArgs + " arguments, must be different than {" + minArgs + ", " + maxArgs + "}");
return false;
}
return true;
}
public int[] getMinMax(AFunction node)
{
int[] minMax = new int[2];
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;
minMax[0] = minArgs;
minMax[1] = maxArgs;
return minMax;
}
}