Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options for juvix dev tree read #2599

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Commands/Dev/Core/FromConcrete/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ instance CanonicalProjection CoreFromConcreteOptions Eval.EvalOptions where

parseCoreFromConcreteOptions :: Parser CoreFromConcreteOptions
parseCoreFromConcreteOptions = do
_coreFromConcreteTransformations <- optTransformationIds
_coreFromConcreteTransformations <- optCoreTransformationIds
_coreFromConcreteShowDeBruijn <- optDeBruijn
_coreFromConcreteShowIdentIds <- optIdentIds
_coreFromConcreteShowArgsNum <- optArgsNum
Expand Down
8 changes: 4 additions & 4 deletions app/Commands/Dev/Core/Read/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ parseCoreReadOptions = do
_coreReadNoPrint <-
switch
( long "no-print"
<> help "do not print the transformed code"
<> help "Do not print the transformed code"
)
_coreReadEval <-
switch
( long "eval"
<> help "evaluate after the transformation"
<> help "Evaluate after the transformation"
)
_coreReadNormalize <-
switch
( long "normalize"
<> help "normalize after the transformation"
<> help "Normalize after the transformation"
)
_coreReadTransformations <- optTransformationIds
_coreReadTransformations <- optCoreTransformationIds
_coreReadInputFile <- parseInputFile FileExtJuvixCore
pure CoreReadOptions {..}
2 changes: 1 addition & 1 deletion app/Commands/Dev/Repl/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ parseDevRepl = do
_replIsDev = True
_replInputFile <- optional (parseInputFile FileExtJuvix)
_replTransformations <- do
ts <- optTransformationIds
ts <- optCoreTransformationIds
pure $
if
| null ts -> toStoredTransformations
Expand Down
18 changes: 17 additions & 1 deletion app/Commands/Dev/Tree/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@ module Commands.Dev.Tree.Read where

import Commands.Base
import Commands.Dev.Tree.Read.Options
import Juvix.Compiler.Tree.Data.InfoTable qualified as Tree
import Juvix.Compiler.Tree.Pretty qualified as Tree
import Juvix.Compiler.Tree.Transformation qualified as Tree
import Juvix.Compiler.Tree.Translation.FromSource qualified as Tree
import TreeEvaluator qualified as Eval

runCommand :: forall r. (Members '[Embed IO, 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
Left err -> exitJuvixError (JuvixError err)
Right tab -> renderStdOut (Tree.ppOutDefault tab tab)
Right tab -> do
tab' <- Tree.applyTransformations (project opts ^. treeReadTransformations) tab
unless (project opts ^. treeReadNoPrint) $
renderStdOut (Tree.ppOutDefault tab' tab')
doEval tab'
where
file :: AppPath File
file = opts ^. treeReadInputFile

doEval :: Tree.InfoTable -> Sem r ()
doEval tab'
| project opts ^. treeReadEval = do
putStrLn "--------------------------------"
putStrLn "| Eval |"
putStrLn "--------------------------------"
Eval.evalTree tab'
| otherwise = return ()
19 changes: 17 additions & 2 deletions app/Commands/Dev/Tree/Read/Options.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
module Commands.Dev.Tree.Read.Options where

import CommonOptions
import Juvix.Compiler.Tree.Data.TransformationId

newtype TreeReadOptions = TreeReadOptions
{ _treeReadInputFile :: AppPath File
data TreeReadOptions = TreeReadOptions
{ _treeReadTransformations :: [TransformationId],
_treeReadEval :: Bool,
_treeReadNoPrint :: Bool,
_treeReadInputFile :: AppPath File
}
deriving stock (Data)

makeLenses ''TreeReadOptions

parseTreeReadOptions :: Parser TreeReadOptions
parseTreeReadOptions = do
_treeReadNoPrint <-
switch
( long "no-print"
<> help "Do not print the transformed code"
)
_treeReadEval <-
switch
( long "eval"
<> help "Evaluate after the transformation"
)
_treeReadTransformations <- optTreeTransformationIds
_treeReadInputFile <- parseInputFile FileExtJuvixTree
pure TreeReadOptions {..}
28 changes: 22 additions & 6 deletions app/CommonOptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ where

import Control.Exception qualified as GHC
import Data.List.NonEmpty qualified as NonEmpty
import Juvix.Compiler.Core.Data.TransformationId.Parser
import Juvix.Compiler.Core.Data.TransformationId.Parser qualified as Core
import Juvix.Compiler.Tree.Data.TransformationId.Parser qualified as Tree
import Juvix.Data.FileExt
import Juvix.Prelude
import Options.Applicative
Expand Down Expand Up @@ -216,17 +217,32 @@ optNoDisambiguate =
<> help "Don't disambiguate the names of bound variables"
)

optTransformationIds :: Parser [TransformationId]
optTransformationIds =
optCoreTransformationIds :: Parser [Core.TransformationId]
optCoreTransformationIds =
option
(eitherReader parseTransf)
( long "transforms"
<> short 't'
<> value []
<> metavar "[Transform]"
<> completer (mkCompleter (return . completionsString))
<> completer (mkCompleter (return . Core.completionsString))
<> help "hint: use autocomplete"
)
where
parseTransf :: String -> Either String [TransformationId]
parseTransf = mapLeft unpack . parseTransformations . pack
parseTransf :: String -> Either String [Core.TransformationId]
parseTransf = mapLeft unpack . Core.parseTransformations . pack

optTreeTransformationIds :: Parser [Tree.TransformationId]
optTreeTransformationIds =
option
(eitherReader parseTransf)
( long "transforms"
<> short 't'
<> value []
<> metavar "[Transform]"
<> completer (mkCompleter (return . Tree.completionsString))
<> help "hint: use autocomplete"
)
where
parseTransf :: String -> Either String [Tree.TransformationId]
parseTransf = mapLeft unpack . Tree.parseTransformations . pack
Loading