404fda9bc8ed5ff4320eae9490b491e0277299fb
[ghc-hetmet.git] / utils / ext-core / Printer.hs
1 module Printer where
2
3 import Text.PrettyPrint.HughesPJ
4 import Numeric (fromRat)
5 import Char
6
7 import Core
8 import Encoding
9
10 instance Show Module where
11   showsPrec d m = shows (pmodule m)
12
13 instance Show Tdef where
14   showsPrec d t = shows (ptdef t)
15
16 instance Show Cdef where
17   showsPrec d c = shows (pcdef c)
18
19 instance Show Vdefg where
20   showsPrec d v = shows (pvdefg v)
21
22 instance Show Vdef where
23   showsPrec d v = shows (pvdef v)
24
25 instance Show Exp where
26   showsPrec d e = shows (pexp e)
27
28 instance Show Alt where
29   showsPrec d a = shows (palt a)
30
31 instance Show Ty where
32   showsPrec d t = shows (pty t)
33
34 instance Show Kind where
35   showsPrec d k = shows (pkind k)
36
37 instance Show Lit where
38   showsPrec d l = shows (plit l)
39
40
41 indent = nest 2
42
43 -- seems like this is asking for a type class...
44
45 pmodule (Module mname tdefs vdefgs) =
46   (text "%module" <+> panmname mname)
47   $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
48              $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
49
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)))))
53
54 ptdef (Newtype qtcon tbinds tyopt ) =
55   text "%newtype" <+> pqname qtcon <+> (hsep (map ptbind tbinds)) <+> 
56         (case tyopt of
57            Just ty -> char '=' <+> pty ty 
58            Nothing -> empty)
59
60 pcdef (Constr qdcon tbinds tys)  =
61   (pqname qdcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
62
63 pname id = text id
64
65 pqname (m,id) = pmname m <> pname id
66
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 '.'
71
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
77          -- with no parents.
78              (case parentStrs of
79                 [] -> empty
80                 _  -> hcat (punctuate hierModuleSeparator 
81                         (map pname parents)) 
82                       <> hierModuleSeparator)
83              <> pname name
84
85 -- note that this is not a '.' but a Z-encoded '.':
86 -- GHCziIOBase.IO, not GHC.IOBase.IO.
87 -- What a pain.
88 hierModuleSeparator = text (zEncodeString ".")
89
90 ptbind (t,Klifted) = pname t
91 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
92
93 pattbind (t,k) = char '@' <> ptbind (t,k)
94
95 pakind (Klifted) = char '*'
96 pakind (Kunlifted) = char '#'
97 pakind (Kopen) = char '?'
98 pakind k = parens (pkind k)
99
100 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
101 pkind k = pakind k
102
103 paty (Tvar n) = pname n
104 paty (Tcon c) = pqname c
105 paty t = parens (pty t)
106
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] 
109 pbty t = paty t
110
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
113 pty t = pbty t
114
115 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
116 pappty t ts = sep (map paty (t:ts))
117
118 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
119 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
120
121 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
122 pvdefg (Nonrec vdef) = pvdef vdef
123
124 pvdef (Vdef (qv,t,e)) = sep [pqname qv <+> text "::" <+> pty t <+> char '=',
125                      indent (pexp e)]
126
127 paexp (Var x) = pqname x
128 paexp (Dcon x) = pqname x
129 paexp (Lit l) = plit l
130 paexp e = parens(pexp e)
131
132 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
133 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
134                     indent (pexp e)]
135
136 pbind (Tb tb) = char '@' <+> ptbind tb
137 pbind (Vb vb) = pvbind vb
138
139 pfexp (App e1 e2) = pappexp e1 [Left e2]
140 pfexp (Appt e t) = pappexp e [Right t]
141 pfexp e = paexp e
142
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
148
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
157 pexp e = pfexp e
158
159
160 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
161
162 palt (Acon c tbs vbs e) =
163         sep [pqname c, 
164              sep (map pattbind tbs),
165              sep (map pvbind vbs) <+> text "->"]
166         $$ indent (pexp e)
167 palt (Alit l e) = 
168         (plit l <+>  text "->")
169         $$ indent (pexp e)
170 palt (Adefault e) = 
171         (text "%_ ->")
172         $$ indent (pexp e)
173
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)
178
179 pstring s = doubleQuotes(text (escape s))
180
181 escape s = foldr f [] (map ord s)
182     where 
183      f cv rest | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) = 
184          '\\':'x':h1:h0:rest
185            where (q1,r1) = quotRem cv 16
186                  h1 = intToDigit q1
187                  h0 = intToDigit r1
188      f cv rest = (chr cv):rest
189