try-catch

This commit is contained in:
2024-10-04 13:29:34 +02:00
parent fb7f5c936a
commit 46aa789d64
66 changed files with 564 additions and 0 deletions

View File

@ -13,4 +13,8 @@ runEval = runEval' envEmpty stateInitial
runEval' r s (Free (PrintOp p m)) =
let (ps, res) = runEval' r s m
in (p : ps, res)
runEval' r s (Free (TryCatchOp m l)) =
case (runEval' r s m) of
(_, Left e) -> runEval' r s l
a -> a
runEval' _ _ (Free (ErrorOp e)) = ([], Left e)

View File

@ -65,6 +65,22 @@ pureTests =
--
testCase "Div0" $
eval' (Div (CstInt 7) (CstInt 0))
@?= ([], Left "Division by zero"),
--
testCase "TryCatch try1" $
eval' (TryCatch (CstInt 1) (CstInt 2))
@?= ([], Right (ValInt 1)),
--
testCase "TryCatch try2" $
eval' (TryCatch (CstInt 1) (Div (CstInt 1) (CstInt 0)))
@?= ([], Right (ValInt 1)),
--
testCase "TryCatch catch1" $
eval' (TryCatch (Div (CstInt 1) (CstInt 0)) (CstInt 1))
@?= ([], Right (ValInt 1)),
--
testCase "TryCatch catch1" $
eval' (TryCatch (Div (CstInt 1) (CstInt 0)) (Div (CstInt 1) (CstInt 0)))
@?= ([], Left "Division by zero")
]