8000 compilation: change the method name to admit functions who have same … · compiler-experts/miniJava@eba5de9 · GitHub
[go: up one dir, main page]

Skip to content

Commit eba5de9

Browse files
committed
compilation: change the method name to admit functions who have same name but diffrent parameters
1 parent 94f8475 commit eba5de9

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

phase2/Compiling/Compilation.ml

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
open AST
22
open Hashtbl
33

4-
4+
exception ParentClassNotDefined of string
5+
exception SameFunctionAlreadyDefined of string
56

67
type classDescriptor =
78
{
@@ -84,7 +85,7 @@ let printClassTable classTable =
8485
*)
8586

8687
let addMethodsToMethodTable className methodTable cmethod =
87-
let nameMethod = className ^ "_" ^ cmethod.mname in
88+
let nameMethod = className ^ "_" ^ cmethod.mname ^ "_" ^ (ListII.concat_map "," stringOf_argType cmethod.margstype) in
8889
if(verifyHashtbl methodTable nameMethod) = false
8990
then begin
9091
Hashtbl.add methodTable nameMethod cmethod
@@ -102,10 +103,12 @@ let addToMethodTable methodTable className c =
102103
ulity: add methods, constructors,attributes in the Hashtbl classTable
103104
*)
104105
let addMethodsToClassDesciptor className methods cmethod =
105-
if(verifyHashtbl methods cmethod.mname) = false
106+
let nameKey = cmethod.mname ^ "_" ^ (ListII.concat_map "," stringOf_argType cmethod.margstype) in
107+
108+
if(verifyHashtbl methods nameKey) = false
106109
then begin
107-
let nameMethod = className ^ "_" ^ cmethod.mname in
108-
Hashtbl.add methods cmethod.mname nameMethod
110+
let nameMethod = className ^ "_" ^ cmethod.mname ^ "_" ^ (ListII.concat_map "," stringOf_argType cmethod.margstype) in
111+
Hashtbl.add methods nameKey nameMethod
109112
end
110113
else begin
111114
print_endline("function " ^ cmethod.mname ^ " already defined")
@@ -155,9 +158,11 @@ let addToClassTable classTable className c =
155158
(*asttype ={ mutable modifiers : modifier list; id : string; info : type_info;}
156159
ulity: add class and methods in the Hashtbl
157160
*)
158-
let rec findParentClass cname typelist = match typelist with
161+
let rec findParentClass cname typelist =
162+
match typelist with
159163
| head::liste -> if head.id = cname then head else findParentClass cname liste
160164

165+
161166
let rec compileClass methodTable classTable ast asttype =
162167
match asttype.info with
163168
| Class c -> if(verifyHashtbl classTable asttype.id) = false
@@ -169,10 +174,13 @@ let rec compileClass methodTable classTable ast asttype =
169174
end
170175
else
171176
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
177+
try
178+
let parenttype = findParentClass c.cparent.tid ast.type_list in
179+
compileClass methodTable classTable ast parenttype;
180+
addToClassTable classTable asttype.id c;
181+
addToMethodTable methodTable asttype.id c;
182+
with
183+
| _ -> raise(ParentClassNotDefined(c.cparent.tid))
176184
end
177185
end
178186

0 commit comments

Comments
 (0)
0