Fix external core syntax (though not full compilation)
[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 Pretty
9 import ExternalCore
10 import Char
11 import Encoding
12
13 instance Show Module where
14   showsPrec d m = shows (pmodule m)
15
16 instance Show Tdef where
17   showsPrec d t = shows (ptdef t)
18
19 instance Show Cdef where
20   showsPrec d c = shows (pcdef c)
21
22 instance Show Vdefg where
23   showsPrec d v = shows (pvdefg v)
24
25 instance Show Exp where
26   showsPrec d e = shows (pexp e)
27
28 instance Show Alt where
29   showsPrec d a = shows (palt a)
30
31 instance Show Ty where
32   showsPrec d t = shows (pty t)
33
34 instance Show Kind where
35   showsPrec d k = shows (pkind k)
36
37 instance Show Lit where
38   showsPrec d l = shows (plit l)
39
40
41 indent = nest 2
42
43 pmodule (Module mname tdefs vdefgs) =
44   (text "%module" <+> text mname)
45     $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
46                $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
47
48 ptdef (Data tcon tbinds cdefs) =
49   (text "%data" <+> pqname tcon <+> (hsep (map ptbind tbinds)) <+> char '=')
50   $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
51
52 ptdef (Newtype tcon tbinds rep ) =
53   text "%newtype" <+> pqname tcon <+> (hsep (map ptbind tbinds)) <+> repclause
54        where repclause = case rep of
55                            Just ty -> char '=' <+> pty ty 
56                            Nothing -> empty
57
58 pcdef (Constr dcon tbinds tys)  =
59   (pname dcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
60 pcdef (GadtConstr dcon ty)  =
61   (pname dcon) <+> text "::" <+> pty ty
62
63 pname id = text (zEncodeString id)
64
65 pqname ("",id) = pname id
66 pqname (m,id)  = text m <> char '.' <> pname id
67
68 ptbind (t,Klifted) = pname t
69 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
70
71 pattbind (t,k) = char '@' <> ptbind (t,k)
72
73 pakind (Klifted) = char '*'
74 pakind (Kunlifted) = char '#'
75 pakind (Kopen) = char '?'
76 pakind k = parens (pkind k)
77
78 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
79 pkind (Keq t1 t2) = parens (pty t1 <> text ":=:" <> pty t2)
80 pkind k = pakind k
81
82 paty (Tvar n) = pname n
83 paty (Tcon c) = pqname c
84 paty t = parens (pty t)
85
86 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
87 pbty (Tapp t1 t2) = pappty t1 [t2] 
88 pbty t = paty t
89
90 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
91 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
92 pty t = pbty t
93
94 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
95 pappty t ts = sep (map paty (t:ts))
96
97 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
98 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
99
100 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
101 pvdefg (Nonrec vdef) = pvdef vdef
102
103 pvdef (l,v,t,e) = sep [plocal l <+> pname v <+> text "::" <+> pty t <+> char '=',
104                     indent (pexp e)]
105
106 plocal True  = text "%local"
107 plocal False = empty
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 ty alts) = sep [text "%case" <+> parens (paty ty) <+> paexp e,
134                              text "%of" <+> pvbind vb]
135                         $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
136 pexp (Cast e co) = (text "%cast" <+> parens (pexp e)) $$ paty co
137 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
138 pexp (External n cc t) = (text "%external" <+> text cc <+> pstring n) $$ paty t
139 pexp (DynExternal cc t) = (text "%dynexternal" <+> text cc) $$ paty t
140 pexp (Label n) = (text "%label" <+> pstring n)
141 pexp e = pfexp e
142
143
144 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
145
146 palt (Acon c tbs vbs e) =
147         sep [pqname c, 
148              sep (map pattbind tbs),
149              sep (map pvbind vbs) <+> text "->"]
150         $$ indent (pexp e)
151 palt (Alit l e) = 
152         (plit l <+>  text "->")
153         $$ indent (pexp e)
154 palt (Adefault e) = 
155         (text "%_ ->")
156         $$ indent (pexp e)
157
158 plit (Lint i t) = parens (integer i <> text "::" <> pty t)
159 plit (Lrational r t) = parens (rational r <>  text "::" <> pty t)  -- might be better to print as two integers
160 plit (Lchar c t) = parens (text ("\'" ++ escape [c] ++ "\'") <> text "::" <> pty t)
161 plit (Lstring s t) = parens (pstring s <> text "::" <> pty t)
162
163 pstring s = doubleQuotes(text (escape s))
164
165 escape s = foldr f [] (map ord s)
166     where 
167      f cv rest
168         | cv > 0xFF = '\\':'x':hs ++ rest
169         | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) = 
170          '\\':'x':h1:h0:rest
171            where (q1,r1) = quotRem cv 16
172                  h1 = intToDigit q1
173                  h0 = intToDigit r1
174                  hs = dropWhile (=='0') $ reverse $ mkHex cv
175                  mkHex 0 = ""
176                  mkHex cv = intToDigit r : mkHex q
177                     where (q,r) = quotRem cv 16
178      f cv rest = (chr cv):rest
179
180 \end{code}
181
182
183
184