/
toplevel.ml
48 lines (45 loc) · 1.46 KB
/
toplevel.ml
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
40
41
42
43
44
45
46
47
48
open Base
open Stdio
module I = Minipy.Interpreter
module Parse = Minipy.Parse
let protect ~f =
try f () with
| Minipy.RuntimeError message -> printf "RuntimeError: %s\n%!" message
let toplevel () =
let env = I.Env.empty () in
let eval_stmts stmts = protect ~f:(fun () -> I.eval_stmts env stmts) in
let rec repl () =
Stdio.printf ">>> %!";
match In_channel.input_line stdin with
| None -> ()
| Some line ->
let stmts = Parse.parse_string (line ^ "\n") in
(match stmts with
| Error { message; context } ->
printf "ParseError: %s\n%!" message;
Option.iter context ~f:(fun c -> printf "%s\n%!" c)
| Ok stmts ->
(match List.last stmts with
| None -> ()
| Some { value = Expr { value }; loc = _ } ->
let stmts = List.drop_last_exn stmts in
eval_stmts stmts;
protect ~f:(fun () ->
match I.eval_expr env value with
| Val_none -> ()
| v -> Minipy.Value.to_string v ~escape_special_chars:false |> print_endline)
| Some _ -> eval_stmts stmts));
repl ()
in
repl ()
let () =
let argv = Sys.get_argv () in
if Array.length argv <= 1
then toplevel ()
else
protect ~f:(fun () ->
match Parse.parse_file argv.(1) with
| Ok stmts -> I.simple_eval stmts
| Error { message; context } ->
printf "ParseError: %s\n%!" message;
Option.iter context ~f:(fun c -> printf "%s\n%!" c))