-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ml
31 lines (28 loc) · 999 Bytes
/
main.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
open Parser
let rec eval (ast:Parser.ast) : Peano.p =
match ast with
| Parser.Const i -> Peano.n i
| Parser.Add (x,y) -> Peano.( + ) (eval x) (eval y)
| Parser.Sub (x,y) -> Peano.( - ) (eval x) (eval y)
| Parser.Mod (x,y) -> Peano.( % ) (eval x) (eval y)
| Parser.Mul (x,y) -> Peano.( * ) (eval x) (eval y)
| Parser.Div (x,y) -> Peano.( / ) (eval x) (eval y)
| Parser.Exp (x,y) -> Peano.( ^ ) (eval x) (eval y)
let rec loop () =
print_string "peano> ";
let input = read_line () in
let ast = input |> Parser.init |> Parser.parse_expr.run in
let _ = match ast with
| Ok (ast, _) ->
let peano_result = eval ast in
let bracketed_repr = Parser.show ast in
Printf.printf "%s\n= %d\n"
bracketed_repr
(Peano.to_int peano_result)
| Error e -> Printf.printf
"Error: %s @ %d"
e.desc
e.location;
in
loop ()
let () = loop ()