comparison lab0/eval.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/eval.ml *)
2 (* Copyright (c) 2017 J. M. Spivey *)
3
4 open Tree
5
6 (* |do_binop| -- compute result of binary operator *)
7 let do_binop w v1 v2 =
8 match w with
9 Plus -> v1 +. v2
10 | Minus -> v1 -. v2
11 | Times -> v1 *. v2
12 | Divide ->
13 if v2 = 0.0 then failwith "dividing by zero";
14 v1 /. v2
15
16 (* |eval_expr| -- evaluate an expression *)
17 let rec eval_expr =
18 function
19 Number r -> r
20 | Variable x -> failwith "Sorry, I don't do variables"
21 | Binop (w, e1, e2) ->
22 do_binop w (eval_expr e1) (eval_expr e2)
23
24 (* |process| -- process an equation, return value of RHS *)
25 let process (x, e) = eval_expr e