@@ -230,6 +230,42 @@ let add_local_variable current_env id t =
230
230
then Hashtbl. add current_env.variables id t
231
231
else raise(DuplicateLocalVariable ((Type. stringOf t)^ " " ^ id))
232
232
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
+
233
269
let rec verify_statement current_env envs statement =
234
270
match statement with
235
271
| VarDecl dl ->
@@ -239,15 +275,7 @@ let rec verify_statement current_env envs statement =
239
275
| None -> add_local_variable current_env id t
240
276
(* check int j = 2; or int j = i;*)
241
277
| 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
251
279
)
252
280
);
253
281
) dl
@@ -278,18 +306,32 @@ let rec verify_statement current_env envs statement =
278
306
| While (e ,s ) -> () (* TODO*)
279
307
| If (e ,s ,None) -> (verify_expression envs current_env e;
280
308
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" )
285
310
| If (e ,s1 ,Some s2 ) -> (verify_expression envs current_env e;
286
311
verify_statement current_env envs s1;
287
312
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;
293
335
| Try (body ,catch ,finally ) -> () (* TODO*)
294
336
295
337
(* check type for constructors *)
0 commit comments