Commit a8a58134 authored by Taddeus Kroes's avatar Taddeus Kroes

funclang series5: Added evaluation of conditional expression.

parent 9dcd50cc
......@@ -40,7 +40,12 @@ let rec eval exp =
| FunAp (e1, e2) -> (* prevent recursion *)
let result = FunAp (eval e1, eval e2) in
if result = exp then result else eval result
| Cond (cond, e1, e2) -> Cond (eval cond, eval e1, eval e2)
| Cond (cond, e1, e2) ->
(* If the condition evaluates to a boolean, choose the corresponding
* conditional expression *)
(match eval cond with
Bool b -> if b then eval e1 else eval e2
| c -> Cond (c, eval e1, eval e2))
| Let (x, e1, e2) -> eval (subs e2 x e1)
| LetRec (x, e1, e2) -> LetRec (x, eval e1, eval e2)
| _ -> exp
......@@ -78,6 +78,8 @@ test_eval "true" (BinopAp (Or, Bool true, Bool false));;
test_eval "fun u -> fun w -> fun a -> a" ass1a;;
test_eval "(a (b c))" ass1b;;
test_eval "c + b" (Let ("a", Var "c", (BinopAp (Add, Var "a", Var "b"))));;
test_eval "a" (Cond (Bool true, Var "a", Var "b"));;
test_eval "b" (Cond (Bool false, Var "a", Var "b"));;
let x = Var "x" in
let y = Var "y" in
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment