| 1 |
open Location
|
| 2 |
|
| 3 |
let () =
|
| 4 |
List.iter
|
| 5 |
(fun (n,t) -> Typer.register_global_types [n, mk noloc (Ast.Internal t)])
|
| 6 |
Builtin.types
|
| 7 |
|
| 8 |
let input = Stream.of_channel stdin
|
| 9 |
let ppf = Format.std_formatter
|
| 10 |
let prog () =
|
| 11 |
try Parser.prog input
|
| 12 |
with
|
| 13 |
| Stdpp.Exc_located (loc, e) ->
|
| 14 |
raise (Location (loc, e))
|
| 15 |
|
| 16 |
let rec print_exn ppf = function
|
| 17 |
| Location ((i,j), exn) ->
|
| 18 |
Format.fprintf ppf "Error at chars %i-%i@\n" i j;
|
| 19 |
print_exn ppf exn
|
| 20 |
| Typer.ShouldHave (t,msg) ->
|
| 21 |
Format.fprintf ppf "This expression should have type %a@\n%s@\n"
|
| 22 |
Types.Print.print_descr t
|
| 23 |
msg
|
| 24 |
| Typer.Constraint (s,t,msg) ->
|
| 25 |
Format.fprintf ppf "This expression should have type %a@\n"
|
| 26 |
Types.Print.print_descr t;
|
| 27 |
Format.fprintf ppf "but its infered type is: %a@\n"
|
| 28 |
Types.Print.print_descr s;
|
| 29 |
Format.fprintf ppf "which is not a subtype, as shown by the value %a@\n"
|
| 30 |
Types.Print.print_sample (Types.Sample.get (Types.diff s t));
|
| 31 |
Format.fprintf ppf "%s@\n" msg
|
| 32 |
| Typer.NonExhaustive t ->
|
| 33 |
Format.fprintf ppf "This pattern matching is not exhaustive@\n";
|
| 34 |
Format.fprintf ppf "Residual type: %a@\n"
|
| 35 |
Types.Print.print_descr t;
|
| 36 |
Format.fprintf ppf "Sample value: %a@\n"
|
| 37 |
Types.Print.print_sample (Types.Sample.get t)
|
| 38 |
| exn ->
|
| 39 |
Format.fprintf ppf "%s@\n" (Printexc.to_string exn)
|
| 40 |
|
| 41 |
let phrase ph =
|
| 42 |
match ph.descr with
|
| 43 |
| Ast.EvalStatement e ->
|
| 44 |
let (fv,e) = Typer.expr e in
|
| 45 |
let t = Typer.type_check Typer.Env.empty e Types.any true in
|
| 46 |
Format.fprintf ppf "%a@\n" Types.Print.print_descr t;
|
| 47 |
| Ast.TypeDecl _ -> ()
|
| 48 |
| _ -> assert false
|
| 49 |
|
| 50 |
let () =
|
| 51 |
try
|
| 52 |
let p = prog () in
|
| 53 |
let type_decls =
|
| 54 |
List.fold_left
|
| 55 |
(fun accu ph -> match ph.descr with
|
| 56 |
| Ast.TypeDecl (x,t) -> (x,t) :: accu
|
| 57 |
| _ -> accu
|
| 58 |
) [] p in
|
| 59 |
Typer.register_global_types type_decls;
|
| 60 |
List.iter phrase p
|
| 61 |
with (Failure _) as e -> raise e | exn -> print_exn ppf exn
|
| 62 |
|