-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminiml.ml
executable file
·52 lines (46 loc) · 1.4 KB
/
miniml.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
49
50
51
52
(* MiniML: a subset of ML
Read-eval-print loop
Stuart M. Shieber
November 19, 2015
*)
open Printf ;;
(* str_to_exp: string -> expr
Returns the expression specified by the string using the Miniml
parser. *)
let str_to_exp (str: string) : Expr.expr =
let lexbuf = Lexing.from_string str in
let exp = Miniml_parse.input Miniml_lex.token lexbuf in
exp
;;
(* Read-eval-print loop for MiniML *)
let repl () =
(* lexical analyzer buffer from stdin *)
let lexbuf = Lexing.from_channel stdin in
(* set up the initial environment *)
let env = Evaluation.Env.create () in
printf "Entering %s...\n" Sys.argv.(0);
flush stdout;
while true do
(try
printf "<== ";
flush stdout;
(* read and parse an expression from the input *)
let exp = Miniml_parse.input Miniml_lex.token lexbuf in
(* evaluate it *)
let res = Evaluation.evaluate exp env in
(* print the result *)
match res with
| Evaluation.Env.Val res -> printf "==> %s\n" (Expr.exp_to_string res)
| Evaluation.Env.Closure(_,_) -> raise (Evaluation.EvalError "can't happen")
with
| Parsing.Parse_error -> printf "xx> parse error\n"
| End_of_file -> printf "Goodbye.\n"; exit 0
);
flush stdout
done
;;
(* Run repl if called from command line *)
try
let _ = Str.search_forward (Str.regexp "miniml\\.\\(byte\\|native\\)") (Sys.argv.(0)) 0 in
repl ()
with Not_found -> () ;;