Skip to content

Commit 5303868

Browse files
committed
Typing: add check type of Cast/Type/Instanceof/ClassOf in expression
1 parent 2c9b823 commit 5303868

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

phase2/Typing/Typing.ml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ let rec verify_expression env current_env e =
215215
if (e1.etype <> Some(Primitive(Int)) && e1.etype <> Some(Primitive(Float))) then
216216
raise(WrongTypePostfixOperation(string_of_expression(e1)^"--"))
217217
);
218-
e.etype <- e1.etype;
218+
e.etype <- e1.etype
219219
| Pre (op,e1) ->
220220
verify_expression env current_env e1;
221221
(match op with
@@ -238,11 +238,17 @@ let rec verify_expression env current_env e =
238238
| Op_cor | Op_cand | Op_eq | Op_ne | Op_gt | Op_lt | Op_ge | Op_le -> e.etype <- Some(Primitive(Boolean))
239239
| Op_or | Op_and | Op_xor | Op_shl | Op_shr | Op_shrr | Op_add | Op_sub | Op_mul | Op_div | Op_mod -> e.etype <- e1.etype)
240240
| CondOp (e1,e2,e3) -> () (*TODO*)
241-
| Cast (t,e) -> () (*TODO*)
242-
| Type t -> () (*TODO*)
243-
| ClassOf t-> () (*TODO*)
244-
| Instanceof (e1, t)-> () (*TODO*)
245-
| VoidClass -> () (*TODO*)
241+
| Cast (t,e1) ->
242+
verify_expression env current_env e1;
243+
e.etype <- Some(t)
244+
| Type t ->
245+
e.etype <- Some(t)
246+
| ClassOf t ->
247+
e.etype <- Some(t)
248+
| Instanceof (e1, t) ->
249+
verify_expression env current_env e1;
250+
e.etype <- Some(Primitive(Boolean))
251+
| VoidClass -> ()
246252

247253
(* add a local variable to the current environment *)
248254
let add_local_variable current_env id t =

phase2/tests/test.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public I(int e) {
1616
int g = ~5;
1717
float h = (-8.5f);
1818
boolean boo = I.a;
19+
int sr = 5;
20+
long l = (long) sr;
21+
boolean test = my instanceof I;
1922
private String func1 (int a) {
2023
int b = 1;
2124
b += (5+4);

0 commit comments

Comments
 (0)