annotate lab0/main.ml @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* lab1/main.ml *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 (* Copyright (c) 2017 J. M. Spivey *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 open Eval
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 open Print
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 (* |parse_equation| -- parse a string as an equation *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 let parse_equation s =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 Parser.equation Lexer.token (Lexing.from_string s)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 (* |failure| -- print message after an exception *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 let failure msg =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 printf "Failed: $\n" [fStr msg]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 (* |main| -- main read-eval-print loop *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 let main () =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 printf "Welcome to the world of arithmetic\n" [];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 try
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 while true do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 printf "? " []; flush stdout;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 let line = input_line stdin in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 try
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 let (x, e) = parse_equation line in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 let v = process (x, e) in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 printf "=> $\n" [fFlo v]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 with
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 Failure msg -> failure msg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 | Not_found -> failure "not found"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 | Parsing.Parse_error -> failure "syntax error"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 done
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 with End_of_file ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 printf "\nBye\n" []
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 let calc = main ()