calcInt :: EExp -> MRSIO Integer
calcInt x = case x of
    EVar ident     -> convertVar calcInt ident
    EAdd exp1 exp2 -> do n1 <- calcInt exp1
                         n2 <- calcInt exp2
                         return $ n1 + n2
    ESub exp1 exp2 -> do n1 <- calcInt exp1
                         n2 <- calcInt exp2
                         return $ n1 - n2
    EMul exp1 exp2 -> do n1 <- calcInt exp1
                         n2 <- calcInt exp2
                         return $ n1 * n2
    EDiv exp1 exp2 -> do n1 <- calcInt exp1
                         n2 <- calcInt exp2
                         return $ n1 `div` n2
    EInt n         -> return n
    Call id d      -> do exp <- calcFunc (Call id d)
                         case exp of
                           EInt i -> return i
                           e -> do putStr_IO $ "calc int " ++ show e
                                   return 0
    EKey cont k    -> getFromCont cont k
      where
        getFromCont (EArrI a) (EInt i1) = do i <- intFromEInt (EInt i1)
                                             exp <- return $ a ! i
                                             case exp of
                                               EInt i -> return $ toInteger i
                                               e      -> do putStr_Err "Error type error"
                                                            return 0

        getFromCont (EVar ident) key     = do a <- askExp ident
                                              getFromCont a key
        getFromCont (EDict d t1 t2) key  = if M.member key d then
              do exp <- return $ fromJust $ M.lookup key d
                 case exp of
                  EInt i -> return $ toInteger i
                  e      -> do putStr_Err $ "Error type error"
                               return 0
              else
                do putStr_Err $ "Dict " ++ show d ++ " doesn't have a key " ++ show key
                   return 0

        getFromCont arr key              = do cexp <- calcExp key
                                              getFromCont arr cexp
calcInt :: EExp -> MRSIO Integer
calcInt x = case x of
    EVar ident     -> convertVar calcInt ident
    EAdd exp1 exp2 -> do n1 <- calcInt exp1
                         n2 <- calcInt exp2
                         return $ n1 + n2
    ESub exp1 exp2 -> do n1 <- calcInt exp1
                         n2 <- calcInt exp2
                         return $ n1 - n2
    EMul exp1 exp2 -> do n1 <- calcInt exp1
                         n2 <- calcInt exp2
                         return $ n1 * n2
    EDiv exp1 exp2 -> do n1 <- calcInt exp1
                         n2 <- calcInt exp2
                         return $ n1 `div` n2
    EInt n         -> return n
    Call id d      -> do exp <- calcFunc (Call id d)
                         case exp of
                           EInt i -> return i
                           e -> do putStr_IO $ "calc int " ++ show e
                                   return 0
    EKey cont k    -> getFromCont cont k
      where
        getFromCont (EArrI a) (EInt i1) = do i <- intFromEInt (EInt i1)
                                             exp <- return $ a ! i
                                             case exp of
                                               EInt i -> return $ toInteger i
                                               e      -> do putStr_Err "Error type error"
                                                            return 0

        getFromCont (EVar ident) key     = do a <- askExp ident
                                              getFromCont a key
        getFromCont (EDict d t1 t2) key  = if M.member key d then
              do exp <- return $ fromJust $ M.lookup key d
                 case exp of
                  EInt i -> return $ toInteger i
                  e      -> do putStr_Err $ "Error type error"
                               return 0
              else
                do putStr_Err $ "Dict " ++ show d ++ " doesn't have a key " ++ show key
                   return 0

        getFromCont arr key              = do cexp <- calcExp key
                                              getFromCont arr cexp
calcInt x = case x of
    EVar ident     -> convertVar calcInt ident
    EAdd exp1 exp2 -> do n1 <- calcInt exp1
                         n2 <- calcInt exp2
                         return $ n1 + n2
    ESub exp1 exp2 -> do n1 <- calcInt exp1
                         n2 <- calcInt exp2
                         return $ n1 - n2
    EMul exp1 exp2 -> do n1 <- calcInt exp1
                         n2 <- calcInt exp2
                         return $ n1 * n2
    EDiv exp1 exp2 -> do n1 <- calcInt exp1
                         n2 <- calcInt exp2
                         return $ n1 `div` n2
    EInt n         -> return n
    Call id d      -> do exp <- calcFunc (Call id d)
                         case exp of
                           EInt i -> return i
                           e -> do putStr_IO $ "calc int " ++ show e
                                   return 0
    EKey cont k    -> getFromCont cont k
      where
        getFromCont (EArrI a) (EInt i1) = do i <- intFromEInt (EInt i1)
                                             exp <- return $ a ! i
                                             case exp of
                                               EInt i -> return $ toInteger i
                                               e      -> do putStr_Err "Error type error"
                                                            return 0

        getFromCont (EVar ident) key     = do a <- askExp ident
                                              getFromCont a key
        getFromCont (EDict d t1 t2) key  = if M.member key d then
              do exp <- return $ fromJust $ M.lookup key d
                 case exp of
                  EInt i -> return $ toInteger i
                  e      -> do putStr_Err $ "Error type error"
                               return 0
              else
                do putStr_Err $ "Dict " ++ show d ++ " doesn't have a key " ++ show key
                   return 0

        getFromCont arr key              = do cexp <- calcExp key
                                              getFromCont arr cexp