Add error handling
This commit is contained in:
parent
4e0d4dd9ac
commit
dae562047c
1 changed files with 23 additions and 7 deletions
30
main.ml
30
main.ml
|
@ -1,14 +1,30 @@
|
||||||
|
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
|
||||||
|
|
||||||
(* simple REPL *)
|
(* simple REPL *)
|
||||||
let rec repl () : unit =
|
let rec repl () : unit =
|
||||||
Printf.printf "> ";
|
printf "> ";
|
||||||
let line = read_line () in
|
let line = read_line () in
|
||||||
if line <> "quit" then begin
|
if line <> "quit" then begin
|
||||||
line
|
try
|
||||||
|> Lex.tokenize
|
line
|
||||||
|> Parser.parse
|
|> Lex.tokenize
|
||||||
|> Eval.eval
|
|> Parser.parse
|
||||||
|> Ast.typ_to_string
|
|> Eval.eval
|
||||||
|> Printf.printf "%s\n";
|
|> Ast.typ_to_string
|
||||||
|
|> printf "%s\n"
|
||||||
|
with
|
||||||
|
| e -> print_error e;
|
||||||
repl ()
|
repl ()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue