0 | module Core.CompileExpr.Pretty
2 | import Core.CompileExpr
7 | import Idris.Doc.Annotations
13 | %hide Vect.catMaybes
16 | %hide Core.Name.prettyOp
18 | %hide CompileExpr.(::)
19 | %hide CompileExpr.Nil
28 | %hide Symbols.equals
29 | %hide Fin.fromInteger
37 | prettyFlag : ConInfo -> Maybe (Doc ann)
38 | prettyFlag DATACON = Nothing
39 | prettyFlag f = Just (byShow f)
41 | prettyCon : Name -> ConInfo -> Maybe Int -> Doc IdrisSyntax
44 | [ Just (annotate ((if ci == TYCON then TCon else DCon) (Just x)) (pretty0 x))
45 | , (braces . ("tag =" <++>) . byShow) <$> mtag
49 | prettyName : Name -> Doc IdrisSyntax
50 | prettyName = pretty0
54 | prettyNamedCExp : NamedCExp -> Doc IdrisSyntax
55 | prettyNamedCExp = prettyPrecNamedCExp Open
57 | prettyPrecNamedCExp : Prec -> NamedCExp -> Doc IdrisSyntax
58 | prettyPrecNamedCExp d (NmLocal _ x) = annotate Bound $
prettyName x
59 | prettyPrecNamedCExp d (NmRef _ x) = annotate (Fun x) $
prettyName x
60 | prettyPrecNamedCExp d (NmLam _ x y)
61 | = parenthesise (d > Open) $
keyword "\\" <+> prettyName x <+> fatArrow <++> prettyNamedCExp y
62 | prettyPrecNamedCExp d (NmLet _ x y z)
63 | = parenthesise (d > Open) $
64 | vcat [ let_ <++> prettyName x <++> equals <++> prettyNamedCExp y <++> in_, prettyNamedCExp z ]
65 | prettyPrecNamedCExp d (NmApp _ x xs)
66 | = parenthesise (d > Open) $
67 | sep (prettyNamedCExp x :: map (prettyPrecNamedCExp App) xs)
68 | prettyPrecNamedCExp d (NmCon _ x ci tag xs)
69 | = parenthesise (d > Open) $
70 | sep (prettyCon x ci tag :: map (prettyPrecNamedCExp App) xs)
71 | prettyPrecNamedCExp d (NmOp _ op xs)
72 | = parenthesise (d > Open) $
prettyOp op $
map (prettyPrecNamedCExp App) xs
73 | prettyPrecNamedCExp d (NmExtPrim _ p xs)
74 | = parenthesise (d > Open) $
75 | sep (annotate (Fun p) (pretty0 p) :: map (prettyPrecNamedCExp App) xs)
76 | prettyPrecNamedCExp d (NmForce _ lr x)
77 | = parenthesise (d > Open) $
78 | sep [keyword "Force", byShow lr, prettyPrecNamedCExp App x]
79 | prettyPrecNamedCExp d (NmDelay _ lr x)
80 | = parenthesise (d > Open) $
81 | sep [keyword "Delay", byShow lr, prettyPrecNamedCExp App x]
82 | prettyPrecNamedCExp d (NmConCase _ sc xs def)
83 | = parenthesise (d > Open) $
84 | vcat [ case_ <++> prettyNamedCExp sc <++> of_
85 | , indent 2 (prettyAlts prettyNamedConAlt xs def)]
86 | prettyPrecNamedCExp d (NmConstCase _ sc xs def)
87 | = parenthesise (d > Open) $
88 | vcat [ case_ <++> prettyNamedCExp sc <++> of_
89 | , indent 2 (prettyAlts prettyNamedConstAlt xs def)]
90 | prettyPrecNamedCExp d (NmPrimVal _ x) = pretty x
91 | prettyPrecNamedCExp d (NmErased _) = "___"
92 | prettyPrecNamedCExp d (NmCrash _ x)
93 | = parenthesise (d > Open) $
94 | sep [annotate Keyword "crash", byShow x]
96 | prettyNamedConAlt : NamedConAlt -> Doc IdrisSyntax
97 | prettyNamedConAlt (MkNConAlt x ci tag args exp)
98 | = sep (prettyCon x ci tag :: map prettyName args ++ [fatArrow <+> softline <+> align (prettyNamedCExp exp) ])
100 | prettyNamedConstAlt : NamedConstAlt -> Doc IdrisSyntax
101 | prettyNamedConstAlt (MkNConstAlt x exp)
102 | = pretty x <++> fatArrow <+> softline <+> align (prettyNamedCExp exp)
104 | prettyAlts : (alt -> Doc IdrisSyntax) -> List alt -> Maybe NamedCExp -> Doc IdrisSyntax
105 | prettyAlts pre xs def = vcat
106 | $
zipWith (\ s, p => s <++> pre p)
107 | (keyword "{" :: (keyword ";" <$ xs))
109 | ++ maybe [] (\ deflt => [keyword "; _" <++> fatArrow <+> softline <+> align (prettyNamedCExp deflt)]) def
112 | prettyCExp : {args : _} -> CExp args -> Doc IdrisSyntax
113 | prettyCExp = prettyNamedCExp . forget
115 | prettyCDef : CDef -> Doc IdrisDocAnn
116 | prettyCDef (MkFun [] exp) = reAnnotate Syntax $
prettyCExp exp
117 | prettyCDef (MkFun args exp) = reAnnotate Syntax $
118 | keyword "\\" <++> concatWith (\ x, y => x <+> keyword "," <++> y) (map prettyName $
toList args)
119 | <++> fatArrow <++> prettyCExp exp
120 | prettyCDef (MkCon mtag arity nt)
121 | = vcat $
header (maybe "Type" (const "Data") mtag <++> "Constructor") :: map (indent 2)
122 | ( maybe [] (\ tag => ["tag:" <++> byShow tag]) mtag ++
123 | [ "arity:" <++> byShow arity ] ++
124 | maybe [] (\ n => ["newtype by:" <++> byShow n]) nt)
125 | prettyCDef (MkForeign ccs args ret)
126 | = vcat $
header "Foreign function" :: map (indent 2)
127 | [ "bindings:" <++> cast (prettyList ccs)
128 | , "argument types:" <++> byShow args
129 | , "return type:" <++> byShow ret
131 | prettyCDef (MkError exp) = "Error:" <++> reAnnotate Syntax (prettyCExp exp)
134 | Pretty IdrisDocAnn CDef where
135 | pretty = prettyCDef