about summary refs log tree commit diff
path: root/src/bin/main.ml
blob: 4cbeef7439d8762c14f9244212b7558e720271f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module Term = Asai.Tty.Make(Error.Message)

let display = Term.display ~show_backtrace:false

let handle_error f =
  Error.run ~emit:display ~fatal:display f

let handle_fatal f =
  Error.run ~emit:display ~fatal:(fun msg -> display msg; exit 1) f

let ep ~toplvl input =
  let ast = Parser.parse_expr input in
  let toplvl = Option.value toplvl ~default:Yuujinchou.Trie.empty in
  let (tp, tm) = Elaborator.infer_toplevel ~toplvl ast in
  let value = NbE.eval_toplevel tm in
  Format.printf "%a : %a\n%!"
    (Pretty.pp ~names:Emp) (NbE.quote_toplevel @@ NbE.force_all value)
    (Pretty.pp ~names:Emp) (NbE.quote_toplevel @@ NbE.force_all tp)

let rec repl ~toplvl () =
  match LNoise.linenoise "toytt> " with
  | Some input ->
    handle_error (fun () -> ep ~toplvl input);
    let _ = LNoise.history_add input in
    repl ~toplvl ()
  | None -> repl ~toplvl ()

let input_file = ref None

let () =
  let anon_fun path = input_file := Some path in
  let usage_msg = "usage: toytt [input file]" in
  let () = Arg.parse [] anon_fun usage_msg in
  match !input_file with
  | None ->
    repl ~toplvl:None ()
  | Some path ->
    let toplvl = handle_fatal (fun () -> Driver.process_file path) in
    repl ~toplvl:(Some toplvl) ()