11{-# LANGUAGE GADTs #-}
2+ {-# LANGUAGE LambdaCase #-}
23{-# LANGUAGE OverloadedStrings #-}
34module LambdaComp.Driver where
45
@@ -7,6 +8,7 @@ import Paths_lambdacomp (getDataDir)
78import Control.Exception (bracketOnError )
89import Control.Monad.Except (ExceptT (ExceptT , runExceptT ), MonadError (throwError ))
910import Control.Monad.Trans (MonadTrans (lift ))
11+ import Data.Functor ((<&>) )
1012import Data.Text.IO qualified as T
1113import System.Directory (makeAbsolute )
1214import System.Exit (ExitCode (ExitFailure , ExitSuccess ))
@@ -31,7 +33,7 @@ import LambdaComp.External.ToElaborated (ElaborationError, runToElaborat
3133import LambdaComp.Parser (runProgramParser )
3234
3335mainFuncWithOptions :: Handle -> Options -> IO ExitCode
34- mainFuncWithOptions outH (Options inputFp backend phase mayFp) = fmap ( either ExitFailure ( const ExitSuccess )) . runExceptT $ do
36+ mainFuncWithOptions outH (Options inputFp backend phase mayFp) = ( <* hFlush outH) . exceptTToExitCode $ do
3537 input <- lift $ T. readFile inputFp
3638 let getTm = either ((>> throwError 1 ) . lift . hPutStrLn stderr) pure $ runProgramParser inputFp input
3739 getElTm = getTm >>= handleElabError outH . runToElaborated
@@ -59,7 +61,6 @@ mainFuncWithOptions outH (Options inputFp backend phase mayFp) = fmap (either Ex
5961 cCode <- getCCode
6062 runWithFp (const $ genAndExeCExe outH cCode) mayFp
6163 AMBackend -> getAMTm >>= lift . topEval outH >>= pHPrintNoColor outH
62- lift $ hFlush outH
6364
6465handleElabError :: Handle -> Either ElaborationError El. Program -> ExceptT Int IO El. Program
6566handleElabError outH (Left elabErr) = pHPrintNoColor outH elabErr >> throwError 1
@@ -105,9 +106,10 @@ genCExe outH cCode fp = do
105106 cleanupProcess
106107 (\ (_, _, _, p) -> waitForProcess p)
107108
108- exitCodeToExceptT :: IO ExitCode -> ExceptT Int IO ()
109- exitCodeToExceptT a = ExceptT $ do
110- e <- a
111- case e of
112- ExitSuccess -> pure $ Right ()
113- ExitFailure c -> pure $ Left c
109+ exitCodeToExceptT :: Functor m => m ExitCode -> ExceptT Int m ()
110+ exitCodeToExceptT a = ExceptT $ a <&> \ case
111+ ExitSuccess -> Right ()
112+ ExitFailure c -> Left c
113+
114+ exceptTToExitCode :: Functor m => ExceptT Int m () -> m ExitCode
115+ exceptTToExitCode = fmap (either ExitFailure (const ExitSuccess )) . runExceptT
0 commit comments