8000 Merge branch 'Typing-check-NewArray' · compiler-experts/miniJava@29f7958 · GitHub
[go: up one dir, main page]

Skip to content

Commit 29f7958

Browse files
committed
Merge branch 'Typing-check-NewArray'
2 parents 9c3d81a + 59e521d commit 29f7958

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

phase2/Typing/TypeError.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ exception ConstructorAlreadyExists of string
1010
exception DuplicateLocalVariable of string
1111
exception IncompatibleTypes of string
1212
exception InvalidMethodDeclaration of string
13-
exception InvalidTypeCondOperation of Type.t * Type.t
13+
exception InvalidTypeCondOperation of string * string
1414
exception MethodAlreadyExists of string
1515
exception UnknownAttribute of string * string
1616
exception UnknownActualType of string
@@ -20,6 +20,6 @@ exception UnknownVariable of string
2020
exception WrongTypePrefixOperation of string * string
2121
exception WrongTypePostfixOperation of string
2222
exception WrongInvokedArgumentsLength of string
23-
exception WrongTypesAssignOperation of Type.t option * Type.t option
24-
exception WrongTypeCondOperation of Type.t option
25-
exception WrongTypesOperation of Type.t option * Type.t option
23+
exception WrongTypesAssignOperation of string * string * string
24+
exception WrongTypeCondOperation of string
25+
exception WrongTypesOperation of string * string * string

phase2/Typing/Typing.ml

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ type class_env = {
2222
parent : Type.ref_type;
2323
}
2424

25+
(* print Type.t option *)
26+
let stringOfOpt s =
27+
match s with
28+
| None -> "null"
29+
| Some t -> stringOf t
30+
2531
(* auxiliary print functions *)
2632
let print_arguments tab arguments =
2733
List.iter (fun a -> print_string(tab ^ AST.stringOf_arg a)) arguments
@@ -92,10 +98,10 @@ let verify_name s env current_env =
9298
else Some(Hashtbl.find current_env.variables s)
9399

94100
(* check the type of the assignment operation *)
95-
let verify_assignop_type t1 t2 =
101+
let verify_assignop_type t1 t2 op =
96102
if t1 <> t2 then
97103
begin
98-
raise(WrongTypesAssignOperation(t1, t2));
104+
raise(WrongTypesAssignOperation(stringOfOpt t1, string_of_assign_op op ,stringOfOpt t2));
99105
match t1 with
100106
| Some t ->
101107
print_string "\n************************\n";
@@ -114,7 +120,7 @@ let verify_assignop_type t1 t2 =
114120
let verify_operation_type t1 op t2 =
115121
if t1 <> t2 then
116122
begin
117-
raise(WrongTypesOperation(t1, t2));
123+
raise(WrongTypesOperation(stringOfOpt t1, string_of_infix_op op, stringOfOpt t2));
118124
end
119125

120126
(* check the type of call expression when it existe the method name, the arguments and global env *)
@@ -135,6 +141,8 @@ let verify_call_expr meth_name args env class_name =
135141
| WrongInvokedArgumentsLength s -> raise(WrongInvokedArgumentsLength(s))
136142
end
137143

144+
145+
138146
(* check the type of the expressions *)
139147
let rec verify_expression env current_env e =
140148
(* print_string(string_of_expression_desc(e.edesc)); *)
@@ -166,7 +174,7 @@ let rec verify_expression env current_env e =
166174
| WrongInvokedArgumentsLength s -> raise(WrongInvokedArgumentsLength(s))
167175
end
168176
end
169-
(* | NewArray (Some n1,n2,al) -> () (*TODO*) *)
177+
| NewArray (t,args,target) -> e.etype <- Some(Array(t, List.length args))
170178
| Call (r,m,al) ->
171179
(match r with
172180
| None -> (* when method is called without declaring class name*)
@@ -203,7 +211,7 @@ let rec verify_expression env current_env e =
203211
| AssignExp (e1,op,e2) ->
204212
verify_expression env current_env e1;
205213
verify_expression env current_env e2;
206-
verify_assignop_type e1.etype e2.etype;
214+
verify_assignop_type e1.etype e2.etype op;
207215
e.etype <- e1.etype
208216
| Post (e1,op) ->
209217
verify_expression env current_env e1;
@@ -243,11 +251,11 @@ let rec verify_expression env current_env e =
243251
verify_expression env current_env e3;
244252
(* check if e1.etype is boolean and e2.etype equals e3.etype *)
245253
if e1.etype <> Some(Primitive(Boolean))
246-
then raise(WrongTypeCondOperation(e1.etype))
254+
then raise(WrongTypeCondOperation(stringOfOpt e1.etype))
247255
(match e2.etype, e3.etype with
248256
| Some(_), None -> ()
249257
| None, Some(_) -> ()
250-
| Some(t1), Some(t2) -> if t1 <> t2 then raise(InvalidTypeCondOperation(t1, t2)));
258+
| Some(t1), Some(t2) -> if t1 <> t2 then raise(InvalidTypeCondOperation(stringOf t1, stringOf t2)));
251259
if e2.etype <> None then e.etype <- e2.etype else e.etype <- e3.etype
252260
| Cast (t,e1) ->
253261
verify_expression env current_env e1;
@@ -409,7 +417,7 @@ let verify_attributes envs current_class attrs =
409417
verify_expression envs current_env e;
410418
(* Verify the assignment operation's type is coherent *)
411419
let mytype = Some(attrs.atype) in
412-
verify_assignop_type mytype e.etype
420+
verify_assignop_type mytype e.etype AST.Assign
413421
| None -> ()
414422

415423
(* check the type of the class *)

phase2/tests/test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public I(int e) {
2020
long l = (long) sr;
2121
boolean test = my instanceof I;
2222
int condop = (1 == 2) ? 1 : 5;
23+
int[] arr = new int[5];
2324
private String func1 (int a) {
2425
int b = 1;
2526
b += (5+4);

0 commit comments

Comments
 (0)
0