🎣 Add TryCatchOp and add our eval. catch implementation and runeval missing

This commit is contained in:
2024-10-03 16:32:44 +02:00
parent fcd0ed780e
commit 98d41e6a6d
2 changed files with 23 additions and 1 deletions

View File

@ -20,7 +20,7 @@ evalIntBinOp' f e1 e2 =
where
f' x y = pure $ f x y
-- Replace with your 'eval' from your solution to assignment 2.
-- Replaced their eval with ours as instructed NOTE
eval :: Exp -> EvalM Val
eval (CstInt x) = pure $ ValInt x
eval (CstBool b) = pure $ ValBool b
@ -71,3 +71,23 @@ eval (Apply e1 e2) = do
failure "Cannot apply non-function"
eval (TryCatch e1 e2) =
eval e1 `catch` eval e2
eval (Print s e1) = do
v1 <- eval e1
case v1 of
(ValInt i) -> do
evalPrint (s++": "++(show i))
pure $ v1
(ValBool b) -> do
evalPrint (s++": "++(show b))
pure $ v1
(ValFun _ _ _) -> do
evalPrint (s++": #<fun>")
pure $ v1
eval (KvPut e1 e2) = do
v1 <- eval e1
v2 <- eval e2
evalKvPut v1 v2
pure $ v2
eval (KvGet e) = do
v <- eval e
evalKvGet v