8000 Merge branch 'Typing-add-for-statement-checking' · compiler-experts/miniJava@ae247bc · GitHub
[go: up one dir, main page]

Skip to content

Commit ae247bc

Browse files
committed
Merge branch 'Typing-add-for-statement-checking'
2 parents f092a41 + 9e71e15 commit ae247bc

File tree

2 files changed

+81
-20
lines changed

2 files changed

+81
-20
lines changed

phase2/Typing/Typing.ml

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,42 @@ let add_local_variable current_env id t =
230230
then Hashtbl.add current_env.variables id t
231231
else raise(DuplicateLocalVariable((Type.stringOf t)^" "^id))
232232

233+
(* check if actual type matches with declared type
234+
Parameters:
235+
- e is of expression: the expression contains the actual type
236+
- t is of Type.t: the declared type
237+
- id if of string: the id for the variable in question
238+
- current_env: the local definition environment
239+
Function:
240+
- if actual type matches declared type:
241+
8000 add variable id and its type to the current_env
242+
- if e.type is None or type donesn't match:
243+
raise UnknowActualType exception and IncompatibleTypes
244+
exception respectively *)
245+
let verify_actual_type_with_declared_type e t id current_env =
246+
let s = string_of_expression_desc e.edesc in
247+
match e.etype with
248+
| None -> raise(UnknowActualType(" "^s^" don't have type information"));
249+
(* check int i = 2, int j = i *)
250+
| Some actual_t -> (
251+
if actual_t <> t (*actual type not equals to declared type*)
252+
then raise(IncompatibleTypes((Type.stringOf actual_t)^" cannnot be converted to "^(Type.stringOf t)^" for "^id))
253+
else add_local_variable current_env id t)
254+
255+
(* check if acutal type matches with boolean
256+
Parameters:
257+
- e is of expression: the expression contains the actual type
258+
- err_msg if of string: the customized exception message
259+
Function:
260+
- if e.type is None or type donesn't match:
261+
raise UnknowActualType exception and IncompatibleTypes
262+
exception respectively *)
263+
let verify_actual_type_with_boolean e err_msg =
264+
match e.etype with
265+
| None -> raise(UnknowActualType("unknow type in if else condition"))
266+
| Some actual_t -> if actual_t <> Primitive(Boolean)
267+
then raise(IncompatibleTypes((stringOf actual_t)^" cannot be converted to boolean"))
268+
233269
let rec verify_statement current_env envs statement =
234270
match statement with
235271
| VarDecl dl ->
@@ -239,15 +275,7 @@ let rec verify_statement current_env envs statement =
239275
| None -> add_local_variable current_env id t
240276
(* check int j = 2; or int j = i;*)
241277
| Some e -> (verify_expression envs current_env e;
242-
let s = string_of_expression_desc e.edesc in
243-
match e.etype with
244-
| None -> print_string ("["^s^"]"); (*TODO*)
245-
(* check int i = 2, int j = i *)
246-
| Some actual_t -> (
247-
if actual_t <> t (*actual type not equals to declared type*)
248-
then raise(IncompatibleTypes((Type.stringOf actual_t)^" cannnot be converted to "^(Type.stringOf t)^" for "^id))
249-
else add_local_variable current_env id t
250-
)
278+
verify_actual_type_with_declared_type e t id current_env
251279
)
252280
);
253281
) dl
@@ -278,18 +306,32 @@ let rec verify_statement current_env envs statement =
278306
| While(e,s) -> () (*TODO*)
279307
| If(e,s,None) -> (verify_expression envs current_env e;
280308
verify_statement current_env envs s;
281-
match e.etype with
282-
| None -> raise(UnknowActualType("unknow type in if condition"))
283-
| Some actual_t -> if actual_t <> Primitive(Boolean)
284-
then raise(IncompatibleTypes((stringOf actual_t)^" cannot be converted to boolean")))
309+
verify_actual_type_with_boolean e "unknow type in if condition")
285310
| If(e,s1,Some s2) -> (verify_expression envs current_env e;
286311
verify_statement current_env envs s1;
287312
verify_statement current_env envs s2;
288-
match e.etype with
289-
| None -> raise(UnknowActualType("unknow type in if else condition"))
290-
| Some actual_t -> if actual_t <> Primitive(Boolean)
291-
then raise(IncompatibleTypes((stringOf actual_t)^" cannot be converted to boolean")))
292-
| For(fil,eo,el,s) -> () (*TODO*)
313+
verify_actual_type_with_boolean e "unknow type in if else condition")
314+
| For(fil,eo,el,s) ->
315+
let for_env = { returntype = current_env.returntype;
316+
variables = Hashtbl.copy current_env.variables;
317+
this_class = current_env.this_class;
318+
env_type = current_env.env_type} in
319+
List.iter (fun (t,id,eo) ->
320+
(match t with
321+
| None -> ()
322+
| Some t -> (match eo with
323+
| None -> add_local_variable for_env id t
324+
| Some e -> verify_expression envs for_env e;
325+
verify_actual_type_with_declared_type e t id for_env
326+
));
327+
) fil;
328+
(match eo with
329+
| None -> ()
330+
| Some e -> verify_expression envs for_env e;
331+
verify_actual_type_with_boolean e "unknow type in for loop condition"
332+
);
333+
List.iter (verify_expression envs for_env) el;
334+
verify_statement for_env envs s;
293335
| Try(body,catch,finally) -> () (*TODO*)
294336

295337
(* check type for constructors *)

phase2/tests/TestsUnitairesTypingStatement.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ class I {
77
int j = i;
88
// Return None for constructor -> ok
99
public I(boolean a, float b) {
10-
this.a = a;
11-
this.b = b;
10+
// TODO: need expression check on attribute of class
11+
// this.a = a;
12+
// this.b = b;
1213
return;
1314
}
1415
// Return something for constructor -> not ok
@@ -52,4 +53,22 @@ private void testIf(int i){
5253
// j = 4;
5354
// }
5455
}
56+
57+
private void testFor1() {
58+
for (;;) {
59+
i++;
60+
}
61+
}
62+
63+
private void testFor2() {
64+
for (int c = 1+1, b = 2+2; c < 6 ; c++) {
65+
i++;
66+
}
67+
}
68+
// test bad loop condition
69+
// private void testFor3() {
70+
// for (int c = 1+1, b = 2+2; c; c++) {
71+
// i++;
72+
// }
73+
// }
5574
}

0 commit comments

Comments
 (0)
0