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