2022-05-18 18:10:10 +03:00
|
|
|
module App where
|
|
|
|
|
2022-08-12 10:48:06 +03:00
|
|
|
import Data.ByteString qualified as ByteString
|
2022-05-18 18:10:10 +03:00
|
|
|
import GlobalOptions
|
2022-08-03 14:20:40 +03:00
|
|
|
import Juvix.Compiler.Pipeline
|
|
|
|
import Juvix.Data.Error qualified as Error
|
2022-07-08 14:59:45 +03:00
|
|
|
import Juvix.Prelude hiding (Doc)
|
|
|
|
import Juvix.Prelude.Pretty hiding (Doc)
|
2022-05-18 18:10:10 +03:00
|
|
|
import System.Console.ANSI qualified as Ansi
|
|
|
|
|
|
|
|
data App m a where
|
2022-06-09 17:36:07 +03:00
|
|
|
ExitMsg :: ExitCode -> Text -> App m ()
|
2022-07-08 14:59:45 +03:00
|
|
|
ExitJuvixError :: JuvixError -> App m a
|
2022-08-30 12:24:15 +03:00
|
|
|
PrintJuvixError :: JuvixError -> App m ()
|
2022-06-09 17:36:07 +03:00
|
|
|
ReadGlobalOptions :: App m GlobalOptions
|
2022-05-18 18:10:10 +03:00
|
|
|
RenderStdOut :: (HasAnsiBackend a, HasTextBackend a) => a -> App m ()
|
2022-07-08 14:59:45 +03:00
|
|
|
RunPipelineEither :: Sem PipelineEff a -> App m (Either JuvixError a)
|
2022-06-09 17:36:07 +03:00
|
|
|
Say :: Text -> App m ()
|
2022-08-12 10:48:06 +03:00
|
|
|
Raw :: ByteString -> App m ()
|
2022-05-18 18:10:10 +03:00
|
|
|
|
|
|
|
makeSem ''App
|
|
|
|
|
|
|
|
runAppIO :: forall r a. Member (Embed IO) r => GlobalOptions -> Sem (App ': r) a -> Sem r a
|
|
|
|
runAppIO g = interpret $ \case
|
|
|
|
RenderStdOut t
|
|
|
|
| g ^. globalOnlyErrors -> return ()
|
|
|
|
| otherwise -> embed $ do
|
|
|
|
sup <- Ansi.hSupportsANSI stdout
|
|
|
|
renderIO (not (g ^. globalNoColors) && sup) t
|
|
|
|
ReadGlobalOptions -> return g
|
|
|
|
RunPipelineEither p -> embed (runIOEither p)
|
|
|
|
Say t
|
|
|
|
| g ^. globalOnlyErrors -> return ()
|
|
|
|
| otherwise -> embed (putStrLn t)
|
2022-08-30 12:24:15 +03:00
|
|
|
PrintJuvixError e -> do
|
|
|
|
printErr e
|
2022-07-08 14:59:45 +03:00
|
|
|
ExitJuvixError e -> do
|
2022-08-30 12:24:15 +03:00
|
|
|
printErr e
|
2022-05-18 18:10:10 +03:00
|
|
|
embed exitFailure
|
2022-06-09 17:36:07 +03:00
|
|
|
ExitMsg exitCode t -> embed (putStrLn t >> exitWith exitCode)
|
2022-08-12 10:48:06 +03:00
|
|
|
Raw b -> embed (ByteString.putStr b)
|
2022-08-30 12:24:15 +03:00
|
|
|
where
|
|
|
|
printErr e =
|
2022-09-01 14:22:32 +03:00
|
|
|
embed $ hPutStrLn stderr $ run $ runReader (genericFromGlobalOptions g) $ Error.render (not (g ^. globalNoColors)) (g ^. globalOnlyErrors) e
|
2022-05-18 18:10:10 +03:00
|
|
|
|
|
|
|
runPipeline :: Member App r => Sem PipelineEff a -> Sem r a
|
|
|
|
runPipeline p = do
|
|
|
|
r <- runPipelineEither p
|
|
|
|
case r of
|
2022-07-08 14:59:45 +03:00
|
|
|
Left err -> exitJuvixError err
|
2022-05-18 18:10:10 +03:00
|
|
|
Right res -> return res
|
|
|
|
|
|
|
|
newline :: Member App r => Sem r ()
|
|
|
|
newline = say ""
|
2022-06-09 17:36:07 +03:00
|
|
|
|
|
|
|
printSuccessExit :: Member App r => Text -> Sem r ()
|
|
|
|
printSuccessExit = exitMsg ExitSuccess
|
|
|
|
|
|
|
|
printFailureExit :: Member App r => Text -> Sem r ()
|
|
|
|
printFailureExit = exitMsg (ExitFailure 1)
|