[project @ 2001-08-17 17:18:51 by apt]
[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 {- (texports,dexports,vexports) -}  tdefs vdefs) =
43   (text "%module" <+> text mname)
44 {-  $$ indent (parens (((fsep (map pname texports) <> char ',')
45                         $$ (fsep (map pname dexports) <> char ',')
46                         $$ (fsep (map pname vexports)))) 
47 -}
48     $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
49                $$ (vcat (map ((<> char ';') . pgvdef) vdefs)))
50
51 pgvdef (False,vdef) = text "%local" <+> pvdefg vdef
52 pgvdef (True,vdef) = pvdefg vdef
53
54 ptdef (Data tcon tbinds cdefs) =
55   (text "%data" <+> pname tcon <+> (hsep (map ptbind tbinds)) <+> char '=')
56   $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
57
58 ptdef (Newtype tcon tbinds rep ) =
59   text "%newtype" <+> pname tcon <+> (hsep (map ptbind tbinds)) <+> repclause
60        where repclause = case rep of
61                            Just ty -> char '=' <+> pty ty 
62                            Nothing -> empty
63
64 pcdef (Constr dcon tbinds tys)  =
65   (pname dcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
66
67 pname id = text id
68
69 pqname ("",id) = pname id
70 pqname (m,id) = pname m <> char '.' <> pname id
71
72 ptbind (t,Klifted) = pname t
73 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
74
75 pattbind (t,k) = char '@' <> ptbind (t,k)
76
77 pakind (Klifted) = char '*'
78 pakind (Kunlifted) = char '#'
79 pakind (Kopen) = char '?'
80 pakind k = parens (pkind k)
81
82 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
83 pkind k = pakind k
84
85 paty (Tvar n) = pname n
86 paty (Tcon c) = pqname c
87 paty t = parens (pty t)
88
89 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
90 pbty (Tapp t1 t2) = pappty t1 [t2] 
91 pbty t = paty t
92
93 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
94 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
95 pty t = pbty t
96
97 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
98 pappty t ts = sep (map paty (t:ts))
99
100 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
101 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
102
103 pvdefg (Rec vtes) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvte vtes))))
104 pvdefg (Nonrec vte) = pvte vte
105
106 pvte (v,t,e) = sep [pname v <+> text "::" <+> pty t <+> char '=',
107                     indent (pexp e)]
108
109 paexp (Var x) = pqname x
110 paexp (Dcon x) = pqname x
111 paexp (Lit l) = plit l
112 paexp e = parens(pexp e)
113
114 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
115 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
116                     indent (pexp e)]
117
118 pbind (Tb tb) = char '@' <+> ptbind tb
119 pbind (Vb vb) = pvbind vb
120
121 pfexp (App e1 e2) = pappexp e1 [Left e2]
122 pfexp (Appt e t) = pappexp e [Right t]
123 pfexp e = paexp e
124
125 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
126 pappexp (Appt e t) as = pappexp e (Right t:as)
127 pappexp e as = fsep (paexp e : map pa as)
128            where pa (Left e) = paexp e
129                  pa (Right t) = char '@' <+> paty t
130
131 pexp (Lam b e) = char '\\' <+> plamexp [b] e
132 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
133 pexp (Case e vb alts) = sep [text "%case" <+> paexp e,
134                              text "%of" <+> pvbind vb]
135                         $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
136 pexp (Coerce t e) = (text "%coerce" <+> paty t) $$ pexp e
137 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
138 pexp (External n t) = (text "%external" <+> pstring n) $$ paty t
139 pexp e = pfexp e
140
141
142 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
143
144 palt (Acon c tbs vbs e) =
145         sep [pqname c, 
146              sep (map pattbind tbs),
147              sep (map pvbind vbs) <+> text "->"]
148         $$ indent (pexp e)
149 palt (Alit l e) = 
150         (plit l <+>  text "->")
151         $$ indent (pexp e)
152 palt (Adefault e) = 
153         (text "%_ ->")
154         $$ indent (pexp e)
155
156 plit (Lint i t) = parens (integer i <> text "::" <> pty t)
157 plit (Lrational r t) = parens (rational r <>  text "::" <> pty t)  -- might be better to print as two integers
158 plit (Lchar c t) = parens (text ("\'" ++ escape [c] ++ "\'") <> text "::" <> pty t)
159 plit (Lstring s t) = parens (pstring s <> text "::" <> pty t)
160
161 pstring s = doubleQuotes(text (escape s))
162
163 escape s = foldr f [] (map ord s)
164     where 
165      f cv rest | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) = 
166          '\\':'x':h1:h0:rest
167            where (q1,r1) = quotRem cv 16
168                  h1 = intToDigit q1
169                  h0 = intToDigit r1
170      f cv rest = (chr cv):rest
171
172 \end{code}
173
174
175
176