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