3 import Text.PrettyPrint.HughesPJ
4 import Numeric (fromRat)
10 instance Show Module where
11 showsPrec d m = shows (pmodule m)
13 instance Show Tdef where
14 showsPrec d t = shows (ptdef t)
16 instance Show Cdef where
17 showsPrec d c = shows (pcdef c)
19 instance Show Vdefg where
20 showsPrec d v = shows (pvdefg v)
22 instance Show Vdef where
23 showsPrec d v = shows (pvdef v)
25 instance Show Exp where
26 showsPrec d e = shows (pexp e)
28 instance Show Alt where
29 showsPrec d a = shows (palt a)
31 instance Show Ty where
32 showsPrec d t = shows (pty t)
34 instance Show Kind where
35 showsPrec d k = shows (pkind k)
37 instance Show Lit where
38 showsPrec d l = shows (plit l)
43 -- seems like this is asking for a type class...
45 pmodule (Module mname tdefs vdefgs) =
46 (text "%module" <+> panmname mname)
47 $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
48 $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
50 ptdef (Data qtcon tbinds cdefs) =
51 (text "%data" <+> pqname qtcon <+> (hsep (map ptbind tbinds)) <+> char '=')
52 $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
54 ptdef (Newtype qtcon tbinds tyopt ) =
55 text "%newtype" <+> pqname qtcon <+> (hsep (map ptbind tbinds)) <+>
57 Just ty -> char '=' <+> pty ty
60 pcdef (Constr qdcon tbinds tys) =
61 (pqname qdcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
65 pqname (m,id) = pmname m <> pname id
67 -- be sure to print the '.' here so we don't print out
68 -- ".foo" for unqualified foo...
69 pmname Nothing = empty
70 pmname (Just m) = panmname m <> char '.'
72 panmname p@(pkgName, parents, name) =
73 let parentStrs = map pname parents in
74 pname pkgName <> char ':' <>
75 -- This is to be sure to not print out:
76 -- main:.Main for when there's a single module name
80 _ -> hcat (punctuate hierModuleSeparator
82 <> hierModuleSeparator)
85 -- note that this is not a '.' but a Z-encoded '.':
86 -- GHCziIOBase.IO, not GHC.IOBase.IO.
88 hierModuleSeparator = text (zEncodeString ".")
90 ptbind (t,Klifted) = pname t
91 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
93 pattbind (t,k) = char '@' <> ptbind (t,k)
95 pakind (Klifted) = char '*'
96 pakind (Kunlifted) = char '#'
97 pakind (Kopen) = char '?'
98 pakind k = parens (pkind k)
100 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
103 paty (Tvar n) = pname n
104 paty (Tcon c) = pqname c
105 paty t = parens (pty t)
107 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
108 pbty (Tapp t1 t2) = pappty t1 [t2]
111 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
112 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
115 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
116 pappty t ts = sep (map paty (t:ts))
118 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
119 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
121 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
122 pvdefg (Nonrec vdef) = pvdef vdef
124 pvdef (Vdef (qv,t,e)) = sep [pqname qv <+> text "::" <+> pty t <+> char '=',
127 paexp (Var x) = pqname x
128 paexp (Dcon x) = pqname x
129 paexp (Lit l) = plit l
130 paexp e = parens(pexp e)
132 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
133 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
136 pbind (Tb tb) = char '@' <+> ptbind tb
137 pbind (Vb vb) = pvbind vb
139 pfexp (App e1 e2) = pappexp e1 [Left e2]
140 pfexp (Appt e t) = pappexp e [Right t]
143 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
144 pappexp (Appt e t) as = pappexp e (Right t:as)
145 pappexp e as = fsep (paexp e : map pa as)
146 where pa (Left e) = paexp e
147 pa (Right t) = char '@' <+> paty t
149 pexp (Lam b e) = char '\\' <+> plamexp [b] e
150 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
151 pexp (Case e vb t alts) = sep [text "%case" <+> pty t <+> paexp e,
152 text "%of" <+> pvbind vb]
153 $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
154 pexp (Cast e t) = (text "%cast" <+> paty t) $$ pexp e
155 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
156 pexp (External n t) = (text "%extcall" <+> pstring n) $$ paty t
160 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
162 palt (Acon c tbs vbs e) =
164 sep (map pattbind tbs),
165 sep (map pvbind vbs) <+> text "->"]
168 (plit l <+> text "->")
174 plit (Lint i t) = parens (integer i <> text "::" <> pty t)
175 plit (Lrational r t) = parens (text (show (fromRat r)) <> text "::" <> pty t)
176 plit (Lchar c t) = parens (text ("\'" ++ escape [c] ++ "\'") <> text "::" <> pty t)
177 plit (Lstring s t) = parens (pstring s <> text "::" <> pty t)
179 pstring s = doubleQuotes(text (escape s))
181 escape s = foldr f [] (map ord s)
183 f cv rest | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) =
185 where (q1,r1) = quotRem cv 16
188 f cv rest = (chr cv):rest