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