about summary refs log tree commit diff
path: root/src/bin/main.ml
diff options
context:
space:
mode:
authorMalte Voos <git@mal.tc>2024-07-08 22:01:42 +0200
committerMalte Voos <git@mal.tc>2024-07-08 22:01:42 +0200
commit97f84ccace4e634b4e02178a702818e69292dc9f (patch)
tree9cef95c62e3fa078db256c7fe657732fecef40a8 /src/bin/main.ml
parent57de10d8728f51942f676b68f1f3ea29d9b78e6e (diff)
downloadtoytt-97f84ccace4e634b4e02178a702818e69292dc9f.tar.gz
toytt-97f84ccace4e634b4e02178a702818e69292dc9f.zip
implement top-level definitions
Diffstat (limited to 'src/bin/main.ml')
-rw-r--r--src/bin/main.ml42
1 files changed, 31 insertions, 11 deletions
diff --git a/src/bin/main.ml b/src/bin/main.ml
index e1be039..4cbeef7 100644
--- a/src/bin/main.ml
+++ b/src/bin/main.ml
@@ -1,19 +1,39 @@
 module Term = Asai.Tty.Make(Error.Message)
 
-let ep input =
+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 (tp, tm) = Elaborator.infer_toplevel ast in
-  let value = NbE.eval ~env:Emp tm 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 ~size:0 value)
-    (Pretty.pp ~names:Emp) (NbE.quote ~size:0 tp)
+    (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 () =
+let rec repl ~toplvl () =
   match LNoise.linenoise "toytt> " with
-  | Some input -> 
-    Error.run ~emit:(Term.display ~show_backtrace:false) ~fatal:(Term.display ~show_backtrace:false) (fun () -> ep input);
+  | Some input ->
+    handle_error (fun () -> ep ~toplvl input);
     let _ = LNoise.history_add input in
-    repl ()
-  | None -> repl ()
+    repl ~toplvl ()
+  | None -> repl ~toplvl ()
+
+let input_file = ref None
 
-let () = repl ();
+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) ()