comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 (* lab1/main.ml *)
2 (* Copyright (c) 2017 J. M. Spivey *)
3
4 open Eval
5 open Print
6
7 (* |parse_equation| -- parse a string as an equation *)
8 let parse_equation s =
9 Parser.equation Lexer.token (Lexing.from_string s)
10
11 (* |failure| -- print message after an exception *)
12 let failure msg =
13 printf "Failed: $\n" [fStr msg]
14
15 (* |main| -- main read-eval-print loop *)
16 let main () =
17 printf "Welcome to the world of arithmetic\n" [];
18 try
19 while true do
20 printf "? " []; flush stdout;
21 let line = input_line stdin in
22 try
23 let (x, e) = parse_equation line in
24 let v = process (x, e) in
25 printf "=> $\n" [fFlo v]
26 with
27 Failure msg -> failure msg
28 | Not_found -> failure "not found"
29 | Parsing.Parse_error -> failure "syntax error"
30 done
31 with End_of_file ->
32 printf "\nBye\n" []
33
34 let calc = main ()