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