2022-01-18 14:25:42 +03:00
|
|
|
{-# LANGUAGE ApplicativeDo #-}
|
|
|
|
module Main (main) where
|
|
|
|
|
|
|
|
import MiniJuvix.Utils.Prelude
|
|
|
|
import qualified MiniJuvix.Syntax.Concrete.Parser as M
|
2022-01-18 19:29:04 +03:00
|
|
|
import qualified MiniJuvix.Syntax.Concrete.Scoped.Scoper as M
|
2022-01-19 14:41:16 +03:00
|
|
|
import qualified MiniJuvix.Syntax.Concrete.Scoped.Pretty.Ansi as M
|
2022-01-19 14:49:07 +03:00
|
|
|
import qualified MiniJuvix.Syntax.Concrete.Scoped.Pretty.Base as M
|
2022-01-18 14:25:42 +03:00
|
|
|
import Options.Applicative
|
|
|
|
import Options.Applicative.Help.Pretty
|
2022-01-19 14:49:07 +03:00
|
|
|
import MiniJuvix.Syntax.Concrete.Scoped.Pretty.Base (Options(_optShowNameId))
|
2022-01-18 14:25:42 +03:00
|
|
|
|
|
|
|
data Command =
|
|
|
|
Scope ScopeOptions
|
|
|
|
| Parse ParseOptions
|
|
|
|
|
|
|
|
data ScopeOptions = ScopeOptions {
|
2022-01-19 14:49:07 +03:00
|
|
|
_scopeRootDir :: FilePath
|
|
|
|
, _scopeInputFile :: FilePath
|
|
|
|
, _scopeShowIds :: Bool
|
2022-01-18 14:25:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data ParseOptions = ParseOptions
|
|
|
|
|
|
|
|
parseScope :: Parser ScopeOptions
|
|
|
|
parseScope = do
|
2022-01-19 14:49:07 +03:00
|
|
|
_scopeRootDir <- strOption
|
2022-01-18 14:25:42 +03:00
|
|
|
(long "rootDir"
|
|
|
|
<> short 'd'
|
|
|
|
<> metavar "DIR"
|
|
|
|
<> value "."
|
|
|
|
<> showDefault
|
|
|
|
<> help "Root directory")
|
2022-01-19 14:49:07 +03:00
|
|
|
_scopeInputFile <- argument str
|
2022-01-18 14:25:42 +03:00
|
|
|
(metavar "MINIJUVIX_FILE"
|
|
|
|
<> help "Path to a .mjuvix file"
|
|
|
|
)
|
2022-01-19 14:49:07 +03:00
|
|
|
_scopeShowIds <- switch
|
|
|
|
( long "show-name-ids"
|
|
|
|
<> help "Show the unique number of each identifier"
|
|
|
|
)
|
|
|
|
|
2022-01-18 14:25:42 +03:00
|
|
|
pure ScopeOptions {..}
|
|
|
|
|
|
|
|
parseParse :: Parser ParseOptions
|
|
|
|
parseParse = pure ParseOptions
|
|
|
|
|
|
|
|
descr :: ParserInfo Command
|
|
|
|
descr = info (parseCommand <**> helper)
|
|
|
|
(fullDesc
|
|
|
|
<> progDesc "The MiniJuvix compiler."
|
|
|
|
<> headerDoc (Just $ dullblue $ bold $ underline "MiniJuvix help")
|
|
|
|
<> footerDoc (Just foot)
|
|
|
|
)
|
|
|
|
where
|
|
|
|
foot :: Doc
|
|
|
|
foot = bold "maintainers: " <> "jan@heliax.dev; jonathan@heliax.dev"
|
|
|
|
|
|
|
|
parseCommand :: Parser Command
|
|
|
|
parseCommand = subparser (
|
|
|
|
command "parse" (info (Parse <$> parseParse) (progDesc "Parse some .mjuvix files"))
|
|
|
|
<> command "scope" (info (Scope <$> parseScope) (progDesc "Parse and scope some .mjuvix files"))
|
|
|
|
)
|
|
|
|
|
2022-01-19 14:49:07 +03:00
|
|
|
mkPrettyOptions :: ScopeOptions -> M.Options
|
|
|
|
mkPrettyOptions ScopeOptions {..} = M.defaultOptions {
|
|
|
|
_optShowNameId = _scopeShowIds
|
|
|
|
}
|
|
|
|
|
2022-01-18 14:25:42 +03:00
|
|
|
go :: Command -> IO ()
|
|
|
|
go c = case c of
|
2022-01-19 14:49:07 +03:00
|
|
|
Scope opts@ScopeOptions {..} -> do
|
|
|
|
res <- M.runModuleParserIO _scopeInputFile
|
2022-01-18 14:25:42 +03:00
|
|
|
case res of
|
|
|
|
Left err -> print err
|
|
|
|
Right m -> do
|
|
|
|
print m
|
|
|
|
putStrLn "\n\n"
|
2022-01-19 14:49:07 +03:00
|
|
|
s <- M.scopeCheck _scopeInputFile [m]
|
2022-01-18 14:25:42 +03:00
|
|
|
case s of
|
|
|
|
Left err -> print err
|
2022-01-19 14:49:07 +03:00
|
|
|
Right [r] -> M.printTopModule (mkPrettyOptions opts) r
|
2022-01-19 14:41:16 +03:00
|
|
|
Right _ -> error "impossible"
|
2022-01-18 14:25:42 +03:00
|
|
|
Parse _ -> putStrLn "not implemented"
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = execParser descr >>= go
|