22449df3239d2a1239e67e22fbd7c03eba10f6e4
[ghc-hetmet.git] / compiler / coreSyn / PprExternalCore.lhs
1 %
2 % (c) The University of Glasgow 2001-2006
3 %
4
5 \begin{code}
6 module PprExternalCore () where
7
8 import ExternalCore
9 import Encoding
10
11 import Pretty
12 import Char
13
14
15 instance Show Module where
16   showsPrec _ m = shows (pmodule m)
17
18 instance Show Tdef where
19   showsPrec _ t = shows (ptdef t)
20
21 instance Show Cdef where
22   showsPrec _ c = shows (pcdef c)
23
24 instance Show Vdefg where
25   showsPrec _ v = shows (pvdefg 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
43 indent :: Doc -> Doc
44 indent = nest 2
45
46 pmodule :: Module -> Doc
47 pmodule (Module mname tdefs vdefgs) =
48   (text "%module" <+> text mname)
49     $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
50                $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
51
52 ptdef :: Tdef -> Doc
53 ptdef (Data tcon tbinds cdefs) =
54   (text "%data" <+> pqname tcon <+> (hsep (map ptbind tbinds)) <+> char '=')
55   $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
56
57 ptdef (Newtype tcon tbinds (coercion,k) rep) =
58 -- Here we take apart the newtype tycon in order to get the newtype coercion,
59 -- which needs to be represented in the External Core file because it's not
60 -- straightforward to derive its definition from the newtype declaration alone.
61 -- At the same time, we need the newtype decl to declare the tycon itself.
62 -- Sigh.
63   text "%newtype" <+> pqname tcon <+> (hsep (map ptbind tbinds)) 
64     <+> axiomclause <+> repclause
65        where  axiomclause = char '^' <+> parens (pqname coercion <+> text "::"
66                                      <+> pkind k)
67               repclause   = case rep of
68                               Just ty -> char '=' <+> pty ty 
69                               Nothing -> empty
70              
71
72 pcdef :: Cdef -> Doc
73 pcdef (Constr dcon tbinds tys)  =
74   (pname dcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
75 pcdef (GadtConstr dcon ty)  =
76   (pname dcon) <+> text "::" <+> pty ty
77
78 pname :: Id -> Doc
79 pname id = text (zEncodeString id)
80
81 pqname :: Qual Id -> Doc
82 pqname ("",id) = pname id
83 pqname (m,id)  = text m <> char '.' <> pname id
84
85 ptbind, pattbind :: Tbind -> Doc
86 ptbind (t,Klifted) = pname t
87 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
88
89 pattbind (t,k) = char '@' <> ptbind (t,k)
90
91 pakind, pkind :: Kind -> Doc
92 pakind (Klifted) = char '*'
93 pakind (Kunlifted) = char '#'
94 pakind (Kopen) = char '?'
95 pakind k = parens (pkind k)
96
97 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
98 pkind (Keq t1 t2) = parens (parens (pty t1) <+> text ":=:" <+> 
99                             parens (pty t2))
100 pkind k = pakind k
101
102 paty, pbty, pty :: Ty -> Doc
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 :: Ty -> [Ty] -> Doc
116 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
117 pappty t ts = sep (map paty (t:ts))
118
119 pforall :: [Tbind] -> Ty -> Doc
120 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
121 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
122
123 pvdefg :: Vdefg -> Doc
124 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
125 pvdefg (Nonrec vdef) = pvdef vdef
126
127 pvdef :: Vdef -> Doc
128 -- note: at one point every vdef was getting printed out as "local".
129 -- I think that's manifestly wrong. Right now, the "%local" keyword
130 -- is never used.
131 pvdef (_l,v,t,e) = sep [pname v <+> text "::" <+> pty t <+> char '=',
132                     indent (pexp e)]
133
134 paexp, pfexp, pexp :: Exp -> Doc
135 paexp (Var x) = pqname x
136 paexp (Dcon x) = pqname x
137 paexp (Lit l) = plit l
138 paexp e = parens(pexp e)
139
140 plamexp :: [Bind] -> Exp -> Doc
141 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
142 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
143                     indent (pexp e)]
144
145 pbind :: Bind -> Doc
146 pbind (Tb tb) = char '@' <+> ptbind tb
147 pbind (Vb vb) = pvbind vb
148
149 pfexp (App e1 e2) = pappexp e1 [Left e2]
150 pfexp (Appt e t) = pappexp e [Right t]
151 pfexp e = paexp e
152
153 pappexp :: Exp -> [Either Exp Ty] -> Doc
154 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
155 pappexp (Appt e t) as = pappexp e (Right t:as)
156 pappexp e as = fsep (paexp e : map pa as)
157            where pa (Left e) = paexp e
158                  pa (Right t) = char '@' <+> paty t
159
160 pexp (Lam b e) = char '\\' <+> plamexp [b] e
161 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
162 pexp (Case e vb ty alts) = sep [text "%case" <+> parens (paty ty) <+> paexp e,
163                              text "%of" <+> pvbind vb]
164                         $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
165 pexp (Cast e co) = (text "%cast" <+> parens (pexp e)) $$ paty co
166 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
167 pexp (External n cc t) = (text "%external" <+> text cc <+> pstring n) $$ paty t
168 pexp (DynExternal cc t) = (text "%dynexternal" <+> text cc) $$ paty t
169 pexp (Label n) = (text "%label" <+> pstring n)
170 pexp e = pfexp e
171
172 pvbind :: Vbind -> Doc
173 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
174
175 palt :: Alt -> Doc
176 palt (Acon c tbs vbs e) =
177         sep [pqname c, 
178              sep (map pattbind tbs),
179              sep (map pvbind vbs) <+> text "->"]
180         $$ indent (pexp e)
181 palt (Alit l e) = 
182         (plit l <+>  text "->")
183         $$ indent (pexp e)
184 palt (Adefault e) = 
185         (text "%_ ->")
186         $$ indent (pexp e)
187
188 plit :: Lit -> Doc
189 plit (Lint i t) = parens (integer i <> text "::" <> pty t)
190 plit (Lrational r t) = parens (rational r <>  text "::" <> pty t)  -- might be better to print as two integers
191 plit (Lchar c t) = parens (text ("\'" ++ escape [c] ++ "\'") <> text "::" <> pty t)
192 plit (Lstring s t) = parens (pstring s <> text "::" <> pty t)
193
194 pstring :: String -> Doc
195 pstring s = doubleQuotes(text (escape s))
196
197 escape :: String -> String
198 escape s = foldr f [] (map ord s)
199     where 
200      f cv rest
201         | cv > 0xFF = '\\':'x':hs ++ rest
202         | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) = 
203          '\\':'x':h1:h0:rest
204            where (q1,r1) = quotRem cv 16
205                  h1 = intToDigit q1
206                  h0 = intToDigit r1
207                  hs = dropWhile (=='0') $ reverse $ mkHex cv
208                  mkHex 0 = ""
209                  mkHex cv = intToDigit r : mkHex q
210                     where (q,r) = quotRem cv 16
211      f cv rest = (chr cv):rest
212
213 \end{code}
214
215
216
217