module TrycatDef where
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
data Stmt
= Assign String Expr — the String is for the LHS variable
| Try [Stmt] Exception [Stmt] — 1st list for try-block, 2nd for catch-block
| Compound [Stmt] — begin … end
deriving (Eq, Show)
data Expr
= Lit Integer
| Var String
| Add Expr Expr
| Div Expr Expr
deriving (Eq, Show)
data Exception = DivByZero | VarUninit
deriving (Eq, Show)
class Monad m => MonadTrycat m where
— Init/Write a variable.
putVar :: String -> Integer -> m ()
— Read a variable. Throws VarUninit if uninit.
getVar :: String -> m Integer
— Throw the given exception.
raise :: Exception -> m a
— tryCatch p eCaught handler:
— Run p, but if eCaught is caught, run handler.
tryCatch :: m a -> Exception -> m a -> m a
data TC a = MkTC (Map String Integer -> (Map String Integer, Either Exception a))
unTC :: TC a -> Map String Integer -> (Map String Integer, Either Exception a)
unTC (MkTC stf) = stf