2022-08-03 14:20:40 +03:00
|
|
|
module Commands.Dev.Termination where
|
2022-03-08 14:53:26 +03:00
|
|
|
|
|
|
|
import Control.Monad.Extra
|
2022-04-05 20:57:21 +03:00
|
|
|
import Data.Text qualified as Text
|
2022-06-09 17:36:07 +03:00
|
|
|
import GlobalOptions
|
2022-09-12 11:44:00 +03:00
|
|
|
import Juvix.Compiler.Abstract.Pretty.Base qualified as Abstract
|
2022-07-08 14:59:45 +03:00
|
|
|
import Juvix.Prelude hiding (Doc)
|
2022-03-25 02:52:30 +03:00
|
|
|
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
|
2022-06-09 17:36:07 +03:00
|
|
|
{ _callsFunctionNameFilter :: Maybe (NonEmpty Text),
|
2022-09-12 11:44:00 +03:00
|
|
|
_callsShowDecreasingArgs :: Abstract.ShowDecrArgs
|
2022-03-08 14:53:26 +03:00
|
|
|
}
|
|
|
|
|
2022-06-09 17:36:07 +03:00
|
|
|
newtype CallGraphOptions = CallGraphOptions
|
|
|
|
{ _graphFunctionNameFilter :: Maybe (NonEmpty Text)
|
2022-03-08 14:53:26 +03:00
|
|
|
}
|
|
|
|
|
2022-05-06 12:48:07 +03:00
|
|
|
makeLenses ''CallsOptions
|
|
|
|
makeLenses ''CallGraphOptions
|
|
|
|
|
2022-03-08 14:53:26 +03:00
|
|
|
parseCalls :: Parser CallsOptions
|
|
|
|
parseCalls = do
|
|
|
|
_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'
|
2022-09-12 11:44:00 +03:00
|
|
|
<> value Abstract.ArgRel
|
2022-03-08 14:53:26 +03:00
|
|
|
<> help "possible values: argument, relation, both"
|
|
|
|
)
|
|
|
|
pure CallsOptions {..}
|
|
|
|
where
|
2022-09-12 11:44:00 +03:00
|
|
|
decrArgsParser :: ReadM Abstract.ShowDecrArgs
|
2022-03-25 02:52:30 +03:00
|
|
|
decrArgsParser = eitherReader $ \s ->
|
|
|
|
case map toLower s of
|
2022-09-12 11:44:00 +03:00
|
|
|
"argument" -> return Abstract.OnlyArg
|
|
|
|
"relation" -> return Abstract.OnlyRel
|
|
|
|
"both" -> return Abstract.ArgRel
|
2022-03-25 02:52:30 +03:00
|
|
|
_ -> Left "bad argument"
|
2022-03-08 14:53:26 +03:00
|
|
|
|
|
|
|
parseCallGraph :: Parser CallGraphOptions
|
|
|
|
parseCallGraph = do
|
|
|
|
_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)
|
2022-07-08 14:59:45 +03:00
|
|
|
(progDesc "Compute the calls table of a .juvix file")
|
2022-03-08 14:53:26 +03:00
|
|
|
commandGraph :: Mod CommandFields TerminationCommand
|
|
|
|
commandGraph = command "graph" minfo
|
|
|
|
where
|
|
|
|
minfo :: ParserInfo TerminationCommand
|
|
|
|
minfo =
|
|
|
|
info
|
|
|
|
(CallGraph <$> parseCallGraph)
|
2022-07-08 14:59:45 +03:00
|
|
|
(progDesc "Compute the complete call graph of a .juvix file")
|
2022-03-08 14:53:26 +03:00
|
|
|
|
2022-09-12 11:44:00 +03:00
|
|
|
instance CanonicalProjection (GlobalOptions, CallsOptions) Abstract.Options where
|
|
|
|
project (GlobalOptions {..}, CallsOptions {..}) =
|
|
|
|
Abstract.defaultOptions
|
|
|
|
{ Abstract._optShowNameIds = _globalShowNameIds,
|
|
|
|
Abstract._optShowDecreasingArgs = _callsShowDecreasingArgs
|
|
|
|
}
|