Remove Distribution.Compat.Char from compat again
[ghc-hetmet.git] / utils / ext-core / Language / Core / Printer.hs
1 {-# OPTIONS -Werror -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 import Language.Core.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 = char '=' <+> pty tyopt
73
74 pcdef (Constr qdcon tbinds tys)  =
75   (pqname qdcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
76
77 pname = text
78
79 pqname (m,v) = pmname m <> pname v
80
81 -- be sure to print the '.' here so we don't print out
82 -- ".foo" for unqualified foo...
83 pmname Nothing = empty
84 pmname (Just m) = panmname m <> char '.'
85
86 panmname (M (P pkgName, parents, name)) =
87   let parentStrs = map pname parents in
88          pname pkgName <> char ':' <>
89          -- This is to be sure to not print out:
90          -- main:.Main for when there's a single module name
91          -- with no parents.
92              (case parentStrs of
93                 [] -> empty
94                 _  -> hcat (punctuate hierModuleSeparator 
95                         (map pname parents)) 
96                       <> hierModuleSeparator)
97              <> pname name
98
99 -- note that this is not a '.' but a Z-encoded '.':
100 -- GHCziIOBase.IO, not GHC.IOBase.IO.
101 -- What a pain.
102 hierModuleSeparator = text (zEncodeString ".")
103
104 ptbind (t,Klifted) = pname t
105 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
106
107 pattbind (t,k) = char '@' <> ptbind (t,k)
108
109 pakind (Klifted) = char '*'
110 pakind (Kunlifted) = char '#'
111 pakind (Kopen) = char '?'
112 pakind k = parens (pkind k)
113
114 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
115 pkind (Keq from to) = peqkind (from,to)
116 pkind k = pakind k
117
118 peqkind (t1, t2) = parens (parens (pty t1) <+> text ":=:" <+> parens (pty t2)) 
119
120 paty (Tvar n) = pname n
121 paty (Tcon c) = pqname c
122 paty t = parens (pty t)
123
124 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
125 pbty (Tapp t1 t2) = pappty t1 [t2] 
126 pbty t = paty t
127
128 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
129 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
130 pty (TransCoercion t1 t2) = 
131     (sep ([pqname transCoercion, paty t1, paty t2]))
132 pty (SymCoercion t) = 
133     (sep [pqname symCoercion, paty t])
134 pty (UnsafeCoercion t1 t2) = 
135     (sep [pqname unsafeCoercion, paty t1, paty t2])
136 pty (LeftCoercion t) = 
137     (pqname leftCoercion <+> paty t)
138 pty (RightCoercion t) = 
139     (pqname rightCoercion <+> paty t)
140 pty (InstCoercion t1 t2) = 
141     (sep [pqname instCoercion, paty t1, paty t2])
142 pty t = pbty t
143
144 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
145 pappty t ts = sep (map paty (t:ts))
146
147 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
148 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
149
150 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
151 pvdefg (Nonrec vdef) = pvdef vdef
152
153 pvdef (Vdef (qv,t,e)) = sep [pqname qv <+> text "::" <+> pty t <+> char '=',
154                      indent (pexp e)]
155
156 paexp (Var x) = pqname x
157 paexp (Dcon x) = pqname x
158 paexp (Lit l) = plit l
159 paexp e = parens(pexp e)
160
161 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
162 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
163                     indent (pexp e)]
164
165 pbind (Tb tb) = char '@' <+> ptbind tb
166 pbind (Vb vb) = pvbind vb
167
168 pfexp (App e1 e2) = pappexp e1 [Left e2]
169 pfexp (Appt e t) = pappexp e [Right t]
170 pfexp e = paexp e
171
172 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
173 pappexp (Appt e t) as = pappexp e (Right t:as)
174 pappexp e as = fsep (paexp e : map pa as)
175            where pa (Left ex) = paexp ex
176                  pa (Right t) = char '@' <+> paty t
177
178 pexp (Lam b e) = char '\\' <+> plamexp [b] e
179 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
180 pexp (Case e vb t alts) = sep [text "%case" <+> paty t <+> paexp e,
181                              text "%of" <+> pvbind vb]
182                         $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
183 pexp (Cast e t) = (text "%cast" <+> parens (pexp e)) $$ paty t
184 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
185 -- TODO: ccall shouldn't really be there
186 pexp (External n t) = (text "%external ccall" <+> pstring n) $$ paty t
187 pexp e = pfexp e
188
189
190 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
191
192 palt (Acon c tbs vbs e) =
193         sep [pqname c, 
194              sep (map pattbind tbs),
195              sep (map pvbind vbs) <+> text "->"]
196         $$ indent (pexp e)
197 palt (Alit l e) = 
198         (plit l <+>  text "->")
199         $$ indent (pexp e)
200 palt (Adefault e) = 
201         (text "%_ ->")
202         $$ indent (pexp e)
203
204 plit (Literal cl t) = parens (pclit cl <> text "::" <> pty t)
205
206 pclit (Lint i) = integer i
207 -- makes sure to print it out as n % d
208 pclit (Lrational r) = text (show r)
209 pclit (Lchar c) = text ("\'" ++ escape [c] ++ "\'")
210 pclit (Lstring s) = pstring s
211
212 pstring s = doubleQuotes(text (escape s))
213
214 escape :: String -> String
215 escape s = foldr f [] (map ord s)
216     where 
217      f cv rest
218         | cv > 0xFF = '\\':'x':hs ++ rest
219         | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) = 
220          '\\':'x':h1:h0:rest
221            where (q1,r1) = quotRem cv 16
222                  h1 = intToDigit q1
223                  h0 = intToDigit r1
224                  hs = dropWhile (=='0') $ reverse $ mkHex cv
225                  mkHex 0 = ""
226                  mkHex num = intToDigit r : mkHex q
227                     where (q,r) = quotRem num 16
228      f cv rest = (chr cv):rest