Skip to content

Commit 6e1be1b

Browse files
committed
compilation: add ParentClassNotDefined and SameFunctionAlreadyDefined and add testCompilationException.java file for test the exception
1 parent eba5de9 commit 6e1be1b

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

phase2/Compiling/Compilation.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ let addMethodsToMethodTable className methodTable cmethod =
9191
Hashtbl.add methodTable nameMethod cmethod
9292
end
9393
else begin
94-
print_endline("function " ^ cmethod.mname ^ " already in the method table")
94+
raise(SameFunctionAlreadyDefined(nameMethod ^ " already defined in method Table "))
9595
end
9696

9797

@@ -111,7 +111,7 @@ let addMethodsToClassDesciptor className methods cmethod =
111111
Hashtbl.add methods nameKey nameMethod
112112
end
113113
else begin
114-
print_endline("function " ^ cmethod.mname ^ " already defined")
114+
raise(SameFunctionAlreadyDefined(nameKey ^ " already defined in ClassDesciptor " ^ className ))
115115
end
116116

117117
let addConstructorsToClassDesciptor constructorsClass constructor =
@@ -180,6 +180,7 @@ let rec compileClass methodTable classTable ast asttype =
180180
addToClassTable classTable asttype.id c;
181181
addToMethodTable methodTable asttype.id c;
182182
with
183+
| SameFunctionAlreadyDefined str -> raise(SameFunctionAlreadyDefined(str))
183184
| _ -> raise(ParentClassNotDefined(c.cparent.tid))
184185
end
185186
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class B extends A{
2+
public void f1(){
3+
}
4+
}
5+
6+
class A {
7+
8+
int a = 5;
9+
String b ="abc";
10+
public void f1(){
11+
}
12+
13+
public void f1(int b){
14+
}
15+
16+
public void f1(int d){
17+
}
18+
19+
public A(int num)
20+
{
21+
int a = num;
22+
}
23+
24+
25+
}
26+
27+
28+
class C {
29+
30+
}

0 commit comments

Comments
 (0)