2022-03-08 14:53:26 +03:00
|
|
|
{-# LANGUAGE ApplicativeDo #-}
|
2022-03-25 02:52:30 +03:00
|
|
|
|
2022-03-08 14:53:26 +03:00
|
|
|
module Commands.Termination where
|
|
|
|
|
|
|
|
import Commands.Extra
|
|
|
|
import Control.Monad.Extra
|
2022-03-25 02:52:30 +03:00
|
|
|
import qualified Data.Text as Text
|
2022-03-08 14:53:26 +03:00
|
|
|
import MiniJuvix.Prelude hiding (Doc)
|
2022-03-25 02:52:30 +03:00
|
|
|
import qualified MiniJuvix.Syntax.Abstract.Pretty.Base as A
|
|
|
|
import Options.Applicative
|
2022-03-08 14:53:26 +03:00
|
|
|
|
2022-03-25 02:52:30 +03:00
|
|
|
data TerminationCommand
|
|
|
|
= Calls CallsOptions
|
2022-03-08 14:53:26 +03:00
|
|
|
| CallGraph CallGraphOptions
|
|
|
|
|
|
|
|
data CallsOptions = CallsOptions
|
|
|
|
{ _callsInputFile :: FilePath,
|
|
|
|
_callsShowIds :: Bool,
|
|
|
|
_callsFunctionNameFilter :: Maybe (NonEmpty Text),
|
|
|
|
_callsShowDecreasingArgs :: A.ShowDecrArgs
|
|
|
|
}
|
|
|
|
|
|
|
|
data CallGraphOptions = CallGraphOptions
|
|
|
|
{ _graphInputFile :: FilePath,
|
|
|
|
_graphFunctionNameFilter :: Maybe (NonEmpty Text)
|
|
|
|
}
|
|
|
|
|
|
|
|
parseCalls :: Parser CallsOptions
|
|
|
|
parseCalls = do
|
|
|
|
_callsInputFile <- parseInputFile
|
|
|
|
_callsShowIds <-
|
|
|
|
switch
|
|
|
|
( long "show-name-ids"
|
|
|
|
<> help "Show the unique number of each identifier"
|
|
|
|
)
|
|
|
|
_callsFunctionNameFilter <-
|
2022-03-25 02:52:30 +03:00
|
|
|
fmap msum . optional $
|
|
|
|
nonEmpty . Text.words
|
|
|
|
<$> option
|
|
|
|
str
|
|
|
|
( long "function"
|
|
|
|
<> short 'f'
|
|
|
|
<> metavar "fun1 fun2 ..."
|
|
|
|
<> help "Only shows the specified functions"
|
|
|
|
)
|
2022-03-08 14:53:26 +03:00
|
|
|
_callsShowDecreasingArgs <-
|
2022-03-25 02:52:30 +03:00
|
|
|
option
|
|
|
|
decrArgsParser
|
2022-03-08 14:53:26 +03:00
|
|
|
( long "show-decreasing-args"
|
|
|
|
<> short 'd'
|
|
|
|
<> value A.ArgRel
|
|
|
|
<> help "possible values: argument, relation, both"
|
|
|
|
)
|
|
|
|
pure CallsOptions {..}
|
|
|
|
where
|
2022-03-25 02:52:30 +03:00
|
|
|
decrArgsParser :: ReadM A.ShowDecrArgs
|
|
|
|
decrArgsParser = eitherReader $ \s ->
|
|
|
|
case map toLower s of
|
|
|
|
"argument" -> return A.OnlyArg
|
|
|
|
"relation" -> return A.OnlyRel
|
|
|
|
"both" -> return A.ArgRel
|
|
|
|
_ -> Left "bad argument"
|
2022-03-08 14:53:26 +03:00
|
|
|
|
|
|
|
parseCallGraph :: Parser CallGraphOptions
|
|
|
|
parseCallGraph = do
|
|
|
|
_graphInputFile <- parseInputFile
|
|
|
|
_graphFunctionNameFilter <-
|
2022-03-25 02:52:30 +03:00
|
|
|
fmap msum . optional $
|
|
|
|
nonEmpty . Text.words
|
|
|
|
<$> option
|
|
|
|
str
|
|
|
|
( long "function"
|
|
|
|
<> short 'f'
|
|
|
|
<> help "Only shows the specified function"
|
|
|
|
)
|
2022-03-08 14:53:26 +03:00
|
|
|
pure CallGraphOptions {..}
|
|
|
|
|
|
|
|
parseTerminationCommand :: Parser TerminationCommand
|
|
|
|
parseTerminationCommand =
|
|
|
|
hsubparser $
|
|
|
|
mconcat
|
|
|
|
[ commandCalls,
|
|
|
|
commandGraph
|
|
|
|
]
|
|
|
|
where
|
|
|
|
commandCalls :: Mod CommandFields TerminationCommand
|
|
|
|
commandCalls = command "calls" minfo
|
|
|
|
where
|
|
|
|
minfo :: ParserInfo TerminationCommand
|
|
|
|
minfo =
|
|
|
|
info
|
|
|
|
(Calls <$> parseCalls)
|
|
|
|
(progDesc "Compute the calls table of a .mjuvix file")
|
|
|
|
commandGraph :: Mod CommandFields TerminationCommand
|
|
|
|
commandGraph = command "graph" minfo
|
|
|
|
where
|
|
|
|
minfo :: ParserInfo TerminationCommand
|
|
|
|
minfo =
|
|
|
|
info
|
|
|
|
(CallGraph <$> parseCallGraph)
|
|
|
|
(progDesc "Compute the complete call graph of a .mjuvix file")
|
|
|
|
|
|
|
|
callsPrettyOptions :: CallsOptions -> A.Options
|
|
|
|
callsPrettyOptions CallsOptions {..} =
|
|
|
|
A.defaultOptions
|
|
|
|
{ A._optShowNameId = _callsShowIds,
|
|
|
|
A._optShowDecreasingArgs = _callsShowDecreasingArgs
|
|
|
|
}
|