Skip to content

Commit

Permalink
pretty printing
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Jan 18, 2024
1 parent 5a478aa commit d974411
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/Juvix/Compiler/Nockma/Pretty/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ where
import Juvix.Compiler.Nockma.Language
import Juvix.Compiler.Nockma.Pretty.Options
import Juvix.Data.CodeAnn
import Juvix.Extra.Strings qualified as Str
import Juvix.Prelude hiding (Atom, Path)

doc :: (PrettyCode c) => Options -> c -> Doc Ann
Expand Down Expand Up @@ -55,15 +56,28 @@ instance PrettyCode NockOp where
ppCode =
return . annotate (AnnKind KNameFunction) . pretty

instance PrettyCode StdlibFunction where
ppCode = return . pretty

instance (PrettyCode a, NockNatural a) => PrettyCode (StdlibCall a) where
ppCode c = do
fun <- ppCode (c ^. stdlibCallFunction)
args <- ppCode (c ^. stdlibCallArgs)
return (Str.stdlibTag <> fun <+> Str.argsTag <> args)

instance (PrettyCode a, NockNatural a) => PrettyCode (Cell a) where
ppCode c = do
m <- asks (^. optPrettyMode)
inside <- case m of
stdlibCall <- runFail $ do
failWhenM (asks (^. optIgnoreHints))
failMaybe (c ^. cellInfo . unIrrelevant) >>= ppCode
components <- case m of
AllDelimiters -> do
l' <- ppCode (c ^. cellLeft)
r' <- ppCode (c ^. cellRight)
return (l' <+> r')
MinimizeDelimiters -> sep <$> mapM ppCode (unfoldCell c)
let inside = stdlibCall <?+> components
return (oneLineOrNextBrackets inside)

unfoldCell :: Cell a -> NonEmpty (Term a)
Expand Down
5 changes: 3 additions & 2 deletions src/Juvix/Compiler/Nockma/Translation/FromSource/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Data.HashMap.Internal.Strict qualified as HashMap
import Data.List.NonEmpty qualified as NonEmpty
import Data.Text qualified as Text
import Juvix.Compiler.Nockma.Language qualified as N
import Juvix.Extra.Strings qualified as Str
import Juvix.Parser.Error
import Juvix.Prelude hiding (Atom, many, some)
import Juvix.Prelude.Parsing hiding (runParser)
Expand Down Expand Up @@ -119,9 +120,9 @@ cell = do
where
stdlibCall :: Parser (N.StdlibCall Natural)
stdlibCall = do
chunk "stdlib@"
chunk Str.stdlibTag
f <- stdlibFun
chunk "args@"
chunk Str.argsTag
args <- term
return
N.StdlibCall
Expand Down
4 changes: 4 additions & 0 deletions src/Juvix/Data/Effect/Fail.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ failWhen :: (Member Fail r) => Bool -> Sem r ()
failWhen c = when c fail
{-# INLINE failWhen #-}

failWhenM :: (Member Fail r) => Sem r Bool -> Sem r ()
failWhenM c = whenM c fail
{-# INLINE failWhenM #-}

failUnlessM :: (Member Fail r) => Sem r Bool -> Sem r ()
failUnlessM c = unlessM c fail
{-# INLINE failUnlessM #-}
Expand Down
6 changes: 6 additions & 0 deletions src/Juvix/Extra/Strings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,12 @@ tmp = "tmp"
instrAdd :: (IsString s) => s
instrAdd = "add"

argsTag :: (IsString s) => s
argsTag = "args@"

stdlibTag :: (IsString s) => s
stdlibTag = "stdlib@"

instrSub :: (IsString s) => s
instrSub = "sub"

Expand Down

0 comments on commit d974411

Please sign in to comment.