Skip to content

Commit bede4f3

Browse files
committed
Merge branch 'compilation-add-exception'
merge to add compilation exception to master
2 parents f45ac9b + a50ef4d commit bede4f3

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

phase2/Compiling/Compilation.ml

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
open AST
22
open Hashtbl
33

4-
4+
exception ParentClassNotDefined of string
5+
exception SameFunctionAlreadyDefined of string
6+
exception SameFunctionConstructorsDefined of string
57

68
type classDescriptor =
79
{
@@ -84,13 +86,13 @@ let printClassTable classTable =
8486
*)
8587

8688
let addMethodsToMethodTable className methodTable cmethod =
87-
let nameMethod = className ^ "_" ^ cmethod.mname in
89+
let nameMethod = className ^ "_" ^ cmethod.mname ^ "_" ^ (ListII.concat_map "," stringOf_argType cmethod.margstype) in
8890
if(verifyHashtbl methodTable nameMethod) = false
8991
then begin
9092
Hashtbl.add methodTable nameMethod cmethod
9193
end
9294
else begin
93-
print_endline("function " ^ cmethod.mname ^ " already in the method table")
95+
raise(SameFunctionAlreadyDefined(nameMethod ^ " already defined in method Table "))
9496
end
9597

9698

@@ -102,17 +104,26 @@ let addToMethodTable methodTable className c =
102104
ulity: add methods, constructors,attributes in the Hashtbl classTable
103105
*)
104106
let addMethodsToClassDesciptor className methods cmethod =
105-
if(verifyHashtbl methods cmethod.mname) = false
107+
let nameKey = cmethod.mname ^ "_" ^ (ListII.concat_map "," stringOf_argType cmethod.margstype) in
108+
109+
if(verifyHashtbl methods nameKey) = false
106110
then begin
107-
let nameMethod = className ^ "_" ^ cmethod.mname in
108-
Hashtbl.add methods cmethod.mname nameMethod
111+
let nameMethod = className ^ "_" ^ cmethod.mname ^ "_" ^ (ListII.concat_map "," stringOf_argType cmethod.margstype) in
112+
Hashtbl.add methods nameKey nameMethod
109113
end
110114
else begin
111-
print_endline("function " ^ cmethod.mname ^ " already defined")
115+
raise(SameFunctionAlreadyDefined(nameKey ^ " already defined in ClassDesciptor " ^ className ))
112116
end
113117

114118
let addConstructorsToClassDesciptor constructorsClass constructor =
115-
Hashtbl.add constructorsClass (ListII.concat_map "," stringOf_argType constructor.cargstype) constructor
119+
let args = (ListII.concat_map "," stringOf_argType constructor.cargstype) in
120+
if(verifyHashtbl constructorsClass args) = false
121+
then begin
122+
Hashtbl.add constructorsClass args constructor
123+
end
124+
else begin
125+
raise(SameFunctionConstructorsDefined(args ^ " type constructor already defined in ClassDesciptor " ))
126+
end
116127

117128
let addMethodsToClassDesciptorFromParent classTable methodsClass c =
118129
if (Hashtbl.mem classTable c.cparent.tid) = true
@@ -155,9 +166,11 @@ let addToClassTable classTable className c =
155166
(*asttype ={ mutable modifiers : modifier list; id : string; info : type_info;}
156167
ulity: add class and methods in the Hashtbl
157168
*)
158-
let rec findParentClass cname typelist = match typelist with
169+
let rec findParentClass cname typelist =
170+
match typelist with
159171
| head::liste -> if head.id = cname then head else findParentClass cname liste
160172

173+
161174
let rec compileClass methodTable classTable ast asttype =
162175
match asttype.info with
163176
| Class c -> if(verifyHashtbl classTable asttype.id) = false
@@ -169,10 +182,15 @@ let rec compileClass methodTable classTable ast asttype =
169182
end
170183
else
171184
begin
172-
let parenttype = findParentClass c.cparent.tid ast.type_list in
173-
compileClass methodTable classTable ast parenttype;
174-
addToClassTable classTable asttype.id c;
175-
addToMethodTable methodTable asttype.id c
185+
try
186+
let parenttype = findParentClass c.cparent.tid ast.type_list in
187+
compileClass methodTable classTable ast parenttype;
188+
addToClassTable classTable asttype.id c;
189+
addToMethodTable methodTable asttype.id c;
190+
with
191+
| SameFunctionAlreadyDefined str -> raise(SameFunctionAlreadyDefined(str))
192+
| SameFunctionConstructorsDefined str -> raise(SameFunctionConstructorsDefined(str))
193+
| _ -> raise(ParentClassNotDefined(c.cparent.tid))
176194
end
177195
end
178196

phase2/tests/testCompilation.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,35 @@ public void f1(){
66
class A {
77

88
int a = 5;
9-
Sttring b ="abc";
9+
String b ="abc";
1010
public void f1(){
1111
}
12+
13+
public void f1(int b,int c){
14+
}
15+
16+
public void f1(int d){
17+
}
18+
1219
public void f2(){
1320
}
1421

1522
public A(int num)
1623
{
17-
a = num;
24+
int a = num;
1825
}
1926

20-
public A(int num,String name)
27+
28+
public A(int num1,int num2)
2129
{
22-
a = num;
30+
int a = num;
2331
}
2432

33+
34+
}
35+
36+
37+
38+
class C {
39+
2540
}

0 commit comments

Comments
 (0)