76ef6da9bb3b5e2ec2cf90f109ba6110167d4256
[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 Encoding
9 import ExternalCore
10
11 import Pretty
12 import Char
13
14 instance Show Module where
15   showsPrec _ m = shows (pmodule m)
16
17 instance Show Tdef where
18   showsPrec _ t = shows (ptdef t)
19
20 instance Show Cdef where
21   showsPrec _ c = shows (pcdef c)
22
23 instance Show Vdefg where
24   showsPrec _ v = shows (pvdefg 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 Lit where
39   showsPrec _ l = shows (plit l)
40
41
42 indent :: Doc -> Doc
43 indent = nest 2
44
45 pmodule :: Module -> Doc
46 pmodule (Module mname tdefs vdefgs) =
47   (text "%module" <+> text mname)
48     $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
49                $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
50
51 ptdef :: Tdef -> Doc
52 ptdef (Data tcon tbinds cdefs) =
53   (text "%data" <+> pqname tcon <+> (hsep (map ptbind tbinds)) <+> char '=')
54   $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
55
56 ptdef (Newtype tcon coercion tbinds rep) =
57   text "%newtype" <+> pqname tcon <+> pqname coercion 
58    <+> (hsep (map ptbind tbinds)) $$ indent repclause
59        where repclause = char '=' <+> pty rep
60
61 pcdef :: Cdef -> Doc
62 pcdef (Constr dcon tbinds tys)  =
63   (pqname dcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
64 pcdef (GadtConstr dcon ty)  =
65   (pqname dcon) <+> text "::" <+> pty ty
66
67 pname :: Id -> Doc
68 pname id = text (zEncodeString id)
69
70 pqname :: Qual Id -> Doc
71 pqname ("",id) = pname id
72 pqname (m,id)  = text m <> char '.' <> pname id
73
74 ptbind, pattbind :: Tbind -> Doc
75 ptbind (t,Klifted) = pname t
76 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
77
78 pattbind (t,k) = char '@' <> ptbind (t,k)
79
80 pakind, pkind :: Kind -> Doc
81 pakind (Klifted) = char '*'
82 pakind (Kunlifted) = char '#'
83 pakind (Kopen) = char '?'
84 pakind k = parens (pkind k)
85
86 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
87 pkind (Keq t1 t2) = parens (parens (pty t1) <+> text ":=:" <+> 
88                             parens (pty t2))
89 pkind k = pakind k
90
91 paty, pbty, pty :: Ty -> Doc
92 paty (Tvar n) = pname n
93 paty (Tcon c) = pqname c
94 paty t = parens (pty t)
95
96 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
97 pbty (Tapp t1 t2) = parens $ pappty t1 [t2] 
98 pbty t = paty t
99
100 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
101 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
102 pty (TransCoercion t1 t2) =
103   sep [text "%trans", paty t1, paty t2]
104 pty (SymCoercion t) =
105   sep [text "%sym", paty t]
106 pty (UnsafeCoercion t1 t2) =
107   sep [text "%unsafe", paty t1, paty t2]
108 pty (LeftCoercion t) =
109   sep [text "%left", paty t]
110 pty (RightCoercion t) =
111   sep [text "%right", paty t]
112 pty (InstCoercion t1 t2) =
113   sep [text "%inst", paty t1, paty t2]
114 pty t = pbty t
115
116 pappty :: Ty -> [Ty] -> Doc
117 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
118 pappty t ts = sep (map paty (t:ts))
119
120 pforall :: [Tbind] -> Ty -> Doc
121 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
122 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
123
124 pvdefg :: Vdefg -> Doc
125 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
126 pvdefg (Nonrec vdef) = pvdef vdef
127
128 pvdef :: Vdef -> Doc
129 -- TODO: Think about whether %local annotations are actually needed.
130 -- Right now, the local flag is never used, because the Core doc doesn't
131 -- explain the meaning of %local.
132 pvdef (_l,v,t,e) = sep [(pqname v <+> text "::" <+> pty t <+> char '='),
133                     indent (pexp e)]
134
135 paexp, pfexp, pexp :: Exp -> Doc
136 paexp (Var x) = pqname x
137 paexp (Dcon x) = pqname x
138 paexp (Lit l) = plit l
139 paexp e = parens(pexp e)
140
141 plamexp :: [Bind] -> Exp -> Doc
142 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
143 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
144                     indent (pexp e)]
145
146 pbind :: Bind -> Doc
147 pbind (Tb tb) = char '@' <+> ptbind tb
148 pbind (Vb vb) = pvbind vb
149
150 pfexp (App e1 e2) = pappexp e1 [Left e2]
151 pfexp (Appt e t) = pappexp e [Right t]
152 pfexp e = paexp e
153
154 pappexp :: Exp -> [Either Exp Ty] -> Doc
155 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
156 pappexp (Appt e t) as = pappexp e (Right t:as)
157 pappexp e as = fsep (paexp e : map pa as)
158            where pa (Left e) = paexp e
159                  pa (Right t) = char '@' <+> paty t
160
161 pexp (Lam b e) = char '\\' <+> plamexp [b] e
162 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
163 pexp (Case e vb ty alts) = sep [text "%case" <+> paty ty <+> paexp e,
164                              text "%of" <+> pvbind vb]
165                         $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
166 pexp (Cast e co) = (text "%cast" <+> parens (pexp e)) $$ paty co
167 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
168 pexp (External n cc t) = (text "%external" <+> text cc <+> pstring n) $$ paty t
169 pexp (DynExternal cc t) = (text "%dynexternal" <+> text cc) $$ paty t
170 pexp (Label n) = (text "%label" <+> pstring n)
171 pexp e = pfexp e
172
173 pvbind :: Vbind -> Doc
174 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
175
176 palt :: Alt -> Doc
177 palt (Acon c tbs vbs e) =
178         sep [pqname c, 
179              sep (map pattbind tbs),
180              sep (map pvbind vbs) <+> text "->"]
181         $$ indent (pexp e)
182 palt (Alit l e) = 
183         (plit l <+>  text "->")
184         $$ indent (pexp e)
185 palt (Adefault e) = 
186         (text "%_ ->")
187         $$ indent (pexp e)
188
189 plit :: Lit -> Doc
190 plit (Lint i t) = parens (integer i <> text "::" <> pty t)
191 -- we use (text (show r)) because "(rational r)" was printing out things
192 -- like "2.0e-2" (which isn't External Core)
193 plit (Lrational r t) = parens (text (show r) <>  text "::" <> pty t)
194 plit (Lchar c t) = parens (text ("\'" ++ escape [c] ++ "\'") <> text "::" <> pty t)
195 plit (Lstring s t) = parens (pstring s <> text "::" <> pty t)
196
197 pstring :: String -> Doc
198 pstring s = doubleQuotes(text (escape s))
199
200 escape :: String -> String
201 escape s = foldr f [] (map ord s)
202     where 
203      f cv rest
204         | cv > 0xFF = '\\':'x':hs ++ rest
205         | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) = 
206          '\\':'x':h1:h0:rest
207            where (q1,r1) = quotRem cv 16
208                  h1 = intToDigit q1
209                  h0 = intToDigit r1
210                  hs = dropWhile (=='0') $ reverse $ mkHex cv
211                  mkHex 0 = ""
212                  mkHex cv = intToDigit r : mkHex q
213                     where (q,r) = quotRem cv 16
214      f cv rest = (chr cv):rest
215
216 \end{code}
217
218
219
220