Skip to content

Commit

Permalink
Core printing
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Sep 3, 2024
1 parent 7815d52 commit 1a77aa2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/Juvix/Compiler/Core/Pretty/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,23 @@ instance PrettyCode Bottom where
ty' <- ppCode _bottomType
return (parens (kwBottom <+> kwColon <+> ty'))

instance PrettyCode SideIfBranch where
ppCode :: (Member (Reader Options) r) => SideIfBranch -> Sem r (Doc Ann)
ppCode SideIfBranch {..} = do
cond <- ppCode _sideIfBranchCondition
body <- ppCode _sideIfBranchBody
return $ kwIf <+> cond <+> kwAssign <+> body

instance PrettyCode MatchBranchRhs where
ppCode :: (Member (Reader Options) r) => MatchBranchRhs -> Sem r (Doc Ann)
ppCode = \case
MatchBranchRhsExpression x -> do
e <- ppCode x
return $ kwAssign <+> e
MatchBranchRhsIfs x -> do
brs <- mapM ppCode x
return $ vsep brs

instance PrettyCode Node where
ppCode :: forall r. (Member (Reader Options) r) => Node -> Sem r (Doc Ann)
ppCode node = case node of
Expand All @@ -394,11 +411,11 @@ instance PrettyCode Node where
ppCodeCase' branchBinderNames branchBinderTypes branchTagNames x
NMatch Match {..} -> do
let branchPatterns = map (^. matchBranchPatterns) _matchBranches
branchBodies = map (^. matchBranchBody) _matchBranches
branchRhs = map (^. matchBranchRhs) _matchBranches
pats <- mapM ppPatterns branchPatterns
vs <- mapM ppCode _matchValues
vs' <- zipWithM ppWithType (toList vs) (toList _matchValueTypes)
bs <- sequence $ zipWithExact (\ps br -> ppCode br >>= \br' -> return $ ps <+> kwAssign <+> br') pats branchBodies
bs <- sequence $ zipWithExact (\ps br -> ppCode br >>= \br' -> return $ ps <+> br') pats branchRhs
let bss = bracesIndent $ align $ concatWith (\a b -> a <> kwSemicolon <> line <> b) bs
rty <- ppTypeAnnot _matchReturnType
return $ kwMatch <+> hsep (punctuate comma vs') <+> kwWith <> rty <+> bss
Expand Down
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Core/Transformation/MatchToCase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ goMatchToCase recur node = case node of
matchBranchToPatternRow MatchBranch {..} =
PatternRow
{ _patternRowPatterns = toList _matchBranchPatterns,
_patternRowBody = _matchBranchBody,
_patternRowBody = undefined,
_patternRowIgnoredPatternsNum = 0,
_patternRowBinderChangesRev = [BCAdd n]
}
Expand Down

0 comments on commit 1a77aa2

Please sign in to comment.