From 98d41e6a6d223630c1c137456e2e2309bbd3a615 Mon Sep 17 00:00:00 2001 From: Sebastian Larsen Prehn Date: Thu, 3 Oct 2024 16:32:44 +0200 Subject: [PATCH] :fishing_pole_and_fish: Add TryCatchOp and add our eval. catch implementation and runeval missing --- a4/src/APL/Eval.hs | 22 +++++++++++++++++++++- a4/src/APL/Monad.hs | 2 ++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/a4/src/APL/Eval.hs b/a4/src/APL/Eval.hs index 3f33e2e..07930be 100644 --- a/a4/src/APL/Eval.hs +++ b/a4/src/APL/Eval.hs @@ -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++": #") + 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 diff --git a/a4/src/APL/Monad.hs b/a4/src/APL/Monad.hs index 3f26e76..8bdcce1 100644 --- a/a4/src/APL/Monad.hs +++ b/a4/src/APL/Monad.hs @@ -76,6 +76,7 @@ data EvalOp a | StatePutOp State a | PrintOp String a | ErrorOp Error + | TryCatchOp a a instance Functor EvalOp where 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 (PrintOp p m) = PrintOp p $ f m fmap _ (ErrorOp e) = ErrorOp e + fmap f (TryCatchOp m1 m2) = TryCatchOp (f m1) (f m2) type EvalM a = Free EvalOp a