Skip to content

Commit c2e8769

Browse files
committed
Fix flush timing
1 parent ecb9487 commit c2e8769

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

src/LambdaComp/Driver.hs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE GADTs #-}
2+
{-# LANGUAGE LambdaCase #-}
23
{-# LANGUAGE OverloadedStrings #-}
34
module LambdaComp.Driver where
45

@@ -7,6 +8,7 @@ import Paths_lambdacomp (getDataDir)
78
import Control.Exception (bracketOnError)
89
import Control.Monad.Except (ExceptT (ExceptT, runExceptT), MonadError (throwError))
910
import Control.Monad.Trans (MonadTrans (lift))
11+
import Data.Functor ((<&>))
1012
import Data.Text.IO qualified as T
1113
import System.Directory (makeAbsolute)
1214
import System.Exit (ExitCode (ExitFailure, ExitSuccess))
@@ -31,7 +33,7 @@ import LambdaComp.External.ToElaborated (ElaborationError, runToElaborat
3133
import LambdaComp.Parser (runProgramParser)
3234

3335
mainFuncWithOptions :: 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

6465
handleElabError :: Handle -> Either ElaborationError El.Program -> ExceptT Int IO El.Program
6566
handleElabError 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

Comments
 (0)