[parser] working equation type-checker

This commit is contained in:
Arnaud DABY-SEESARAM 2022-12-10 00:33:14 +01:00
parent a17b3c6fdd
commit 8775edc6fc

View File

@ -107,6 +107,20 @@
then ETriOp (t2, op, e1, e2, e3) then ETriOp (t2, op, e1, e2, e3)
else raise (MyParsingError (error_msg, current_location())) else raise (MyParsingError (error_msg, current_location()))
let rec debug_type_pp fmt = function
| FTBase TBool -> Format.fprintf fmt "bool"
| FTBase TReal -> Format.fprintf fmt "real"
| FTBase TInt -> Format.fprintf fmt "int"
| FTArr (t1, t2) -> Format.fprintf fmt "( %a -> %a )"
debug_type_pp t1 debug_type_pp t2
| FTList [] -> ()
| FTList (h :: []) -> Format.fprintf fmt "l%a" debug_type_pp h
| FTList (h :: h' :: t) ->
Format.fprintf fmt "l%a; %a" debug_type_pp h debug_type_pp (FTList (h' :: t))
let debug_type =
Format.printf "Type: %a\n" debug_type_pp
%} %}
%token EOF %token EOF
@ -239,11 +253,14 @@ equations:
equation: equation:
pattern EQUAL expr pattern EQUAL expr
{ let (t_patt, patt) = $1 in { let (t_patt, patt) = $1 in
let expr = $3 in let expr = $3 in let texpr = type_exp expr in
if type_exp expr = t_patt if (match texpr with
| FTList _ -> texpr = t_patt
| _ -> FTList [texpr] = t_patt)
then ((t_patt, patt), expr) then ((t_patt, patt), expr)
else raise (MyParsingError ("The equation does not type check!", else (debug_type t_patt; debug_type (type_exp expr);
current_location())) }; raise (MyParsingError ("The equation does not type check!",
current_location()))) };
pattern: pattern:
| IDENT | IDENT