🎣 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 where
f' x y = pure $ f x y 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 :: Exp -> EvalM Val
eval (CstInt x) = pure $ ValInt x eval (CstInt x) = pure $ ValInt x
eval (CstBool b) = pure $ ValBool b eval (CstBool b) = pure $ ValBool b
@ -71,3 +71,23 @@ eval (Apply e1 e2) = do
failure "Cannot apply non-function" failure "Cannot apply non-function"
eval (TryCatch e1 e2) = eval (TryCatch e1 e2) =
eval e1 `catch` eval 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

View File

@ -76,6 +76,7 @@ data EvalOp a
| StatePutOp State a | StatePutOp State a
| PrintOp String a | PrintOp String a
| ErrorOp Error | ErrorOp Error
| TryCatchOp a a
instance Functor EvalOp where instance Functor EvalOp where
fmap f (ReadOp k) = ReadOp $ f . k fmap f (ReadOp k) = ReadOp $ f . k
@ -83,6 +84,7 @@ instance Functor EvalOp where
fmap f (StatePutOp s m) = StatePutOp s $ f m fmap f (StatePutOp s m) = StatePutOp s $ f m
fmap f (PrintOp p m) = PrintOp p $ f m fmap f (PrintOp p m) = PrintOp p $ f m
fmap _ (ErrorOp e) = ErrorOp e fmap _ (ErrorOp e) = ErrorOp e
fmap f (TryCatchOp m1 m2) = TryCatchOp (f m1) (f m2)
type EvalM a = Free EvalOp a type EvalM a = Free EvalOp a