Mercurial > hg > compilers
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lab0/main.ml Thu Oct 05 08:04:15 2017 +0100 @@ -0,0 +1,34 @@ +(* lab1/main.ml *) +(* Copyright (c) 2017 J. M. Spivey *) + +open Eval +open Print + +(* |parse_equation| -- parse a string as an equation *) +let parse_equation s = + Parser.equation Lexer.token (Lexing.from_string s) + +(* |failure| -- print message after an exception *) +let failure msg = + printf "Failed: $\n" [fStr msg] + +(* |main| -- main read-eval-print loop *) +let main () = + printf "Welcome to the world of arithmetic\n" []; + try + while true do + printf "? " []; flush stdout; + let line = input_line stdin in + try + let (x, e) = parse_equation line in + let v = process (x, e) in + printf "=> $\n" [fFlo v] + with + Failure msg -> failure msg + | Not_found -> failure "not found" + | Parsing.Parse_error -> failure "syntax error" + done + with End_of_file -> + printf "\nBye\n" [] + +let calc = main ()