Skip to content

Commit

Permalink
Nockma compile refactor (#2582)
Browse files Browse the repository at this point in the history
This PR contains refactors split out from the Nockma compile PR
#2570. Each refactor is associated
with a separate commit in this PR.
  • Loading branch information
paulcadman authored Jan 16, 2024
1 parent fa2a731 commit 5178979
Show file tree
Hide file tree
Showing 38 changed files with 127 additions and 134 deletions.
30 changes: 16 additions & 14 deletions app/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import System.Console.ANSI qualified as Ansi

data App m a where
ExitMsg :: ExitCode -> Text -> App m a
ExitFailMsg :: Text -> App m a
ExitJuvixError :: JuvixError -> App m a
PrintJuvixError :: JuvixError -> App m ()
AskRoot :: App m Root
Expand Down Expand Up @@ -78,20 +79,21 @@ reAppIO args@RunAppIOArgs {..} =
AskBuildDir -> return (resolveAbsBuildDir (_runAppIOArgsRoot ^. rootRootDir) (_runAppIOArgsRoot ^. rootBuildDir))
Say t
| g ^. globalOnlyErrors -> return ()
| otherwise -> embed (putStrLn t)
| otherwise -> putStrLn t
PrintJuvixError e -> do
printErr e
ExitJuvixError e -> do
printErr e
embed exitFailure
ExitMsg exitCode t -> exitMsg' exitCode t
ExitMsg exitCode t -> exitMsg' (exitWith exitCode) t
ExitFailMsg t -> exitMsg' exitFailure t
SayRaw b -> embed (ByteString.putStr b)
where
getPkg :: (Members '[SCache Package] r') => Sem r' Package
getPkg = cacheSingletonGet

exitMsg' :: (Members '[Embed IO] r') => ExitCode -> Text -> Sem r' x
exitMsg' exitCode t = liftIO (putStrLn t >> hFlush stdout >> exitWith exitCode)
exitMsg' :: (Members '[Embed IO] r') => IO x -> Text -> Sem r' x
exitMsg' onExit t = liftIO (putStrLn t >> hFlush stdout >> onExit)

getMainFile' :: (Members '[SCache Package, Embed IO] r') => Maybe (AppPath File) -> Sem r' (Path Abs File)
getMainFile' = \case
Expand All @@ -105,7 +107,7 @@ reAppIO args@RunAppIOArgs {..} =
missingMainErr :: (Members '[Embed IO] r') => Sem r' x
missingMainErr =
exitMsg'
(ExitFailure 1)
exitFailure
( "A path to the main file must be given in the CLI or specified in the `main` field of the "
<> pack (toFilePath juvixYamlFile)
<> " file"
Expand All @@ -127,9 +129,9 @@ getEntryPoint' RunAppIOArgs {..} inputFile = do
set entryPointStdin estdin <$> entryPointFromGlobalOptionsPre root (inputFile ^. pathPath) opts

runPipelineEither :: (Members '[Embed IO, TaggedLock, App] r) => AppPath File -> Sem (PipelineEff r) a -> Sem r (Either JuvixError (ResolverState, PipelineResult a))
runPipelineEither input p = do
runPipelineEither input_ p = do
args <- askArgs
entry <- getEntryPoint' args input
entry <- getEntryPoint' args input_
runIOEither entry p

runPipelineSetupEither :: (Members '[Embed IO, TaggedLock, App] r) => Sem (PipelineEff' r) a -> Sem r (Either JuvixError (ResolverState, a))
Expand Down Expand Up @@ -174,28 +176,28 @@ getEntryPointStdin = do
getEntryPointStdin' (RunAppIOArgs {..})

runPipelineTermination :: (Members '[Embed IO, App, TaggedLock] r) => AppPath File -> Sem (Termination ': PipelineEff r) a -> Sem r (PipelineResult a)
runPipelineTermination input p = do
r <- runPipelineEither input (evalTermination iniTerminationState p)
runPipelineTermination input_ p = do
r <- runPipelineEither input_ (evalTermination iniTerminationState p)
case r of
Left err -> exitJuvixError err
Right res -> return (snd res)

runPipeline :: (Members '[App, Embed IO, TaggedLock] r) => AppPath File -> Sem (PipelineEff r) a -> Sem r a
runPipeline input p = do
r <- runPipelineEither input p
runPipeline input_ p = do
r <- runPipelineEither input_ p
case r of
Left err -> exitJuvixError err
Right res -> return (snd res ^. pipelineResult)

runPipelineHtml :: (Members '[App, Embed IO, TaggedLock] r) => Bool -> AppPath File -> Sem r (InternalTypedResult, [InternalTypedResult])
runPipelineHtml bNonRecursive input =
runPipelineHtml bNonRecursive input_ =
if
| bNonRecursive -> do
r <- runPipeline input upToInternalTyped
r <- runPipeline input_ upToInternalTyped
return (r, [])
| otherwise -> do
args <- askArgs
entry <- getEntryPoint' args input
entry <- getEntryPoint' args input_
r <- runPipelineHtmlEither entry
case r of
Left err -> exitJuvixError err
Expand Down
2 changes: 1 addition & 1 deletion app/AsmInterpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ runAsm bValidate tab =
return ()
Right val -> do
renderStdOut (Asm.ppOut (Asm.defaultOptions tab) val)
embed (putStrLn "")
putStrLn ""
Nothing ->
exitMsg (ExitFailure 1) "no 'main' function"
where
Expand Down
3 changes: 1 addition & 2 deletions app/Commands/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Commands.Base
import Commands.Compile.Options
import Commands.Dev.Core.Compile.Base qualified as Compile
import Commands.Extra.Compile qualified as Compile
import Data.Text.IO qualified as TIO
import Juvix.Compiler.Core qualified as Core
import Juvix.Compiler.Core.Pretty qualified as Core
import Juvix.Compiler.Core.Transformation.DisambiguateNames qualified as Core
Expand Down Expand Up @@ -35,4 +34,4 @@ writeCoreFile pa@Compile.PipelineArg {..} = do
case r of
Left e -> exitJuvixError e
Right md ->
embed $ TIO.writeFile (toFilePath coreFile) (show $ Core.ppOutDefault (Core.disambiguateNames md ^. Core.moduleInfoTable))
embed @IO (writeFile (toFilePath coreFile) (show $ Core.ppOutDefault (Core.disambiguateNames md ^. Core.moduleInfoTable)))
3 changes: 1 addition & 2 deletions app/Commands/Dev/Asm/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Commands.Dev.Asm.Compile where
import Commands.Base
import Commands.Dev.Asm.Compile.Options
import Commands.Extra.Compile qualified as Compile
import Data.Text.IO qualified as TIO
import Juvix.Compiler.Asm.Translation.FromSource qualified as Asm
import Juvix.Compiler.Backend qualified as Backend
import Juvix.Compiler.Backend.C qualified as C
Expand All @@ -29,7 +28,7 @@ runCommand opts = do
buildDir <- askBuildDir
ensureDir buildDir
cFile <- inputCFile file
embed $ TIO.writeFile (toFilePath cFile) _resultCCode
embed @IO (writeFile (toFilePath cFile) _resultCCode)
outfile <- Compile.outputFile opts file
Compile.runCommand
opts
Expand Down
9 changes: 4 additions & 5 deletions app/Commands/Dev/Core/Compile/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Commands.Dev.Core.Compile.Base where
import Commands.Base
import Commands.Dev.Core.Compile.Options
import Commands.Extra.Compile qualified as Compile
import Data.Text.IO qualified as TIO
import Juvix.Compiler.Asm.Pretty qualified as Asm
import Juvix.Compiler.Backend qualified as Backend
import Juvix.Compiler.Backend.C qualified as C
Expand Down Expand Up @@ -53,7 +52,7 @@ runCPipeline pa@PipelineArg {..} = do
entryPoint <- getEntry pa
C.MiniCResult {..} <- getRight (run (runReader entryPoint (runError (coreToMiniC _pipelineArgModule :: Sem '[Error JuvixError, Reader EntryPoint] C.MiniCResult))))
cFile <- inputCFile _pipelineArgFile
embed $ TIO.writeFile (toFilePath cFile) _resultCCode
embed @IO (writeFile (toFilePath cFile) _resultCCode)
outfile <- Compile.outputFile _pipelineArgOptions _pipelineArgFile
Compile.runCommand
_pipelineArgOptions
Expand Down Expand Up @@ -85,7 +84,7 @@ runGebPipeline pa@PipelineArg {..} = do
_lispPackageEntry = "*entry*"
}
Geb.Result {..} <- getRight (run (runReader entryPoint (runError (coreToGeb spec _pipelineArgModule :: Sem '[Error JuvixError, Reader EntryPoint] Geb.Result))))
embed $ TIO.writeFile (toFilePath gebFile) _resultCode
embed @IO (writeFile (toFilePath gebFile) _resultCode)

runVampIRPipeline ::
forall r.
Expand All @@ -96,7 +95,7 @@ runVampIRPipeline pa@PipelineArg {..} = do
entryPoint <- getEntry pa
vampirFile <- Compile.outputFile _pipelineArgOptions _pipelineArgFile
VampIR.Result {..} <- getRight (run (runReader entryPoint (runError (coreToVampIR _pipelineArgModule :: Sem '[Error JuvixError, Reader EntryPoint] VampIR.Result))))
embed $ TIO.writeFile (toFilePath vampirFile) _resultCode
embed @IO (writeFile (toFilePath vampirFile) _resultCode)

runAsmPipeline :: (Members '[Embed IO, App, TaggedLock] r) => PipelineArg -> Sem r ()
runAsmPipeline pa@PipelineArg {..} = do
Expand All @@ -105,4 +104,4 @@ runAsmPipeline pa@PipelineArg {..} = do
r <- runReader entryPoint $ runError @JuvixError (coreToAsm _pipelineArgModule)
tab' <- getRight r
let code = Asm.ppPrint tab' tab'
embed $ TIO.writeFile (toFilePath asmFile) code
embed @IO (writeFile (toFilePath asmFile) code)
12 changes: 6 additions & 6 deletions app/Commands/Dev/Core/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ runCommand opts = do
doEval tab' node =
if
| project opts ^. coreReadEval -> do
embed (putStrLn "--------------------------------")
embed (putStrLn "| Eval |")
embed (putStrLn "--------------------------------")
putStrLn "--------------------------------"
putStrLn "| Eval |"
putStrLn "--------------------------------"
Eval.evalAndPrint opts tab' node
| project opts ^. coreReadNormalize -> do
embed (putStrLn "--------------------------------")
embed (putStrLn "| Normalize |")
embed (putStrLn "--------------------------------")
putStrLn "--------------------------------"
putStrLn "| Normalize |"
putStrLn "--------------------------------"
Eval.normalizeAndPrint opts tab' node
| otherwise -> return ()
sinputFile :: AppPath File
Expand Down
12 changes: 6 additions & 6 deletions app/Commands/Dev/Core/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ parseText = Core.runParser replPath defaultModuleId

runRepl :: forall r. (Members '[Embed IO, App] r) => CoreReplOptions -> Core.InfoTable -> Sem r ()
runRepl opts tab = do
embed (putStr "> ")
putStr "> "
embed (hFlush stdout)
done <- embed isEOF
unless done $ do
Expand All @@ -43,7 +43,7 @@ runRepl opts tab = do
runRepl opts tab
Right (tab', Just node) -> do
renderStdOut (Core.ppOut opts node)
embed (putStrLn "")
putStrLn ""
runRepl opts tab'
Right (tab', Nothing) ->
runRepl opts tab'
Expand Down Expand Up @@ -107,7 +107,7 @@ runRepl opts tab = do
| Info.member Info.kNoDisplayInfo (Core.getInfo node') -> runRepl opts tab'
| otherwise -> do
renderStdOut (Core.ppOut opts (Core.disambiguateNodeNames (Core.moduleFromInfoTable tab') node'))
embed (putStrLn "")
putStrLn ""
runRepl opts tab'
where
defaultLoc = singletonInterval (mkInitialLoc replPath)
Expand All @@ -121,19 +121,19 @@ runRepl opts tab = do
runRepl opts tab'
| otherwise -> do
renderStdOut (Core.ppOut opts (Core.disambiguateNodeNames md' node'))
embed (putStrLn "")
putStrLn ""
runRepl opts tab'

replType :: Core.InfoTable -> Core.Node -> Sem r ()
replType tab' node = do
let md' = Core.moduleFromInfoTable tab'
ty = Core.disambiguateNodeNames md' (Core.computeNodeType md' node)
renderStdOut (Core.ppOut opts ty)
embed (putStrLn "")
putStrLn ""
runRepl opts tab'

showReplWelcome :: (Members '[Embed IO, App] r) => Sem r ()
showReplWelcome = embed $ do
showReplWelcome = do
putStrLn "JuvixCore REPL"
putStrLn ""
putStrLn "Type \":h\" for help."
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Geb/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ runCommand opts = do
Left err -> exitJuvixError (JuvixError err)
Right ty -> do
renderStdOut (ppOutDefault ty)
embed (putStrLn "")
putStrLn ""
Right _ -> exitJuvixError (error @JuvixError "Not a morphism")
Left err -> exitJuvixError (JuvixError err)
2 changes: 1 addition & 1 deletion app/Commands/Dev/Geb/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ runCommand opts = do
Left err -> exitJuvixError (JuvixError err)
Right gebTerm -> do
evalAndPrint opts gebTerm
embed (putStrLn "")
putStrLn ""

evalAndPrint ::
forall r a.
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Geb/Infer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ runCommand opts = do
Geb.ppOut
opts
(tyMorph ^. Geb.typedMorphismObject)
embed $ putStrLn ""
putStrLn ""
Right (Geb.ExpressionObject _) ->
exitJuvixError (error @JuvixError "No inference for objects")
Left err -> exitJuvixError (JuvixError err)
2 changes: 1 addition & 1 deletion app/Commands/Dev/Geb/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ runCommand opts = do
Left err -> exitJuvixError (JuvixError err)
Right gebTerm -> do
renderStdOut (Geb.ppOut opts gebTerm)
embed (putStrLn "")
putStrLn ""
8 changes: 4 additions & 4 deletions app/Commands/Dev/Geb/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,25 @@ checkTypedMorphism gebMorphism = Repline.dontCrash $ do
Right _ -> printError (error "Checking only works on typed Geb morphisms.")

runReplCommand :: String -> Repl ()
runReplCommand input =
runReplCommand input_ =
Repline.dontCrash $
do
let evalRes =
Geb.runEval $
Geb.RunEvalArgs
{ _runEvalArgsContent = pack input,
{ _runEvalArgsContent = pack input_,
_runEvalArgsInputFile = gebReplPath,
_runEvalArgsEvaluatorOptions = Geb.defaultEvaluatorOptions
}
printEvalResult evalRes

evalAndOutputMorphism :: String -> Repl ()
evalAndOutputMorphism input =
evalAndOutputMorphism input_ =
Repline.dontCrash $ do
let evalRes =
Geb.runEval $
Geb.RunEvalArgs
{ _runEvalArgsContent = pack input,
{ _runEvalArgsContent = pack input_,
_runEvalArgsInputFile = gebReplPath,
_runEvalArgsEvaluatorOptions =
Geb.defaultEvaluatorOptions
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Dev/Nockma/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ evalStatement = \case
Right res -> liftIO (putStrLn (ppPrint res))

replCommand :: String -> Repl ()
replCommand input = Repline.dontCrash $ do
readStatement input >>= evalStatement
replCommand input_ = Repline.dontCrash $ do
readStatement input_ >>= evalStatement

replAction :: ReplS ()
replAction =
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Init.hs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ getProjName = do
go

say :: (Members '[Embed IO] r) => Text -> Sem r ()
say = embed . putStrLn
say = putStrLn

tryAgain :: (Members '[Embed IO] r) => Sem r ()
tryAgain = say "Please, try again:"
Expand Down
22 changes: 11 additions & 11 deletions app/Commands/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ displayVersion :: String -> Repl ()
displayVersion _ = liftIO (putStrLn versionTag)

replCommand :: ReplOptions -> String -> Repl ()
replCommand opts input = catchAll $ do
replCommand opts input_ = catchAll $ do
ctx <- replGetContext
let tab = Core.computeCombinedInfoTable $ ctx ^. replContextArtifacts . artifactCoreModule
evalRes <- compileThenEval ctx input
evalRes <- compileThenEval ctx input_
whenJust evalRes $ \n ->
if
| Info.member Info.kNoDisplayInfo (Core.getInfo n) -> return ()
Expand Down Expand Up @@ -200,22 +200,22 @@ replCommand opts input = catchAll $ do
return res'

core :: String -> Repl ()
core input = do
core input_ = do
ctx <- replGetContext
opts <- Reader.asks (^. replOptions)
compileRes <- liftIO (compileReplInputIO' ctx (strip (pack input))) >>= replFromEither . snd
compileRes <- liftIO (compileReplInputIO' ctx (strip (pack input_))) >>= replFromEither . snd
whenJust compileRes (renderOutLn . Core.ppOut opts)

dev :: String -> Repl ()
dev input = do
dev input_ = do
ctx <- replGetContext
if
| input == scoperStateCmd -> do
| input_ == scoperStateCmd -> do
renderOutLn (Concrete.ppTrace (ctx ^. replContextArtifacts . artifactScoperState))
| otherwise ->
renderOutLn
( "Unrecognized command "
<> input
<> input_
<> "\nAvailable commands: "
<> unwords cmds
)
Expand All @@ -238,8 +238,8 @@ printConcreteLn :: (Concrete.PrettyPrint a) => a -> Repl ()
printConcreteLn = ppConcrete >=> renderOutLn

replParseIdentifiers :: String -> Repl (NonEmpty Concrete.ScopedIden)
replParseIdentifiers input =
replExpressionUpToScopedAtoms (strip (pack input))
replParseIdentifiers input_ =
replExpressionUpToScopedAtoms (strip (pack input_))
>>= getIdentifiers
where
getIdentifiers :: Concrete.ExpressionAtoms 'Concrete.Scoped -> Repl (NonEmpty Concrete.ScopedIden)
Expand Down Expand Up @@ -375,9 +375,9 @@ printDefinition = replParseIdentifiers >=> printIdentifiers
printInductive (ind ^. Scoped.nameId)

inferType :: String -> Repl ()
inferType input = do
inferType input_ = do
gopts <- State.gets (^. replStateGlobalOptions)
n <- replExpressionUpToTyped (strip (pack input))
n <- replExpressionUpToTyped (strip (pack input_))
renderOutLn (Internal.ppOut (project' @GenericOptions gopts) (n ^. Internal.typedType))

replCommands :: ReplOptions -> [(String, String -> Repl ())]
Expand Down
2 changes: 1 addition & 1 deletion app/Evaluator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ normalizeAndPrint opts tab node =
| otherwise -> do
let node'' = if project opts ^. evalNoDisambiguate then node' else Core.disambiguateNodeNames (Core.moduleFromInfoTable tab) node'
renderStdOut (Core.ppOut opts node'')
embed (putStrLn "")
putStrLn ""
Loading

0 comments on commit 5178979

Please sign in to comment.