🕸️ Try-Catch
This commit is contained in:
@ -21,6 +21,7 @@ data Exp
|
||||
| Let VName Exp Exp
|
||||
| Lambda VName Exp
|
||||
| Apply Exp Exp
|
||||
| TryCatch Exp Exp
|
||||
deriving (Eq, Show)
|
||||
|
||||
printExp :: Exp -> String
|
||||
|
@ -86,6 +86,8 @@ eval env (Apply e1 e2) =
|
||||
(_, Left err) -> Left err
|
||||
(Right (ValFun env2 var e3), Right x) -> eval (envExtend var x env2) e3
|
||||
(_, _) -> Left "Applying non-function"
|
||||
eval env (TryCatch e1 e2) =
|
||||
case (eval env e1) of
|
||||
(Right x) -> Right x
|
||||
(Left _) -> eval env e2
|
||||
|
||||
|
||||
-- TODO: Add cases after extending Exp.
|
||||
|
@ -102,5 +102,11 @@ tests =
|
||||
eval [] (Apply (Let "x" (CstInt 2) (Lambda "y" (Add (Var "x") (Var "y")))) (CstInt 3))
|
||||
@?= Right (ValInt 5),
|
||||
testCase "Apply (fact)" $
|
||||
eval envEmpty (Apply fact (CstInt 5)) @?= Right (ValInt 120)
|
||||
eval envEmpty (Apply fact (CstInt 5)) @?= Right (ValInt 120),
|
||||
testCase "Try" $
|
||||
eval envEmpty (TryCatch (CstInt 0) (CstInt 1))
|
||||
@?= Right (ValInt 0),
|
||||
testCase "Catch" $
|
||||
eval envEmpty (TryCatch (Var "missing") (CstInt 1))
|
||||
@?= Right (ValInt 1)
|
||||
]
|
||||
|
Reference in New Issue
Block a user