remove empty dir
[ghc-hetmet.git] / ghc / compiler / coreSyn / PprExternalCore.lhs
1 %
2 % (c) The University of Glasgow 2001
3 %
4 \begin{code}
5
6 module PprExternalCore () where
7
8 import Pretty
9 import ExternalCore
10 import Char
11
12 instance Show Module where
13   showsPrec d m = shows (pmodule m)
14
15 instance Show Tdef where
16   showsPrec d t = shows (ptdef t)
17
18 instance Show Cdef where
19   showsPrec d c = shows (pcdef c)
20
21 instance Show Vdefg where
22   showsPrec d v = shows (pvdefg v)
23
24 instance Show Exp where
25   showsPrec d e = shows (pexp e)
26
27 instance Show Alt where
28   showsPrec d a = shows (palt a)
29
30 instance Show Ty where
31   showsPrec d t = shows (pty t)
32
33 instance Show Kind where
34   showsPrec d k = shows (pkind k)
35
36 instance Show Lit where
37   showsPrec d l = shows (plit l)
38
39
40 indent = nest 2
41
42 pmodule (Module mname tdefs vdefgs) =
43   (text "%module" <+> text mname)
44     $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
45                $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
46
47 ptdef (Data tcon tbinds cdefs) =
48   (text "%data" <+> pqname tcon <+> (hsep (map ptbind tbinds)) <+> char '=')
49   $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
50
51 ptdef (Newtype tcon tbinds rep ) =
52   text "%newtype" <+> pqname tcon <+> (hsep (map ptbind tbinds)) <+> repclause
53        where repclause = case rep of
54                            Just ty -> char '=' <+> pty ty 
55                            Nothing -> empty
56
57 pcdef (Constr dcon tbinds tys)  =
58   (pname dcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
59 pcdef (GadtConstr dcon ty)  =
60   (pname dcon) <+> text "::" <+> pty ty
61
62 pname id = text id
63
64 pqname ("",id) = pname id
65 pqname (m,id)  = pname m <> char '.' <> pname id
66
67 ptbind (t,Klifted) = pname t
68 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
69
70 pattbind (t,k) = char '@' <> ptbind (t,k)
71
72 pakind (Klifted) = char '*'
73 pakind (Kunlifted) = char '#'
74 pakind (Kopen) = char '?'
75 pakind k = parens (pkind k)
76
77 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
78 pkind k = pakind k
79
80 paty (Tvar n) = pname n
81 paty (Tcon c) = pqname c
82 paty t = parens (pty t)
83
84 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
85 pbty (Tapp t1 t2) = pappty t1 [t2] 
86 pbty t = paty t
87
88 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
89 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
90 pty t = pbty t
91
92 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
93 pappty t ts = sep (map paty (t:ts))
94
95 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
96 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
97
98 pvdefg (Rec vtes) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvte vtes))))
99 pvdefg (Nonrec vte) = pvte vte
100
101 pvte (v,t,e) = sep [pname v <+> text "::" <+> pty t <+> char '=',
102                     indent (pexp e)]
103
104 paexp (Var x) = pqname x
105 paexp (Dcon x) = pqname x
106 paexp (Lit l) = plit l
107 paexp e = parens(pexp e)
108
109 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
110 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
111                     indent (pexp e)]
112
113 pbind (Tb tb) = char '@' <+> ptbind tb
114 pbind (Vb vb) = pvbind vb
115
116 pfexp (App e1 e2) = pappexp e1 [Left e2]
117 pfexp (Appt e t) = pappexp e [Right t]
118 pfexp e = paexp e
119
120 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
121 pappexp (Appt e t) as = pappexp e (Right t:as)
122 pappexp e as = fsep (paexp e : map pa as)
123            where pa (Left e) = paexp e
124                  pa (Right t) = char '@' <+> paty t
125
126 pexp (Lam b e) = char '\\' <+> plamexp [b] e
127 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
128 pexp (Case e vb ty alts) = sep [text "%case" <+> parens (paty ty) <+> paexp e,
129                              text "%of" <+> pvbind vb]
130                         $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
131 pexp (Coerce t e) = (text "%coerce" <+> paty t) $$ pexp e
132 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
133 pexp (External n t) = (text "%external" <+> pstring n) $$ paty t
134 pexp e = pfexp e
135
136
137 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
138
139 palt (Acon c tbs vbs e) =
140         sep [pqname c, 
141              sep (map pattbind tbs),
142              sep (map pvbind vbs) <+> text "->"]
143         $$ indent (pexp e)
144 palt (Alit l e) = 
145         (plit l <+>  text "->")
146         $$ indent (pexp e)
147 palt (Adefault e) = 
148         (text "%_ ->")
149         $$ indent (pexp e)
150
151 plit (Lint i t) = parens (integer i <> text "::" <> pty t)
152 plit (Lrational r t) = parens (rational r <>  text "::" <> pty t)  -- might be better to print as two integers
153 plit (Lchar c t) = parens (text ("\'" ++ escape [c] ++ "\'") <> text "::" <> pty t)
154 plit (Lstring s t) = parens (pstring s <> text "::" <> pty t)
155
156 pstring s = doubleQuotes(text (escape s))
157
158 escape s = foldr f [] (map ord s)
159     where 
160      f cv rest
161         | cv > 0xFF = '\\':'x':hs ++ rest
162         | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) = 
163          '\\':'x':h1:h0:rest
164            where (q1,r1) = quotRem cv 16
165                  h1 = intToDigit q1
166                  h0 = intToDigit r1
167                  hs = dropWhile (=='0') $ reverse $ mkHex cv
168                  mkHex 0 = ""
169                  mkHex cv = intToDigit r : mkHex q
170                     where (q,r) = quotRem cv 16
171      f cv rest = (chr cv):rest
172
173 \end{code}
174
175
176
177