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