Skip to content

Commit 7a96c62

Browse files
committed
Typing: add check the type of ArrayInit in expression
1 parent 29f7958 commit 7a96c62

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

phase2/Typing/TypeError.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ exception UnknownVariable of string
2020
exception WrongTypePrefixOperation of string * string
2121
exception WrongTypePostfixOperation of string
2222
exception WrongInvokedArgumentsLength of string
23+
exception WrongTypeArrayInitList of string * string
2324
exception WrongTypesAssignOperation of string * string * string
2425
exception WrongTypeCondOperation of string
2526
exception WrongTypesOperation of string * string * string

phase2/Typing/Typing.ml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ let verify_call_expr meth_name args env class_name =
141141
| WrongInvokedArgumentsLength s -> raise(WrongInvokedArgumentsLength(s))
142142
end
143143

144+
let rec verify_array_init_list el =
145+
match el with
146+
| [] -> ()
147+
| h::t ->
148+
(match t with
149+
| [] -> ()
150+
| h1::t1 ->
151+
if h.etype <> h1.etype then
152+
raise(WrongTypeArrayInitList(stringOfOpt h.etype, stringOfOpt h1.etype)));
153+
verify_array_init_list t
144154

145155

146156
(* check the type of the expressions *)
@@ -206,7 +216,13 @@ let rec verify_expression env current_env e =
206216
e.etype <- verify_value v
207217
| Name s ->
208218
e.etype <- verify_name s env current_env
209-
| ArrayInit el -> () (*TODO*)
219+
| ArrayInit el ->
220+
List.iter (verify_expression env current_env) el;
221+
(* check the type of each element which is in an array list is same *)
222+
verify_array_init_list el;
223+
e.etype <-
224+
(match (List.hd el).etype with
225+
| Some(t) -> Some(Array(t, 1)))
210226
| Array (e,el) -> () (*TODO*)
211227
| AssignExp (e1,op,e2) ->
212228
verify_expression env current_env e1;

phase2/tests/test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public I(int e) {
2121
boolean test = my instanceof I;
2222
int condop = (1 == 2) ? 1 : 5;
2323
int[] arr = new int[5];
24+
int[] arr2 = {1,2,8.5f};
2425
private String func1 (int a) {
2526
int b = 1;
2627
b += (5+4);

0 commit comments

Comments
 (0)