-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArgs.java
70 lines (49 loc) · 1.82 KB
/
Args.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
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
class Args extends SuperToken implements Token{
Expr expr;
Args args;
public Args(Expr expr, Args args){
this.expr = expr;
this.args = args;
}
public Args(){
this.expr = null;
this.args = null;
}
public Args(Expr expr){
this.expr = expr;
this.args = null;
}
public String toString(int t){
if (args == null && expr == null)
return "";
return expr.toString(t) + (args == null ? "" : (", " + args.toString(t)));
}
public void typeCheck(String fnName, List<VarType> methodArgs) throws Exception{
List<VarType> argTypes = new ArrayList<>();
gatherTypesFromCall(argTypes);
if (argTypes.size() != methodArgs.size()){
System.out.println("❌ Fatal error: Function " + fnName + " expected " + methodArgs.size() + " arguments, " + argTypes.size() + " provided");
System.exit(0);
}
for (int i=0; i < methodArgs.size(); i++){
VarType expectedArg = methodArgs.get(i);
VarType actualArg = argTypes.get(i);
if (!isCoercible(expectedArg, actualArg)){
System.out.println("❌ Fatal error: Expected argument of type " + expectedArg + " in position " + i + " of function " + fnName + ", " + actualArg + " provided");
System.exit(0);
}
}
}
private void gatherTypesFromCall(List<VarType> argTypes) throws Exception {
if (expr != null) {
VarType exprType = expr.typeCheck();
argTypes.add(exprType);
if (args != null && args.expr != null)
args.gatherTypesFromCall(argTypes);
}
}
}