1 module Pretty (prettyShow) where
3 import qualified Data.Map as Map
4 import qualified CoreSyn
5 import qualified Module
6 import qualified HscTypes
7 import Text.PrettyPrint.HughesPJClass
8 import Outputable ( showSDoc, ppr, Outputable, OutputableBndr)
10 import qualified ForSyDe.Backend.Ppr
11 import qualified ForSyDe.Backend.VHDL.AST as AST
15 import TranslatorTypes
18 -- | A version of the default pPrintList method, which uses a custom function
19 -- f instead of pPrint to print elements.
20 printList :: (a -> Doc) -> [a] -> Doc
21 printList f = brackets . fsep . punctuate comma . map f
23 instance Pretty HsFunction where
24 pPrint (HsFunction name args res) =
25 text name <> char ' ' <> parens (hsep $ punctuate comma args') <> text " -> " <> res'
27 args' = map pPrint args
30 instance Pretty x => Pretty (HsValueMap x) where
31 pPrint (Tuple maps) = parens (hsep $ punctuate comma (map pPrint maps))
32 pPrint (Single s) = pPrint s
34 instance Pretty HsValueUse where
35 pPrint Port = char 'P'
36 pPrint (State n) = char 'C' <> int n
37 pPrint (HighOrder _ _) = text "Higher Order"
39 instance Pretty id => Pretty (FlatFunction' id) where
40 pPrint (FlatFunction args res apps conds sigs) =
41 (text "Args: ") $$ nest 10 (pPrint args)
42 $+$ (text "Result: ") $$ nest 10 (pPrint res)
43 $+$ (text "Apps: ") $$ nest 10 (vcat (map pPrint apps))
44 $+$ (text "Conds: ") $$ nest 10 (pPrint conds)
45 $+$ text "Signals: " $$ nest 10 (printList ppsig sigs)
47 ppsig (id, info) = pPrint id <> pPrint info
49 instance Pretty id => Pretty (FApp id) where
50 pPrint (FApp func args res) =
51 pPrint func <> text " : " <> pPrint args <> text " -> " <> pPrint res
53 instance Pretty id => Pretty (CondDef id) where
54 pPrint _ = text "TODO"
56 instance Pretty SignalInfo where
57 pPrint (SignalInfo name use ty) =
58 text ":" <> (pPrint use) <> (ppname name)
60 ppname Nothing = empty
61 ppname (Just name) = text ":" <> text name
63 instance Pretty SigUse where
64 pPrint SigPortIn = text "PI"
65 pPrint SigPortOut = text "PO"
66 pPrint SigInternal = text "I"
67 pPrint (SigStateOld n) = text "SO:" <> int n
68 pPrint (SigStateNew n) = text "SN:" <> int n
69 pPrint SigSubState = text "s"
71 instance Pretty VHDLSession where
72 pPrint (VHDLSession mod nameCount funcs) =
73 text "Module: " $$ nest 15 (text modname)
74 $+$ text "NameCount: " $$ nest 15 (int nameCount)
75 $+$ text "Functions: " $$ nest 15 (vcat (map ppfunc (Map.toList funcs)))
77 ppfunc (hsfunc, fdata) =
78 pPrint hsfunc $+$ nest 5 (pPrint fdata)
79 modname = showSDoc $ Module.pprModule (HscTypes.cm_module mod)
81 instance Pretty FuncData where
82 pPrint (FuncData flatfunc entity arch) =
83 text "Flattened: " $$ nest 15 (ppffunc flatfunc)
84 $+$ text "Entity" $$ nest 15 (ppent entity)
87 ppffunc (Just f) = pPrint f
88 ppffunc Nothing = text "Nothing"
89 ppent (Just e) = pPrint e
90 ppent Nothing = text "Nothing"
91 pparch Nothing = text "VHDL architecture not present"
92 pparch (Just _) = text "VHDL architecture present"
94 instance Pretty Entity where
95 pPrint (Entity id args res decl) =
96 text "Entity: " $$ nest 10 (pPrint id)
97 $+$ text "Args: " $$ nest 10 (pPrint args)
98 $+$ text "Result: " $$ nest 10 (pPrint res)
101 ppdecl Nothing = text "VHDL entity not present"
102 ppdecl (Just _) = text "VHDL entity present"
104 instance (OutputableBndr b) => Pretty (CoreSyn.Bind b) where
105 pPrint (CoreSyn.NonRec b expr) =
106 text "NonRec: " $$ nest 10 (prettyBind (b, expr))
107 pPrint (CoreSyn.Rec binds) =
108 text "Rec: " $$ nest 10 (vcat $ map (prettyBind) binds)
110 instance Pretty AST.VHDLId where
111 pPrint id = ForSyDe.Backend.Ppr.ppr id
113 prettyBind :: (Outputable b, Outputable e) => (b, e) -> Doc
114 prettyBind (b, expr) =
115 text b' <> text " = " <> text expr'
117 b' = showSDoc $ ppr b
118 expr' = showSDoc $ ppr expr