Skip to content

Commit

Permalink
unit return for type_stmt and type_block
Browse files Browse the repository at this point in the history
  • Loading branch information
epatrizio committed Sep 9, 2022
1 parent 3670072 commit d1f2320
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let process source_code_file no_typing =
try
let ast = Parser.prog Lexer.token lexbuf in
close_in ic;
if not no_typing then Typer.typing ast else Ast.Tunit;
if not no_typing then Typer.typing ast;
Compiler.compile ast source_code_file
with
| Lexer.Lexing_error c ->
Expand Down
19 changes: 9 additions & 10 deletions typer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,20 @@ let rec type_expr env e =
| Tabool -> Tint
| _ -> error "not array type (array_size primitive)"
end
| _ -> error "not implemented (call compiler with --no-typing option)"

and type_stmt env s =
match s with
begin match s with
| Sassign(i,e,s) -> let env = Tmap.add i (type_expr env e) env in type_stmt env s
| Srefassign(i,e) ->
let ty1 = type_expr env (Eident i) in
let ty2 = type_expr env e in
if ty1 == ty2 then Tunit else error "not identic type (ref assign)"
if ty1 == ty2 then () else error "not identic type (ref assign)"
| Saassign(i,e1,e2) ->
let tya = type_expr env (Eident i) in
begin match type_expr env e1 with
| Tint -> begin match type_expr env e2 with
| Tint -> if tya == Taint then Tunit else error "not integer type (array element assign)"
| Tbool -> if tya == Tabool then Tunit else error "not boolean type (array element assign)"
| Tint -> if tya == Taint then () else error "not integer type (array element assign)"
| Tbool -> if tya == Tabool then () else error "not boolean type (array element assign)"
| _ -> error "incoherent type (array element assign)"
end
| _ -> error "not integer type (array assign accessor)"
Expand All @@ -154,18 +153,18 @@ and type_stmt env s =
| Tbool -> type_stmt env s1; type_stmt env s2; type_block env b
| _ -> error "not boolean type (for statement condition)"
end
| Sprint e -> type_expr env e (* print bool (0/1) or unit (0) is ok *)
| Sprint e -> let _ = type_expr env e in () (* print bool (0/1) or unit (0) is ok *)
| Sif (e,s1,s2) -> begin
begin match type_expr env e with
| Tbool -> Tbool
| Tbool -> ()
| _ -> error "not boolean type (if condition statement)"
end;
type_stmt env s1;
type_stmt env s2
end
| Sexit -> Tunit
| Sskip -> Tunit
| _ -> error "not implemented (call compiler with --no-typing option)"
| Sexit -> ()
| Sskip -> ()
end

and type_block env b =
match b with
Expand Down

0 comments on commit d1f2320

Please sign in to comment.