-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d16b6b
commit 5838988
Showing
8 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Juvix.Compiler.Nockma.Pretty | ||
( module Juvix.Compiler.Nockma.Pretty, | ||
module Juvix.Compiler.Nockma.Pretty.Base, | ||
module Juvix.Compiler.Nockma.Pretty.Options, | ||
module Juvix.Data.PPOutput, | ||
) | ||
where | ||
|
||
import Juvix.Compiler.Nockma.Pretty.Base | ||
import Juvix.Compiler.Nockma.Pretty.Options | ||
import Juvix.Data.PPOutput | ||
import Juvix.Prelude | ||
import Prettyprinter.Render.Terminal qualified as Ansi | ||
import Prettyprinter.Render.Text (renderStrict) | ||
|
||
ppOutDefault :: (PrettyCode c) => c -> AnsiText | ||
ppOutDefault = mkAnsiText . PPOutput . doc defaultOptions | ||
|
||
ppOut :: (CanonicalProjection a Options, PrettyCode c) => a -> c -> AnsiText | ||
ppOut o = mkAnsiText . PPOutput . doc (project o) | ||
|
||
ppTrace :: (PrettyCode c) => c -> Text | ||
ppTrace = Ansi.renderStrict . reAnnotateS stylize . layoutPretty defaultLayoutOptions . doc traceOptions | ||
|
||
ppPrint :: (PrettyCode c) => c -> Text | ||
ppPrint = renderStrict . toTextStream . ppOutDefault |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module Juvix.Compiler.Nockma.Pretty.Base | ||
( module Juvix.Compiler.Nockma.Pretty.Base, | ||
module Juvix.Data.CodeAnn, | ||
module Juvix.Compiler.Nockma.Pretty.Options, | ||
) | ||
where | ||
|
||
import Juvix.Compiler.Nockma.Language | ||
import Juvix.Compiler.Nockma.Pretty.Options | ||
import Juvix.Data.CodeAnn | ||
import Juvix.Prelude hiding (Atom) | ||
|
||
doc :: (PrettyCode c) => Options -> c -> Doc Ann | ||
doc opts = | ||
run | ||
. runReader opts | ||
. ppCode | ||
|
||
class PrettyCode c where | ||
ppCode :: (Member (Reader Options) r) => c -> Sem r (Doc Ann) | ||
|
||
runPrettyCode :: (PrettyCode c) => Options -> c -> Doc Ann | ||
runPrettyCode opts = run . runReader opts . ppCode | ||
|
||
instance (PrettyCode a) => PrettyCode (Atom a) where | ||
ppCode (Atom k) = annotate (AnnKind KNameFunction) <$> ppCode k | ||
|
||
instance PrettyCode Natural where | ||
ppCode = return . pretty | ||
|
||
instance (PrettyCode a) => PrettyCode (Cell a) where | ||
ppCode c = do | ||
m <- asks (^. optPrettyMode) | ||
case m of | ||
AllDelimiters -> do | ||
l' <- ppCode (c ^. cellLeft) | ||
r' <- ppCode (c ^. cellRight) | ||
return (brackets (oneLineOrNextNoSpace (l' <+> r'))) | ||
MinimizeDelimiters -> do | ||
l <- mapM ppCode (unfoldCell c) | ||
return (brackets (oneLineOrNextNoSpace (sep l))) | ||
|
||
unfoldCell :: Cell a -> NonEmpty (Term a) | ||
unfoldCell c = c ^. cellLeft :| go [] (c ^. cellRight) | ||
where | ||
go :: [Term a] -> Term a -> [Term a] | ||
go acc = \case | ||
t@TermAtom {} -> reverse (t : acc) | ||
TermCell (Cell l r) -> go (l : acc) r | ||
|
||
instance (PrettyCode a) => PrettyCode (Term a) where | ||
ppCode = \case | ||
TermAtom t -> ppCode t | ||
TermCell c -> ppCode c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Juvix.Compiler.Nockma.Pretty.Options where | ||
|
||
import Juvix.Prelude | ||
|
||
data PrettyMode | ||
= MinimizeDelimiters | ||
| AllDelimiters | ||
deriving stock (Data) | ||
|
||
defaultOptions :: Options | ||
defaultOptions = | ||
Options | ||
{ _optPrettyMode = MinimizeDelimiters | ||
} | ||
|
||
newtype Options = Options | ||
{ _optPrettyMode :: PrettyMode | ||
} | ||
|
||
traceOptions :: Options | ||
traceOptions = | ||
Options | ||
{ _optPrettyMode = AllDelimiters | ||
} | ||
|
||
makeLenses ''Options | ||
|
||
fromGenericOptions :: GenericOptions -> Options | ||
fromGenericOptions GenericOptions {} = defaultOptions | ||
|
||
instance CanonicalProjection GenericOptions Options where | ||
project = fromGenericOptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters