-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionSymbol.java
134 lines (109 loc) · 5.25 KB
/
FunctionSymbol.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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class FunctionSymbol extends Symbol {
TypeSymbol returnType;
Map<String, Symbol> args; //Check this later.
List<ClassDeclSymbol> overrides;
public FunctionSymbol(String id, TypeSymbol returnType) {
super(id, PrimitiveType.IDENTIFIER);
args = new LinkedHashMap<String, Symbol>();
overrides = new ArrayList<ClassDeclSymbol>();
this.returnType = returnType;
}
public FunctionSymbol(String id, TypeSymbol returnType, Map<String, Symbol> args) {
super(id, PrimitiveType.IDENTIFIER);
overrides = new ArrayList<ClassDeclSymbol>();
this.returnType = returnType;
this.args = args;
}
public FunctionSymbol(String id, String returnType) {
super(id, PrimitiveType.IDENTIFIER);
args = new LinkedHashMap<String, Symbol>();
overrides = new ArrayList<ClassDeclSymbol>();
PrimitiveType type = PrimitiveType.strToPrimitiveType(returnType);
if(type == PrimitiveType.IDENTIFIER){
this.returnType = new TypeSymbol(returnType);
} else {
this.returnType = new TypeSymbol(type);
}
}
public FunctionSymbol(String id, String returnType, Map<String, Symbol> args) {
super(id, PrimitiveType.IDENTIFIER);
this.args = args;
overrides = new ArrayList<ClassDeclSymbol>();
PrimitiveType type = PrimitiveType.strToPrimitiveType(returnType);
if(type == PrimitiveType.IDENTIFIER){
this.returnType = new TypeSymbol(returnType);
} else {
this.returnType = new TypeSymbol(type);
}
}
public boolean checkOverride(FunctionSymbol override) throws Exception{
if(this.returnType.type != override.returnType.type){
throw new Exception("Declared method has a different return type than the superclass");
} else if(this.returnType.type == PrimitiveType.IDENTIFIER){
if(!this.returnType.id.equals(override.returnType.id)){
throw new Exception("Declared method has a different type of args than the superclass");
}
}
return checkArgsDecl(override.args);
}
private boolean checkArgsDecl(Map<String,Symbol> checkArgs) throws Exception {
Symbol[] argsArray = Arrays.copyOf(args.values().toArray(), args.size(), Symbol[].class);
Symbol[] checkArgsArray = Arrays.copyOf(checkArgs.values().toArray(), checkArgs.size(), Symbol[].class);
if(argsArray.length != checkArgsArray.length){
throw new Exception("Declared method has a different number of args than the superclass");
}
for(int i=0;i<argsArray.length;i++){
if(argsArray[i].type != checkArgsArray[i].type){
throw new Exception("Declared method has a different type of args than the superclass");
} else if(argsArray[i].type == PrimitiveType.IDENTIFIER){
ClassSymbol class1 = (ClassSymbol)argsArray[i];
ClassSymbol class2 = (ClassSymbol)checkArgsArray[i];
if(!class1.className.equals(class2.className)){
throw new Exception("Declared method has a different type of args than the superclass");
}
}
}
return true;
}
public boolean checkArgs(Map<String,Symbol> checkArgs, SymbolTable table) throws Exception {
Symbol[] argsArray = Arrays.copyOf(args.values().toArray(), args.size(), Symbol[].class);
TypeSymbol[] checkArgsArray = Arrays.copyOf(checkArgs.values().toArray(), checkArgs.size(), TypeSymbol[].class);
if(argsArray.length != checkArgsArray.length){
throw new Exception("Method call " + this.id + " has a different number of args than declared. Expected: " + argsArray.length + " but found: " + checkArgsArray.length);
}
for(int i=0;i<argsArray.length;i++){
if(argsArray[i].type != checkArgsArray[i].type){
throw new TypeException(argsArray[i].type.typeName, checkArgsArray[i].type.typeName);
// throw new Exception("Declared method has a different type of args than the superclass");
} else if(argsArray[i].type == PrimitiveType.IDENTIFIER){
ClassSymbol class1 = (ClassSymbol)argsArray[i];
ClassDeclSymbol class2 = table.lookupType(checkArgsArray[i].getTypeName());
if(!class2.isInstanceOf(class1)){
throw new Exception("Type " + class2.id + " not instance of " + class1.className);
// throw new Exception("Declared method has a different type of args than the superclass");
}
}
}
return true;
}
// @Override
// public String toString() {
// // TODO Auto-generated method stub
// String ret;
// if(args.size() != 0){
// ret = returnType.getTypeName() + " " + id + "(";
// for(Symbol s:args.values()){
// ret += s.toString();
// }
// ret += ")";
// } else {
// ret = returnType.getTypeName() + " " + id + "()";
// }
// return ret;
// }
}