Skip to content

Commit

Permalink
add dev nock format command
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Jan 29, 2024
1 parent 6649209 commit 8f9f5f4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
6 changes: 4 additions & 2 deletions app/Commands/Dev/Nockma.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module Commands.Dev.Nockma where

import Commands.Base
import Commands.Dev.Nockma.Eval as FromAsm
import Commands.Dev.Nockma.Eval as Eval
import Commands.Dev.Nockma.Format as Format
import Commands.Dev.Nockma.Options
import Commands.Dev.Nockma.Repl as Repl

runCommand :: forall r. (Members '[Embed IO, App] r) => NockmaCommand -> Sem r ()
runCommand = \case
NockmaRepl opts -> Repl.runCommand opts
NockmaEval opts -> FromAsm.runCommand opts
NockmaEval opts -> Eval.runCommand opts
NockmaFormat opts -> Format.runCommand opts
17 changes: 17 additions & 0 deletions app/Commands/Dev/Nockma/Format.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Commands.Dev.Nockma.Format where

import Commands.Base hiding (Atom)
import Commands.Dev.Nockma.Format.Options
import Juvix.Compiler.Nockma.Pretty
import Juvix.Compiler.Nockma.Translation.FromSource qualified as Nockma

runCommand :: forall r. (Members '[Embed IO, App] r) => NockmaFormatOptions -> Sem r ()
runCommand opts = do
afile <- fromAppPathFile file
parsedTerm <- Nockma.parseTermFile (toFilePath afile)
case parsedTerm of
Left err -> exitJuvixError (JuvixError err)
Right t -> putStrLn (ppPrint t)
where
file :: AppPath File
file = opts ^. nockmaFormatFile
15 changes: 15 additions & 0 deletions app/Commands/Dev/Nockma/Format/Options.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Commands.Dev.Nockma.Format.Options where

import CommonOptions

newtype NockmaFormatOptions = NockmaFormatOptions
{ _nockmaFormatFile :: AppPath File
}
deriving stock (Data)

makeLenses ''NockmaFormatOptions

parseNockmaFormatOptions :: Parser NockmaFormatOptions
parseNockmaFormatOptions = do
_nockmaFormatFile <- parseInputFile FileExtNockma
pure NockmaFormatOptions {..}
14 changes: 13 additions & 1 deletion app/Commands/Dev/Nockma/Options.hs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
module Commands.Dev.Nockma.Options where

import Commands.Dev.Nockma.Eval.Options
import Commands.Dev.Nockma.Format.Options
import Commands.Dev.Nockma.Repl.Options
import CommonOptions

data NockmaCommand
= NockmaRepl NockmaReplOptions
| NockmaEval NockmaEvalOptions
| NockmaFormat NockmaFormatOptions
deriving stock (Data)

parseNockmaCommand :: Parser NockmaCommand
parseNockmaCommand =
hsubparser $
mconcat
[ commandRepl,
commandFromAsm
commandFromAsm,
commandFormat
]
where
commandFromAsm :: Mod CommandFields NockmaCommand
Expand All @@ -26,6 +29,15 @@ parseNockmaCommand =
(NockmaEval <$> parseNockmaEvalOptions)
(progDesc "Evaluate a nockma file. The file should contain a single nockma cell: [subject formula]")

commandFormat :: Mod CommandFields NockmaCommand
commandFormat = command "format" replInfo
where
replInfo :: ParserInfo NockmaCommand
replInfo =
info
(NockmaFormat <$> parseNockmaFormatOptions)
(progDesc "Parses a nockma file and outputs the formatted nockma code")

commandRepl :: Mod CommandFields NockmaCommand
commandRepl = command "repl" replInfo
where
Expand Down

0 comments on commit 8f9f5f4

Please sign in to comment.