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

Separate Comment from pretty Options #2988

Merged
merged 1 commit into from
Sep 2, 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
14 changes: 9 additions & 5 deletions src/Juvix/Compiler/Backend/Isabelle/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import Juvix.Prelude
import Prettyprinter.Render.Terminal qualified as Ansi

ppOutDefault :: (PrettyCode c) => [Comment] -> c -> AnsiText
ppOutDefault comments = mkAnsiText . PPOutput . doc (defaultOptions comments)

ppOut :: (CanonicalProjection a Options, PrettyCode c) => a -> c -> AnsiText
ppOut o = mkAnsiText . PPOutput . doc (project o)
ppOutDefault comments =
mkAnsiText
. PPOutput
. doc defaultOptions comments

ppTrace' :: (CanonicalProjection a Options, PrettyCode c) => a -> c -> Text
ppTrace' opts = Ansi.renderStrict . reAnnotateS stylize . layoutPretty defaultLayoutOptions . doc (project opts)
ppTrace' opts =
Ansi.renderStrict
. reAnnotateS stylize
. layoutPretty defaultLayoutOptions
. doc (project opts) []

ppTrace :: (PrettyCode c) => c -> Text
ppTrace = ppTrace' traceOptions
Expand Down
31 changes: 17 additions & 14 deletions src/Juvix/Compiler/Backend/Isabelle/Pretty/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,42 @@ arrow :: Doc Ann
arrow = "\\<Rightarrow>"

class PrettyCode c where
ppCode :: (Member (State Options) r) => c -> Sem r (Doc Ann)
ppCode :: (Members '[Reader Options, Input Comment] r) => c -> Sem r (Doc Ann)

doc :: (PrettyCode c) => Options -> c -> Doc Ann
doc opts = run . evalState opts . ppCode
doc :: (PrettyCode c) => Options -> [Comment] -> c -> Doc Ann
doc opts comments =
run
. runReader opts
. runInputList comments
. ppCode

ppCodeQuoted :: (HasAtomicity c, PrettyCode c, Member (State Options) r) => c -> Sem r (Doc Ann)
ppCodeQuoted :: (HasAtomicity c, PrettyCode c, Members '[Reader Options, Input Comment] r) => c -> Sem r (Doc Ann)
ppCodeQuoted c
| atomicity c == Atom = ppCode c
| otherwise = dquotes <$> ppCode c

ppTopCode :: (HasAtomicity c, PrettyCode c, Member (State Options) r) => c -> Sem r (Doc Ann)
ppTopCode :: (HasAtomicity c, PrettyCode c, Members '[Reader Options, Input Comment] r) => c -> Sem r (Doc Ann)
ppTopCode c = parensIf (not (isAtomic c)) <$> ppCode c

ppParams :: (HasAtomicity c, PrettyCode c, Member (State Options) r) => [c] -> Sem r (Maybe (Doc Ann))
ppParams :: (HasAtomicity c, PrettyCode c, Members '[Reader Options, Input Comment] r) => [c] -> Sem r (Maybe (Doc Ann))
ppParams = \case
[] -> return Nothing
[x] -> Just <$> ppRightExpression appFixity x
params -> do
ps <- mapM ppCode params
return $ Just $ parens (hsep (punctuate comma ps))

ppComments :: (Member (State Options) r) => Interval -> Sem r (Doc Ann)
ppComments :: (Member (Input Comment) r) => Interval -> Sem r (Doc Ann)
ppComments loc = do
comments <- gets (takeWhile cmpLoc . (^. optComments))
modify' $ over optComments (dropWhile cmpLoc)
comments <- inputWhile cmpLoc
return
. mconcatMap (\c -> annotate AnnComment $ "(*" <> pretty (c ^. commentText) <+> "*)" <> line)
$ comments
where
cmpLoc :: Comment -> Bool
cmpLoc c = c ^. commentInterval . intervalStart <= loc ^. intervalEnd

ppCodeWithComments :: (PrettyCode a, HasLoc a, Member (State Options) r) => a -> Sem r (Doc Ann, Doc Ann)
ppCodeWithComments :: (PrettyCode a, HasLoc a, Members '[Reader Options, Input Comment] r) => a -> Sem r (Doc Ann, Doc Ann)
ppCodeWithComments a = do
comments <- ppComments (getLoc a)
res <- ppCode a
Expand Down Expand Up @@ -192,7 +195,7 @@ instance (PrettyCode a) => PrettyCode (List a) where
elems <- mapM ppCode _listElements
return $ brackets $ hsep (punctuate comma elems)

ppRecord :: (PrettyCode a, Member (State Options) r) => Bool -> Record a -> Sem r (Doc Ann)
ppRecord :: (PrettyCode a, Members '[Reader Options, Input Comment] r) => Bool -> Record a -> Sem r (Doc Ann)
ppRecord bUpdate Record {..} = do
recName <- ppCode _recordName
names <- mapM (ppCode . fst) _recordFields
Expand Down Expand Up @@ -335,21 +338,21 @@ instance PrettyCode Theory where
<> kwEnd

ppRightExpression ::
(PrettyCode a, HasAtomicity a, Member (State Options) r) =>
(PrettyCode a, HasAtomicity a, Members '[Reader Options, Input Comment] r) =>
Fixity ->
a ->
Sem r (Doc Ann)
ppRightExpression = ppLRExpression isRightAssoc

ppLeftExpression ::
(PrettyCode a, HasAtomicity a, Member (State Options) r) =>
(PrettyCode a, HasAtomicity a, Members '[Reader Options, Input Comment] r) =>
Fixity ->
a ->
Sem r (Doc Ann)
ppLeftExpression = ppLRExpression isLeftAssoc

ppLRExpression ::
(HasAtomicity a, PrettyCode a, Member (State Options) r) =>
(HasAtomicity a, PrettyCode a, Members '[Reader Options, Input Comment] r) =>
(Fixity -> Bool) ->
Fixity ->
a ->
Expand Down
8 changes: 3 additions & 5 deletions src/Juvix/Compiler/Backend/Isabelle/Pretty/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ module Juvix.Compiler.Backend.Isabelle.Pretty.Options where
import Juvix.Prelude

data Options = Options
{ _optComments :: [Comment]
}

makeLenses ''Options

defaultOptions :: [Comment] -> Options
defaultOptions :: Options
defaultOptions = Options

traceOptions :: Options
traceOptions = defaultOptions []
traceOptions = defaultOptions

fromGenericOptions :: GenericOptions -> Options
fromGenericOptions _ = defaultOptions []
fromGenericOptions _ = defaultOptions

instance CanonicalProjection GenericOptions Options where
project = fromGenericOptions
9 changes: 9 additions & 0 deletions src/Juvix/Prelude/Effects/Input.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Juvix.Prelude.Effects.Input
( Input,
input,
inputJust,
inputWhile,
peekInput,
runInputList,
)
Expand All @@ -26,6 +27,14 @@ input =
Input [] -> (Nothing, Input [])
Input (i : is) -> (Just i, Input is)

inputWhile :: (Member (Input i) r) => (i -> Bool) -> Sem r [i]
inputWhile c =
stateStaticRep $
\case
Input l ->
let (sat, rest) = span c l
in (sat, Input rest)

peekInput :: (Member (Input i) r) => Sem r (Maybe i)
peekInput = do
Input l <- getStaticRep
Expand Down
Loading