summaryrefslogtreecommitdiff
path: root/source/mod_student/typechecker.ml
diff options
context:
space:
mode:
Diffstat (limited to 'source/mod_student/typechecker.ml')
-rw-r--r--source/mod_student/typechecker.ml19
1 files changed, 16 insertions, 3 deletions
diff --git a/source/mod_student/typechecker.ml b/source/mod_student/typechecker.ml
index ce396d9..b12bc67 100644
--- a/source/mod_student/typechecker.ml
+++ b/source/mod_student/typechecker.ml
@@ -27,6 +27,11 @@ module Make() = struct
| TEXPR_Int _ -> TP_Int
| TEXPR_Bool _ -> TP_Bool
| TEXPR_Array {sub;_} -> TP_Array (te2type sub)
+ let rec te2type_ret = function
+ | TEXPR_Int _ -> TP_Int
+ | TEXPR_Bool _ -> TP_Bool
+ | TEXPR_Array {sub;dim=None;_} -> TP_Array (te2type_ret sub)
+ | TEXPR_Array {sub;dim;loc} -> ErrorReporter.report_array_initialization_forbidden ~loc
let add_var_decl env (VarDecl {loc;id;tp}) =
match TypingEnvironment.add id (ENVTP_Var (te2type tp)) env with
| (env,true) -> env
@@ -138,7 +143,9 @@ module Make() = struct
ErrorReporter.report_cannot_infer ~loc
| EXPR_Struct {elements=x::xs; _} ->
- failwith "not yet implemented"
+ let t=infer_expression env x in
+ let _=List.map (check_expression env t) xs in
+ TP_Array t
and check_function_call env (Call {loc;callee;_} as call)=
match check_function_args env call with
@@ -176,6 +183,12 @@ module Make() = struct
* elementy kontra typ elementu tablicy `tp` *)
| EXPR_Struct {elements; _}, TP_Array tp ->
List.iter (check_expression env tp) elements
+ | EXPR_Binop {op=BINOP_And; lhs; rhs;_}, TP_Int ->
+ check_expression env TP_Int lhs;
+ check_expression env TP_Int rhs
+ | EXPR_Binop {op=BINOP_And; lhs; rhs;_}, (TP_Array tp as t) ->
+ check_expression env t lhs;
+ check_expression env t rhs
(* ========== !! Zaimplementuj pozostale przypadki !! ========= *)
@@ -298,7 +311,7 @@ module Make() = struct
let env = List.fold_left add_var_decl env formal_parameters in
match body with
| Some body -> begin
- match check_statement_block (TypingEnvironment.set_return env (List.map te2type return_types)) body with
+ match check_statement_block (TypingEnvironment.set_return env (List.map te2type_ret return_types)) body with
| _, RT_Unit -> ErrorReporter.report_not_all_control_paths_return_value ~loc ~id
| _, RT_Void -> ()
end
@@ -306,7 +319,7 @@ module Make() = struct
let scan_global_declaration env = function
| GDECL_Function {id; formal_parameters; return_types; loc; _} ->
- match TypingEnvironment.add id (ENVTP_Fn ((List.map (fun (VarDecl x) -> te2type(x.tp)) formal_parameters),(List.map te2type return_types)) ) env with
+ match TypingEnvironment.add id (ENVTP_Fn ((List.map (fun (VarDecl x) -> te2type_ret(x.tp)) formal_parameters),(List.map te2type_ret return_types)) ) env with
| (env,true) -> env
| (env,false) -> ErrorReporter.report_shadows_previous_definition ~loc ~id