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