Skip to content

Commit a50ef4d

Browse files
committed
compilation: add SameFunctionConstructorsDefined exception and modify the testCompilation.java and testCompilatgionException.java to test the exception. And modify addConstructorsToClassDescriptors to permit two constructors have the same name but diffrent parameters
1 parent 6e1be1b commit a50ef4d

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

phase2/Compiling/Compilation.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ open Hashtbl
33

44
exception ParentClassNotDefined of string
55
exception SameFunctionAlreadyDefined of string
6+
exception SameFunctionConstructorsDefined of string
67

78
type classDescriptor =
89
{
@@ -115,7 +116,14 @@ let addMethodsToClassDesciptor className methods cmethod =
115116
end
116117

117118
let addConstructorsToClassDesciptor constructorsClass constructor =
118-
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
119127

120128
let addMethodsToClassDesciptorFromParent classTable methodsClass c =
121129
if (Hashtbl.mem classTable c.cparent.tid) = true
@@ -181,6 +189,7 @@ let rec compileClass methodTable classTable ast asttype =
181189
addToMethodTable methodTable asttype.id c;
182190
with
183191
| SameFunctionAlreadyDefined str -> raise(SameFunctionAlreadyDefined(str))
192+
| SameFunctionConstructorsDefined str -> raise(SameFunctionConstructorsDefined(str))
184193
| _ -> raise(ParentClassNotDefined(c.cparent.tid))
185194
end
186195
end

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
}

phase2/tests/testCompilationException.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,33 @@ class A {
1010
public void f1(){
1111
}
1212

13+
//test same function exception
1314
public void f1(int b){
1415
}
1516

1617
public void f1(int d){
1718
}
1819

20+
public void f1(int d,int c){
21+
}
22+
23+
//test constructor exception
1924
public A(int num)
2025
{
2126
int a = num;
2227
}
2328

29+
public A(int num,int b)
30+
{
31+
int a = num;
32+
}
33+
34+
public A(int num1)
35+
{
36+
int a = num;
37+
}
38+
39+
2440

2541
}
2642

0 commit comments

Comments
 (0)