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

Fix function clause printing #3199

Merged
merged 1 commit into from
Nov 29, 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
38 changes: 23 additions & 15 deletions src/Juvix/Compiler/Concrete/Print/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -701,14 +701,7 @@ ppCase isTop c = do
b :| [] -> case isTop of
Top -> oneLineOrNext (ppCaseBranch' Top b)
NotTop -> space <> oneLineOrNextBraces (ppCaseBranch' NotTop b)
_ -> case isTop of
Top -> do
let brs =
vsepHard (ppCaseBranch' NotTop <$> NonEmpty.init branches')
<> hardline
<> ppCaseBranch' Top (NonEmpty.last branches')
hardline <> indent brs
NotTop -> space <> braces (blockIndent (vsepHard (ppCaseBranch' NotTop <$> branches')))
_ -> ppPipeBranches True isTop ppCaseBranch' branches'

ppCaseBranch' :: IsTop -> CaseBranch s -> Sem r ()
ppCaseBranch' lastTopBranch b = ppCaseBranch lastTopBranch b
Expand Down Expand Up @@ -1120,12 +1113,11 @@ instance PrettyPrint BuiltinFunction where
instance PrettyPrint BuiltinAxiom where
ppCode i = ppCode Kw.kwBuiltin <+> keywordText (P.prettyText i)

instance (SingI s) => PrettyPrint (FunctionClause s) where
ppCode :: forall r. (Members '[ExactPrint, Reader Options] r) => FunctionClause s -> Sem r ()
ppCode FunctionClause {..} = do
let pats' = hsep (ppPatternAtomType <$> _clausenPatterns)
e' = ppTopExpressionType _clausenBody
ppCode _clausenPipeKw <+> pats' <+> ppCode _clausenAssignKw <> oneLineOrNext e'
ppFunctionClause :: (SingI s, Members '[ExactPrint, Reader Options] r) => IsTop -> FunctionClause s -> Sem r ()
ppFunctionClause isTop FunctionClause {..} = do
let pats' = hsep (ppPatternAtomType <$> _clausenPatterns)
e' = ppMaybeTopExpression isTop _clausenBody
ppCode _clausenPipeKw <+> pats' <+> ppCode _clausenAssignKw <> oneLineOrNext e'

instance (SingI s) => PrettyPrint (Argument s) where
ppCode :: (Members '[ExactPrint, Reader Options] r) => Argument s -> Sem r ()
Expand Down Expand Up @@ -1194,6 +1186,22 @@ instance (SingI s) => PrettyPrint (FunctionLhs s) where
?<> instance'
?<> (name' <> sig')

ppPipeBranches :: (Members '[ExactPrint, Reader Options] r) => Bool -> IsTop -> (IsTop -> a -> Sem r ()) -> NonEmpty a -> Sem r ()
ppPipeBranches allowSameLine isTop ppBranch = \case
b :| [] -> case isTop of
Top
| allowSameLine -> oneLineOrNext (ppBranch Top b)
| otherwise -> hardline <> indent (ppBranch Top b)
NotTop -> space <> oneLineOrNextBraces (ppBranch NotTop b)
branches -> case isTop of
Top -> do
let brs =
vsepHard (ppBranch NotTop <$> NonEmpty.init branches)
<> hardline
<> ppBranch Top (NonEmpty.last branches)
hardline <> indent brs
NotTop -> space <> braces (blockIndent (vsepHard (ppBranch NotTop <$> branches)))

instance (SingI s) => PrettyPrint (FunctionDef s) where
ppCode :: forall r. (Members '[ExactPrint, Reader Options] r) => FunctionDef s -> Sem r ()
ppCode fun@FunctionDef {..} = do
Expand All @@ -1202,7 +1210,7 @@ instance (SingI s) => PrettyPrint (FunctionDef s) where
sig' = ppCode (functionDefLhs fun)
body' = case _signBody of
SigBodyExpression e -> space <> ppCode Kw.kwAssign <> oneLineOrNext (ppTopExpressionType e)
SigBodyClauses k -> line <> indent (vsep (ppCode <$> k))
SigBodyClauses k -> ppPipeBranches False Top ppFunctionClause k
doc'
?<> pragmas'
?<> sig'
Expand Down
9 changes: 9 additions & 0 deletions tests/positive/Format.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ case4 (n : Nat) : Nat :=
-- case with application subject
case5 (n : Nat) : Nat := case id n of x := zero;

-- case on function clause
case6 : Nat -> Nat
| zero :=
case zero of {
| zero := zero
| _ := zero
}
| (suc n) := n;

-- qualified commas
t4 : String := "a" M., "b" M., "c" M., "d";

Expand Down
Loading