ce/main.ml

32 lines
655 B
OCaml
Raw Normal View History

2022-01-11 00:45:25 +09:00
open Printf
let error_to_string e =
try raise e with
| Parser.Expected t -> sprintf "expected %s" t
| Parser.Unexpected_token t -> sprintf "unexpected token \"%s\"" t
| Failure f -> f
| Division_by_zero -> "cannot divide by zero"
| _ -> raise e
let print_error e =
printf "error: %s\n" @@ error_to_string e
2022-01-10 01:31:47 +09:00
(* simple REPL *)
let rec repl () : unit =
2022-01-11 00:45:25 +09:00
printf "> ";
2022-01-10 01:31:47 +09:00
let line = read_line () in
if line <> "quit" then begin
2022-01-11 01:05:29 +09:00
(try
line
|> Lex.tokenize
|> Parser.parse
|> Eval.eval
|> Ast.typ_to_string
|> printf "%s\n"
with
| e -> print_error e);
2022-01-10 01:31:47 +09:00
repl ()
end
let () = repl ()