forked from reactorlabs/sourir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.ml
30 lines (23 loc) · 854 Bytes
/
IO.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
open Instr
exception EOF
exception Invalid_input
let rec string_of_value : value -> string = function
| Nil -> "nil"
| Bool b -> string_of_bool b
| Int n -> string_of_int n
| Fun_ref f -> Printf.sprintf "'%s" f
| Array addr -> Printf.sprintf "array@%d" (addr :> int)
let value_of_string str =
try Parse.value_of_string str
with _ -> Printf.kprintf invalid_arg "value_of_string %S" str
type input = unit -> input_tape (* may raise one of the exceptions above *)
and input_tape = Next of value * input
let no_input () = raise EOF
let rec list_input li () = match li with
| [] -> raise EOF
| v :: vs -> Next (v, list_input vs)
let rec stdin_input () =
match value_of_string (read_line ()) with
| exception End_of_file -> raise EOF
| exception (Invalid_argument _) -> raise Invalid_input
| value -> Next (value, stdin_input)