0ae4b18f5029a11b65b2def7e273038b0372593e
[ghc-hetmet.git] / utils / ext-core / Printer.hs
1 {-# OPTIONS -Werror -Wall -fno-warn-missing-signatures #-}
2
3 module Printer where
4
5 import Text.PrettyPrint.HughesPJ
6 import Char
7
8 import Core
9 import Encoding
10 import PrimCoercions
11
12 instance Show Module where
13   showsPrec _ m = shows (pmodule m)
14
15 instance Show Tdef where
16   showsPrec _ t = shows (ptdef t)
17
18 instance Show Cdef where
19   showsPrec _ c = shows (pcdef c)
20
21 instance Show Vdefg where
22   showsPrec _ v = shows (pvdefg v)
23
24 instance Show Vdef where
25   showsPrec _ v = shows (pvdef v)
26
27 instance Show Exp where
28   showsPrec _ e = shows (pexp e)
29
30 instance Show Alt where
31   showsPrec _ a = shows (palt a)
32
33 instance Show Ty where
34   showsPrec _ t = shows (pty t)
35
36 instance Show Kind where
37   showsPrec _ k = shows (pkind k)
38
39 instance Show Lit where
40   showsPrec _ l = shows (plit l)
41
42 instance Show CoreLit where
43   showsPrec _ l = shows (pclit l)
44
45 instance Show KindOrCoercion where
46   showsPrec _ (Kind k) = shows (text "<K" <+> pkind k <> text ">")
47   showsPrec _ (Coercion (DefinedCoercion tbs (t1,t2))) = 
48      shows (text "<C" <+> hsep (map ptbind tbs) <+>
49               parens (pkind (Keq t1 t2)) <> text ">") 
50
51 instance Show AnMname where
52   showsPrec _ mn = shows (panmname mn)
53
54 indent = nest 2
55
56 -- seems like this is asking for a type class...
57
58 pmodule (Module mname tdefs vdefgs) =
59   (text "%module" <+> panmname mname)
60   $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
61              $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
62   <> (if ((not.null) tdefs) || ((not.null) vdefgs) then char '\n' else empty)
63          -- add final newline; sigh.
64
65 ptdef (Data qtcon tbinds cdefs) =
66   (text "%data" <+> pqname qtcon <+> (hsep (map ptbind tbinds)) <+> char '=')
67   $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
68
69 ptdef (Newtype qtcon coercion tbinds tyopt) =
70   text "%newtype" <+> pqname qtcon <+> pqname coercion 
71     <+> (hsep (map ptbind tbinds)) $$ indent repclause
72        where repclause = case tyopt of
73                            Just ty -> char '=' <+> pty ty 
74                            Nothing -> empty
75
76 pcdef (Constr qdcon tbinds tys)  =
77   (pqname qdcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
78
79 pname = text
80
81 pqname (m,v) = pmname m <> pname v
82
83 -- be sure to print the '.' here so we don't print out
84 -- ".foo" for unqualified foo...
85 pmname Nothing = empty
86 pmname (Just m) = panmname m <> char '.'
87
88 panmname (M (P pkgName, parents, name)) =
89   let parentStrs = map pname parents in
90          pname pkgName <> char ':' <>
91          -- This is to be sure to not print out:
92          -- main:.Main for when there's a single module name
93          -- with no parents.
94              (case parentStrs of
95                 [] -> empty
96                 _  -> hcat (punctuate hierModuleSeparator 
97                         (map pname parents)) 
98                       <> hierModuleSeparator)
99              <> pname name
100
101 -- note that this is not a '.' but a Z-encoded '.':
102 -- GHCziIOBase.IO, not GHC.IOBase.IO.
103 -- What a pain.
104 hierModuleSeparator = text (zEncodeString ".")
105
106 ptbind (t,Klifted) = pname t
107 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
108
109 pattbind (t,k) = char '@' <> ptbind (t,k)
110
111 pakind (Klifted) = char '*'
112 pakind (Kunlifted) = char '#'
113 pakind (Kopen) = char '?'
114 pakind k = parens (pkind k)
115
116 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
117 pkind (Keq from to) = peqkind (from,to)
118 pkind k = pakind k
119
120 peqkind (t1, t2) = parens (parens (pty t1) <+> text ":=:" <+> parens (pty t2)) 
121
122 paty (Tvar n) = pname n
123 paty (Tcon c) = pqname c
124 paty t = parens (pty t)
125
126 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
127 pbty (Tapp t1 t2) = pappty t1 [t2] 
128 pbty t = paty t
129
130 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
131 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
132 pty (TransCoercion t1 t2) = 
133     (sep ([pqname transCoercion, paty t1, paty t2]))
134 pty (SymCoercion t) = 
135     (sep [pqname symCoercion, paty t])
136 pty (UnsafeCoercion t1 t2) = 
137     (sep [pqname unsafeCoercion, paty t1, paty t2])
138 pty (LeftCoercion t) = 
139     (pqname leftCoercion <+> paty t)
140 pty (RightCoercion t) = 
141     (pqname rightCoercion <+> paty t)
142 pty (InstCoercion t1 t2) = 
143     (sep [pqname instCoercion, paty t1, paty t2])
144 pty t = pbty t
145
146 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
147 pappty t ts = sep (map paty (t:ts))
148
149 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
150 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
151
152 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
153 pvdefg (Nonrec vdef) = pvdef vdef
154
155 pvdef (Vdef (qv,t,e)) = sep [pqname qv <+> text "::" <+> pty t <+> char '=',
156                      indent (pexp e)]
157
158 paexp (Var x) = pqname x
159 paexp (Dcon x) = pqname x
160 paexp (Lit l) = plit l
161 paexp e = parens(pexp e)
162
163 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
164 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
165                     indent (pexp e)]
166
167 pbind (Tb tb) = char '@' <+> ptbind tb
168 pbind (Vb vb) = pvbind vb
169
170 pfexp (App e1 e2) = pappexp e1 [Left e2]
171 pfexp (Appt e t) = pappexp e [Right t]
172 pfexp e = paexp e
173
174 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
175 pappexp (Appt e t) as = pappexp e (Right t:as)
176 pappexp e as = fsep (paexp e : map pa as)
177            where pa (Left ex) = paexp ex
178                  pa (Right t) = char '@' <+> paty t
179
180 pexp (Lam b e) = char '\\' <+> plamexp [b] e
181 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
182 pexp (Case e vb t alts) = sep [text "%case" <+> paty t <+> paexp e,
183                              text "%of" <+> pvbind vb]
184                         $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
185 pexp (Cast e t) = (text "%cast" <+> parens (pexp e)) $$ paty t
186 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
187 -- TODO: ccall shouldn't really be there
188 pexp (External n t) = (text "%external ccall" <+> pstring n) $$ paty t
189 pexp e = pfexp e
190
191
192 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
193
194 palt (Acon c tbs vbs e) =
195         sep [pqname c, 
196              sep (map pattbind tbs),
197              sep (map pvbind vbs) <+> text "->"]
198         $$ indent (pexp e)
199 palt (Alit l e) = 
200         (plit l <+>  text "->")
201         $$ indent (pexp e)
202 palt (Adefault e) = 
203         (text "%_ ->")
204         $$ indent (pexp e)
205
206 plit (Literal cl t) = parens (pclit cl <> text "::" <> pty t)
207
208 pclit (Lint i) = integer i
209 -- makes sure to print it out as n % d
210 pclit (Lrational r) = text (show r)
211 pclit (Lchar c) = text ("\'" ++ escape [c] ++ "\'")
212 pclit (Lstring s) = pstring s
213
214 pstring s = doubleQuotes(text (escape s))
215
216 escape :: String -> String
217 escape s = foldr f [] (map ord s)
218     where 
219      f cv rest
220         | cv > 0xFF = '\\':'x':hs ++ rest
221         | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) = 
222          '\\':'x':h1:h0:rest
223            where (q1,r1) = quotRem cv 16
224                  h1 = intToDigit q1
225                  h0 = intToDigit r1
226                  hs = dropWhile (=='0') $ reverse $ mkHex cv
227                  mkHex 0 = ""
228                  mkHex num = intToDigit r : mkHex q
229                     where (q,r) = quotRem num 16
230      f cv rest = (chr cv):rest