Skip to content

Commit

Permalink
Slightly improve prettyprinting for Core (#2094)
Browse files Browse the repository at this point in the history
This pr includes some small adjustments to the Core prettyprinter.

Currently printing core code usually generates very long lines. In order
to improve that, now let and lambda bodies are printed in the following
line if they do not fit in the current line.
  • Loading branch information
janmasrovira authored May 15, 2023
1 parent 3cbf6f5 commit 10d052d
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/Juvix/Compiler/Core/Pretty/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ ppCodeLet' name mty lt = do
mempty <+> kwColon <+> ty
Nothing ->
mempty
return $ kwLet <+> n' <> tty <+> kwAssign <+> v' <+> kwIn <+> b'
return $ kwLet <+> n' <> tty <+> kwAssign <> oneLineOrNext v' <+> kwIn <> line <> b'

ppCodeCase' :: (PrettyCode a, Member (Reader Options) r) => [[Text]] -> [[Maybe (Doc Ann)]] -> [Text] -> Case' i bi a ty -> Sem r (Doc Ann)
ppCodeCase' branchBinderNames branchBinderTypes branchTagNames Case {..} =
Expand Down Expand Up @@ -297,6 +297,18 @@ instance PrettyCode LetRec where
getName :: Binder -> Sem r (Doc Ann)
getName i = ppName KNameLocal (i ^. binderName)

instance PrettyCode Lambda where
ppCode (Lambda _ bi body) = do
b <- ppCode body
lam <- do
n <- ppName KNameLocal (bi ^. binderName)
case bi ^. binderType of
NDyn {} -> return $ kwLambda <> n
ty -> do
tty <- ppCode ty
return $ kwLambda <> parens (n <+> kwColon <+> tty)
return (lam <> oneLineOrNext b)

instance PrettyCode Node where
ppCode :: forall r. (Member (Reader Options) r) => Node -> Sem r (Doc Ann)
ppCode node = case node of
Expand All @@ -312,16 +324,7 @@ instance PrettyCode Node where
NCtr x ->
let name = getInfoName (x ^. constrInfo)
in ppCodeConstr' name x
NLam (Lambda _ bi body) -> do
b <- ppCode body
lam <- do
n <- ppName KNameLocal (bi ^. binderName)
case bi ^. binderType of
NDyn {} -> return $ kwLambda <> n
ty -> do
tty <- ppCode ty
return $ kwLambda <> parens (n <+> kwColon <+> tty)
return (lam <+> b)
NLam l -> ppCode l
NLet x -> ppCode x
NRec l -> ppCode l
NCase x@Case {..} -> do
Expand Down Expand Up @@ -475,7 +478,7 @@ instance PrettyCode InfoTable where
case msig of
Just sig -> do
body' <- ppCode n
return (Just (sig <+> kwAssign <+> nest' body' <> kwSemicolon))
return (Just (sig <+> kwAssign <> oneLineOrNext body' <> kwSemicolon))
Nothing ->
return Nothing

Expand Down

0 comments on commit 10d052d

Please sign in to comment.