Skip to content

Commit

Permalink
Refactor readFile and some parsers to use Path instead of `FilePa…
Browse files Browse the repository at this point in the history
…th` (#2649)

Now the prelude exports this function:
```
readFile :: (MonadIO m) => Path Abs File -> m Text
readFile = liftIO . Utf8.readFile . toFilePath
```
It is more convenient to use because it uses typed `Path` and works in
any `MonadIO`.
  • Loading branch information
janmasrovira authored Feb 19, 2024
1 parent 86e8458 commit a825f41
Show file tree
Hide file tree
Showing 55 changed files with 142 additions and 138 deletions.
4 changes: 2 additions & 2 deletions app/Commands/Dev/Asm/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import Juvix.Compiler.Reg.Pretty qualified as Reg
runCommand :: forall r. (Members '[EmbedIO, App, TaggedLock] r) => AsmCompileOptions -> Sem r ()
runCommand opts = do
file <- getFile
s <- readFile (toFilePath file)
case Asm.runParser (toFilePath file) s of
s <- readFile file
case Asm.runParser file s of
Left err -> exitJuvixError (JuvixError err)
Right tab -> do
ep <- getEntryPoint (AppPath (preFileFromAbs file) True)
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Dev/Asm/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import Juvix.Compiler.Asm.Translation.FromSource qualified as Asm
runCommand :: forall r. (Members '[EmbedIO, App] r) => AsmRunOptions -> Sem r ()
runCommand opts = do
afile :: Path Abs File <- fromAppPathFile file
s <- readFile (toFilePath afile)
case Asm.runParser (toFilePath afile) s of
s <- readFile afile
case Asm.runParser afile s of
Left err -> exitJuvixError (JuvixError err)
Right tab -> runAsm (not (opts ^. asmRunNoValidate)) tab
where
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Dev/Asm/Validate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import Juvix.Compiler.Asm.Translation.FromSource qualified as Asm
runCommand :: forall r. (Members '[EmbedIO, App] r) => AsmValidateOptions -> Sem r ()
runCommand opts = do
afile :: Path Abs File <- fromAppPathFile file
s <- readFile (toFilePath afile)
case Asm.runParser (toFilePath afile) s of
s <- readFile afile
case Asm.runParser afile s of
Left err -> exitJuvixError (JuvixError err)
Right tab -> do
case Asm.validate' tab of
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Dev/Casm/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import Juvix.Compiler.Casm.Validate qualified as Casm
runCommand :: forall r. (Members '[EmbedIO, App] r) => CasmReadOptions -> Sem r ()
runCommand opts = do
afile :: Path Abs File <- fromAppPathFile file
s <- readFile (toFilePath afile)
case Casm.runParser (toFilePath afile) s of
s <- readFile afile
case Casm.runParser afile s of
Left err -> exitJuvixError (JuvixError err)
Right (labi, code) ->
case Casm.validate labi code of
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Dev/Casm/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import Juvix.Compiler.Casm.Validate qualified as Casm
runCommand :: forall r. (Members '[EmbedIO, App] r) => CasmRunOptions -> Sem r ()
runCommand opts = do
afile :: Path Abs File <- fromAppPathFile file
s <- readFile (toFilePath afile)
case Casm.runParser (toFilePath afile) s of
s <- readFile afile
case Casm.runParser afile s of
Left err -> exitJuvixError (JuvixError err)
Right (labi, code) ->
case Casm.validate labi code of
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Core/Asm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ runCommand :: forall r a. (Members '[EmbedIO, App, TaggedLock] r, CanonicalProje
runCommand opts = do
inputFile :: Path Abs File <- fromAppPathFile sinputFile
ep <- getEntryPoint sinputFile
s' <- readFile $ toFilePath inputFile
s' <- readFile inputFile
tab <- getRight (mapLeft JuvixError (Core.runParserMain inputFile defaultModuleId mempty s'))
r <- runReader ep . runError @JuvixError $ coreToAsm (Core.moduleFromInfoTable tab)
tab' <- getRight r
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Core/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Juvix.Compiler.Core.Translation.FromSource qualified as Core
runCommand :: forall r. (Members '[EmbedIO, App, TaggedLock] r) => CompileOptions -> Sem r ()
runCommand opts = do
file <- getFile
s <- readFile (toFilePath file)
s <- readFile file
tab <- getRight (mapLeft JuvixError (Core.runParserMain file defaultModuleId mempty s))
let arg = PipelineArg opts file (Core.moduleFromInfoTable tab)
case opts ^. compileTarget of
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Core/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Juvix.Compiler.Core.Translation.FromSource qualified as Core
runCommand :: forall r. (Members '[EmbedIO, App] r) => CoreEvalOptions -> Sem r ()
runCommand opts = do
f :: Path Abs File <- fromAppPathFile b
s <- readFile (toFilePath f)
s <- readFile f
case Core.runParser f defaultModuleId mempty s of
Left err -> exitJuvixError (JuvixError err)
Right (tab, Just node) -> do evalAndPrint opts tab node
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Core/Normalize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Juvix.Compiler.Core.Translation.FromSource qualified as Core
runCommand :: forall r. (Members '[EmbedIO, App] r) => CoreNormalizeOptions -> Sem r ()
runCommand opts = do
f :: Path Abs File <- fromAppPathFile b
s <- readFile (toFilePath f)
s <- readFile f
case Core.runParser f defaultModuleId mempty s of
Left err -> exitJuvixError (JuvixError err)
Right (tab, Just node) -> do normalizeAndPrint opts tab node
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Core/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ runCommand ::
runCommand opts = do
gopts <- askGlobalOptions
inputFile :: Path Abs File <- fromAppPathFile sinputFile
s' <- readFile . toFilePath $ inputFile
s' <- readFile inputFile
tab <- getRight (mapLeft JuvixError (Core.runParserMain inputFile defaultModuleId mempty s'))
let r = run $ runReader (project @GlobalOptions @Core.CoreOptions gopts) $ runError @JuvixError $ Core.applyTransformations (project opts ^. coreReadTransformations) (Core.moduleFromInfoTable tab)
tab0 <- getRight $ mapLeft JuvixError r
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Core/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ runRepl opts tab = do
Right (tab', Nothing) ->
runRepl opts tab'
':' : 'l' : ' ' : f -> do
s' <- readFile f
s' <- readFile (absFile f)
sf <- someBaseToAbs' (someFile f)
case Core.runParser sf defaultModuleId mempty s' of
Left err -> do
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Core/Strip.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ runCommand :: forall r a. (Members '[EmbedIO, App] r, CanonicalProjection a Core
runCommand opts = do
gopts <- askGlobalOptions
inputFile :: Path Abs File <- fromAppPathFile sinputFile
s' <- readFile $ toFilePath inputFile
s' <- readFile inputFile
(tab, _) <- getRight (mapLeft JuvixError (Core.runParser inputFile defaultModuleId mempty s'))
let r =
run $
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 @@ -14,7 +14,7 @@ runCommand opts = do
let b :: AppPath File
b = opts ^. gebInferOptionsInputFile
f :: Path Abs File <- fromAppPathFile b
content :: Text <- readFile (toFilePath f)
content :: Text <- readFile f
case Geb.runParser f content of
Right (Geb.ExpressionMorphism morph) -> do
case Geb.inferObject' morph of
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Geb/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ runCommand opts = do
let b :: AppPath File
b = project opts ^. gebEvalOptionsInputFile
f :: Path Abs File <- fromAppPathFile b
content :: Text <- readFile (toFilePath f)
content :: Text <- readFile f
case Geb.runParser f content of
Left err -> exitJuvixError (JuvixError err)
Right gebTerm -> do
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 @@ -13,7 +13,7 @@ runCommand opts = do
let b :: AppPath File
b = opts ^. gebInferOptionsInputFile
f :: Path Abs File <- fromAppPathFile b
content :: Text <- readFile (toFilePath f)
content :: Text <- readFile f
case Geb.runParser f content of
Right (Geb.ExpressionMorphism gebTerm) ->
case Geb.inferObject' gebTerm of
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Geb/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ runCommand opts = do
let b :: AppPath File
b = opts ^. gebReadOptionsInputFile
f :: Path Abs File <- fromAppPathFile b
content :: Text <- readFile (toFilePath f)
content :: Text <- readFile f
case Geb.runParser f content of
Left err -> exitJuvixError (JuvixError err)
Right gebTerm -> do
Expand Down
5 changes: 2 additions & 3 deletions app/Commands/Dev/Geb/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ loadEntryPoint ep = do
)
let epPath :: Maybe (Path Abs File) = ep ^. entryPointModulePath
whenJust epPath $ \path -> do
let filepath = toFilePath path
liftIO (putStrLn . pack $ "OK loaded " <> filepath)
content <- liftIO (readFile filepath)
liftIO (putStrLn . pack $ "OK loaded " <> toFilePath path)
content <- liftIO (readFile path)
let evalRes =
Geb.runEval $
Geb.RunEvalArgs
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Nockma/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Juvix.Compiler.Nockma.Translation.FromSource qualified as Nockma
runCommand :: forall r. (Members '[EmbedIO, App] r) => NockmaEvalOptions -> Sem r ()
runCommand opts = do
afile <- fromAppPathFile file
parsedTerm <- Nockma.parseTermFile (toFilePath afile)
parsedTerm <- Nockma.parseTermFile afile
case parsedTerm of
Left err -> exitJuvixError (JuvixError err)
Right (TermCell c) -> do
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Nockma/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Juvix.Compiler.Nockma.Translation.FromSource qualified as Nockma
runCommand :: forall r. (Members '[EmbedIO, App] r) => NockmaFormatOptions -> Sem r ()
runCommand opts = do
afile <- fromAppPathFile file
parsedTerm <- Nockma.parseTermFile (toFilePath afile)
parsedTerm <- Nockma.parseTermFile afile
case parsedTerm of
Left err -> exitJuvixError (JuvixError err)
Right t -> putStrLn (ppPrint t)
Expand Down
9 changes: 5 additions & 4 deletions app/Commands/Dev/Nockma/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Juvix.Compiler.Nockma.Pretty
import Juvix.Compiler.Nockma.Pretty qualified as Nockma
import Juvix.Compiler.Nockma.Translation.FromSource (parseProgramFile, parseReplStatement, parseReplText, parseText)
import Juvix.Parser.Error
import Juvix.Prelude qualified as Prelude
import System.Console.Haskeline
import System.Console.Repline qualified as Repline
import Prelude (read)
Expand All @@ -21,7 +22,7 @@ type ReplS = State.StateT ReplState IO
data ReplState = ReplState
{ _replStateProgram :: Maybe (Program Natural),
_replStateStack :: Maybe (Term Natural),
_replStateLoadedFile :: Maybe (FilePath)
_replStateLoadedFile :: Maybe (Prelude.Path Abs File)
}

type Repl a = Repline.HaskelineT ReplS a
Expand Down Expand Up @@ -62,7 +63,7 @@ setStack s = Repline.dontCrash $ do
newStack <- readReplTerm s
State.modify (set replStateStack (Just newStack))

loadFile :: String -> Repl ()
loadFile :: Prelude.Path Abs File -> Repl ()
loadFile s = Repline.dontCrash $ do
State.modify (set replStateLoadedFile (Just s))
prog <- readProgram s
Expand All @@ -82,7 +83,7 @@ options =
[ ("quit", quit),
("get-stack", printStack),
("set-stack", setStack),
("load", loadFile),
("load", loadFile . Prelude.absFile),
("reload", const reloadFile),
("dir", direction')
]
Expand All @@ -98,7 +99,7 @@ getStack = State.gets (^. replStateStack)
getProgram :: Repl (Maybe (Program Natural))
getProgram = State.gets (^. replStateProgram)

readProgram :: FilePath -> Repl (Program Natural)
readProgram :: Prelude.Path Abs File -> Repl (Program Natural)
readProgram s = fromMegaParsecError <$> parseProgramFile s

direction' :: String -> Repl ()
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Dev/Reg/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import RegInterpreter
runCommand :: forall r. (Members '[EmbedIO, App] r) => RegReadOptions -> Sem r ()
runCommand opts = do
afile :: Path Abs File <- fromAppPathFile file
s <- readFile (toFilePath afile)
case Reg.runParser (toFilePath afile) s of
s <- readFile afile
case Reg.runParser afile s of
Left err ->
exitJuvixError (JuvixError err)
Right tab -> do
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Dev/Reg/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import RegInterpreter
runCommand :: forall r. (Members '[Embed IO, App] r) => RegRunOptions -> Sem r ()
runCommand opts = do
afile :: Path Abs File <- fromAppPathFile file
s <- readFile (toFilePath afile)
case Reg.runParser (toFilePath afile) s of
s <- readFile afile
case Reg.runParser afile s of
Left err -> exitJuvixError (JuvixError err)
Right tab -> runReg tab
where
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Dev/Tree/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import Juvix.Compiler.Tree.Translation.FromSource qualified as Tree
runCommand :: forall r. (Members '[EmbedIO, App, TaggedLock] r) => CompileOptions -> Sem r ()
runCommand opts = do
file <- getFile
s <- readFile (toFilePath file)
tab <- getRight (mapLeft JuvixError (Tree.runParser (toFilePath file) s))
s <- readFile file
tab <- getRight (mapLeft JuvixError (Tree.runParser file s))
let arg = PipelineArg opts file tab
case opts ^. compileTarget of
TargetWasm32Wasi -> runCPipeline arg
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Dev/Tree/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import TreeEvaluator
runCommand :: forall r. (Members '[EmbedIO, App] r) => TreeEvalOptions -> Sem r ()
runCommand opts = do
afile :: Path Abs File <- fromAppPathFile file
s <- readFile (toFilePath afile)
case Tree.runParser (toFilePath afile) s of
s <- readFile afile
case Tree.runParser afile s of
Left err -> exitJuvixError (JuvixError err)
Right tab -> evalTree (opts ^. treeEvalEvaluator) tab
where
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Dev/Tree/FromAsm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import Juvix.Compiler.Tree.Translation.FromAsm qualified as Tree
runCommand :: forall r. (Members '[EmbedIO, App] r) => TreeFromAsmOptions -> Sem r ()
runCommand opts = do
afile :: Path Abs File <- fromAppPathFile file
s <- readFile (toFilePath afile)
case Asm.runParser (toFilePath afile) s of
s <- readFile afile
case Asm.runParser afile s of
Left err -> exitJuvixError (JuvixError err)
Right tab -> do
r :: Either JuvixError Tree.InfoTable <- runError $ mapError (JuvixError @TreeError) $ Tree.fromAsm tab
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/Dev/Tree/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import TreeEvaluator qualified as Eval
runCommand :: forall r. (Members '[EmbedIO, App] r) => TreeReadOptions -> Sem r ()
runCommand opts = do
afile :: Path Abs File <- fromAppPathFile file
s <- readFile (toFilePath afile)
case Tree.runParser (toFilePath afile) s of
s <- readFile afile
case Tree.runParser afile s of
Left err -> exitJuvixError (JuvixError err)
Right tab -> do
r <- runError @JuvixError (Tree.applyTransformations (project opts ^. treeReadTransformations) tab)
Expand Down
12 changes: 6 additions & 6 deletions app/Commands/Dev/Tree/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ReplS = State.StateT ReplState IO

data ReplState = ReplState
{ _replStateBuilderState :: Tree.BuilderState,
_replStateLoadedFile :: Maybe FilePath
_replStateLoadedFile :: Maybe (Path Abs File)
}

type Repl a = Repline.HaskelineT ReplS a
Expand All @@ -40,7 +40,7 @@ printHelpTxt = liftIO $ putStrLn helpTxt
quit :: String -> Repl ()
quit _ = liftIO (throwIO Interrupt)

loadFile :: String -> Repl ()
loadFile :: Path Abs File -> Repl ()
loadFile s = Repline.dontCrash $ do
State.modify (set replStateLoadedFile (Just s))
readProgram s
Expand All @@ -52,7 +52,7 @@ reloadFile = Repline.dontCrash $ do
Nothing -> error "no file loaded"
Just f -> readProgram f

readProgram :: FilePath -> Repl ()
readProgram :: Path Abs File -> Repl ()
readProgram f = do
bs <- State.gets (^. replStateBuilderState)
txt <- readFile f
Expand All @@ -65,7 +65,7 @@ options :: [(String, String -> Repl ())]
options =
[ ("help", Repline.dontCrash . const printHelpTxt),
("quit", quit),
("load", loadFile),
("load", loadFile . absFile),
("reload", const reloadFile)
]

Expand All @@ -83,8 +83,8 @@ readNode s = do
State.modify (set replStateBuilderState bs')
return n
where
replFile :: FilePath
replFile = "<file>"
replFile :: Path Abs File
replFile = $(mkAbsFile "/<repl>")

evalNode :: Node -> Repl ()
evalNode node = do
Expand Down
2 changes: 1 addition & 1 deletion bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ csvRules s =
csv :: Path Abs File = suiteCsvFile s
addColorColumn :: IO ()
addColorColumn = do
header :| rows <- nonEmpty' . Text.lines <$> readFile (toFilePath csv)
header :| rows <- nonEmpty' . Text.lines <$> readFile csv
let rows' =
[ showColour (v ^. variantColor) <> "," <> r
| (v, r) <- zipExact (s ^. suiteVariants) rows
Expand Down
11 changes: 7 additions & 4 deletions src/Juvix/Compiler/Asm/Translation/FromSource.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@ parseAsmSig =
_parserSigEmptyExtra = mempty
}

noFile :: Path Abs File
noFile = $(mkAbsFile "/<text>")

parseText :: Text -> Either MegaparsecError InfoTable
parseText = runParser ""
parseText = runParser noFile

parseText' :: BuilderState -> Text -> Either MegaparsecError BuilderState
parseText' bs = runParser' bs ""
parseText' bs = runParser' bs noFile

runParser :: FilePath -> Text -> Either MegaparsecError InfoTable
runParser :: Path Abs File -> Text -> Either MegaparsecError InfoTable
runParser = runParserS parseAsmSig

runParser' :: BuilderState -> FilePath -> Text -> Either MegaparsecError BuilderState
runParser' :: BuilderState -> Path Abs File -> Text -> Either MegaparsecError BuilderState
runParser' = runParserS' parseAsmSig

parseCode ::
Expand Down
6 changes: 3 additions & 3 deletions src/Juvix/Compiler/Casm/Translation/FromSource.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import Juvix.Parser.Error
import Text.Megaparsec qualified as P

parseText :: Text -> Either MegaparsecError (LabelInfo, [Instruction])
parseText = runParser ""
parseText = runParser $(mkAbsFile "/<text>")

runParser :: FilePath -> Text -> Either MegaparsecError (LabelInfo, [Instruction])
runParser :: Path Abs File -> Text -> Either MegaparsecError (LabelInfo, [Instruction])
runParser fileName input_ =
case run $ runLabelInfoBuilder $ P.runParserT parseToplevel fileName input_ of
case run . runLabelInfoBuilder $ P.runParserT parseToplevel (toFilePath fileName) input_ of
(_, Left err) -> Left (MegaparsecError err)
(li, Right instrs) -> Right (li, instrs)

Expand Down
Loading

0 comments on commit a825f41

Please sign in to comment.