Replace printBind* by a Pretty instance.
[matthijs/master-project/cλash.git] / Pretty.hs
1 module Pretty (prettyShow) where
2
3 import qualified CoreSyn
4 import Text.PrettyPrint.HughesPJClass
5 import Outputable ( showSDoc, ppr, Outputable, OutputableBndr)
6 import Flatten
7 import TranslatorTypes
8
9 instance Pretty HsFunction where
10   pPrint (HsFunction name args res) =
11     text name <> char ' ' <> parens (hsep $ punctuate comma args') <> text " -> " <> res'
12     where
13       args' = map pPrint args
14       res'  = pPrint res
15
16 instance Pretty x => Pretty (HsValueMap x) where
17   pPrint (Tuple maps) = parens (hsep $ punctuate comma (map pPrint maps))
18   pPrint (Single s)   = pPrint s
19
20 instance Pretty HsValueUse where
21   pPrint Port            = char 'P'
22   pPrint (State n)       = char 'C' <> int n
23   pPrint (HighOrder _ _) = text "Higher Order"
24
25 instance Pretty FlatFunction where
26   pPrint (FlatFunction args res apps conds) =
27     (text "Args: ") $$ nest 10 (pPrint args)
28     $+$ (text "Result: ") $$ nest 10 (pPrint res)
29     $+$ (text "Apps: ") $$ nest 10 (vcat (map pPrint apps))
30     $+$ (text "Conds: ") $$ nest 10 (pPrint conds)
31
32 instance Pretty FApp where
33   pPrint (FApp func args res) =
34     pPrint func <> text " : " <> pPrint args <> text " -> " <> pPrint res
35
36 instance Pretty SignalDef where
37   pPrint (SignalDef id) = pPrint id
38
39 instance Pretty SignalUse where
40   pPrint (SignalUse id) = pPrint id
41
42 instance Pretty CondDef where
43   pPrint _ = text "TODO"
44
45 instance Pretty VHDLSession where
46   pPrint (VHDLSession nameCount funcs) =
47     text "NameCount: " $$ nest 15 (int nameCount)
48     $+$ text "Functions: " $$ nest 15 (vcat (map ppfunc funcs))
49     where
50       ppfunc (hsfunc, (flatfunc)) =
51         pPrint hsfunc $+$ (text "Flattened: " $$ nest 15 (pPrint flatfunc))
52
53 instance (OutputableBndr b) => Pretty (CoreSyn.Bind b) where
54   pPrint (CoreSyn.NonRec b expr) =
55     text "NonRec: " $$ nest 10 (prettyBind (b, expr))
56   pPrint (CoreSyn.Rec binds) =
57     text "Rec: " $$ nest 10 (vcat $ map (prettyBind) binds)
58
59 prettyBind :: (Outputable b, Outputable e) => (b, e) -> Doc
60 prettyBind (b, expr) =
61   text b' <> text " = " <> text expr'
62   where
63     b' = showSDoc $ ppr b
64     expr' = showSDoc $ ppr expr