root/compiler/coreSyn/PprExternalCore.lhs

Revision 1df198643cc5502ee103f043193d2990c9837e25, 6.6 KB (checked in by Ian Lynagh <igloo@…>, 7 months ago)

Use -fwarn-tabs when validating

We only use it for "compiler" sources, i.e. not for libraries.
Many modules have a -fno-warn-tabs kludge for now.

  • Property mode set to 100644
Line 
1%
2% (c) The University of Glasgow 2001-2006
3%
4
5\begin{code}
6{-# OPTIONS -fno-warn-tabs #-}
7-- The above warning supression flag is a temporary kludge.
8-- While working on this module you are encouraged to remove it and
9-- detab the module (please do the detabbing in a separate patch). See
10--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
11-- for details
12
13module PprExternalCore () where
14
15import Encoding
16import ExternalCore
17
18import Pretty
19import Data.Char
20import Data.Ratio
21
22instance Show Module where
23  showsPrec _ m = shows (pmodule m)
24
25instance Show Tdef where
26  showsPrec _ t = shows (ptdef t)
27
28instance Show Cdef where
29  showsPrec _ c = shows (pcdef c)
30
31instance Show Vdefg where
32  showsPrec _ v = shows (pvdefg v)
33
34instance Show Exp where
35  showsPrec _ e = shows (pexp e)
36
37instance Show Alt where
38  showsPrec _ a = shows (palt a)
39
40instance Show Ty where
41  showsPrec _ t = shows (pty t)
42
43instance Show Kind where
44  showsPrec _ k = shows (pkind k)
45
46instance Show Lit where
47  showsPrec _ l = shows (plit l)
48
49
50indent :: Doc -> Doc
51indent = nest 2
52
53pmodule :: Module -> Doc
54pmodule (Module mname tdefs vdefgs) =
55  (text "%module" <+> text mname)
56    $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
57               $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
58
59ptdef :: Tdef -> Doc
60ptdef (Data tcon tbinds cdefs) =
61  (text "%data" <+> pqname tcon <+> (hsep (map ptbind tbinds)) <+> char '=')
62  $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
63
64ptdef (Newtype tcon coercion tbinds rep) =
65  text "%newtype" <+> pqname tcon <+> pqname coercion
66   <+> (hsep (map ptbind tbinds)) $$ indent repclause
67       where repclause = char '=' <+> pty rep
68
69pcdef :: Cdef -> Doc
70pcdef (Constr dcon tbinds tys)  =
71  (pqname dcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
72pcdef (GadtConstr dcon ty)  =
73  (pqname dcon) <+> text "::" <+> pty ty
74
75pname :: Id -> Doc
76pname id = text (zEncodeString id)
77
78pqname :: Qual Id -> Doc
79pqname ("",id) = pname id
80pqname (m,id)  = text m <> char '.' <> pname id
81
82ptbind, pattbind :: Tbind -> Doc
83ptbind (t,Klifted) = pname t
84ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
85
86pattbind (t,k) = char '@' <> ptbind (t,k)
87
88pakind, pkind :: Kind -> Doc
89pakind (Klifted) = char '*'
90pakind (Kunlifted) = char '#'
91pakind (Kopen) = char '?'
92pakind k = parens (pkind k)
93
94pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
95pkind k = pakind k
96
97paty, pbty, pty :: Ty -> Doc
98paty (Tvar n) = pname n
99paty (Tcon c) = pqname c
100paty t = parens (pty t)
101
102pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
103pbty (Tapp t1 t2) = parens $ pappty t1 [t2] 
104pbty t = paty t
105
106pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
107pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
108pty (TransCoercion t1 t2) =
109  sep [text "%trans", paty t1, paty t2]
110pty (SymCoercion t) =
111  sep [text "%sym", paty t]
112pty (UnsafeCoercion t1 t2) =
113  sep [text "%unsafe", paty t1, paty t2]
114pty (NthCoercion n t) =
115  sep [text "%nth", int n, paty t]
116pty (InstCoercion t1 t2) =
117  sep [text "%inst", paty t1, paty t2]
118pty t = pbty t
119
120pappty :: Ty -> [Ty] -> Doc
121pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
122pappty t ts = sep (map paty (t:ts))
123
124pforall :: [Tbind] -> Ty -> Doc
125pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
126pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
127
128pvdefg :: Vdefg -> Doc
129pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
130pvdefg (Nonrec vdef) = pvdef vdef
131
132pvdef :: Vdef -> Doc
133-- TODO: Think about whether %local annotations are actually needed.
134-- Right now, the local flag is never used, because the Core doc doesn't
135-- explain the meaning of %local.
136pvdef (_l,v,t,e) = sep [(pqname v <+> text "::" <+> pty t <+> char '='),
137                    indent (pexp e)]
138
139paexp, pfexp, pexp :: Exp -> Doc
140paexp (Var x) = pqname x
141paexp (Dcon x) = pqname x
142paexp (Lit l) = plit l
143paexp e = parens(pexp e)
144
145plamexp :: [Bind] -> Exp -> Doc
146plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
147plamexp bs e = sep [sep (map pbind bs) <+> text "->",
148                    indent (pexp e)]
149
150pbind :: Bind -> Doc
151pbind (Tb tb) = char '@' <+> ptbind tb
152pbind (Vb vb) = pvbind vb
153
154pfexp (App e1 e2) = pappexp e1 [Left e2]
155pfexp (Appt e t) = pappexp e [Right t]
156pfexp e = paexp e
157
158pappexp :: Exp -> [Either Exp Ty] -> Doc
159pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
160pappexp (Appt e t) as = pappexp e (Right t:as)
161pappexp e as = fsep (paexp e : map pa as)
162           where pa (Left e) = paexp e
163                 pa (Right t) = char '@' <+> paty t
164
165pexp (Lam b e) = char '\\' <+> plamexp [b] e
166pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
167pexp (Case e vb ty alts) = sep [text "%case" <+> paty ty <+> paexp e,
168                             text "%of" <+> pvbind vb]
169                        $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
170pexp (Cast e co) = (text "%cast" <+> parens (pexp e)) $$ paty co
171pexp (Tick s e) = (text "%source" <+> pstring s) $$ pexp e
172pexp (External n cc t) = (text "%external" <+> text cc <+> pstring n) $$ paty t
173pexp (DynExternal cc t) = (text "%dynexternal" <+> text cc) $$ paty t
174pexp (Label n) = (text "%label" <+> pstring n)
175pexp e = pfexp e
176
177pvbind :: Vbind -> Doc
178pvbind (x,t) = parens(pname x <> text "::" <> pty t)
179
180palt :: Alt -> Doc
181palt (Acon c tbs vbs e) =
182        sep [pqname c, 
183             sep (map pattbind tbs),
184             sep (map pvbind vbs) <+> text "->"]
185        $$ indent (pexp e)
186palt (Alit l e) = 
187        (plit l <+>  text "->")
188        $$ indent (pexp e)
189palt (Adefault e) = 
190        (text "%_ ->")
191        $$ indent (pexp e)
192
193plit :: Lit -> Doc
194plit (Lint i t) = parens (integer i <> text "::" <> pty t)
195-- we use (text (show (numerator r))) (and the same for denominator)
196-- because "(rational r)" was printing out things like "2.0e-2" (which
197-- isn't External Core), and (text (show r)) was printing out things
198-- like "((-1)/5)" which isn't either (it should be "(-1/5)").
199plit (Lrational r t) = parens (text (show (numerator r)) <+> char '%'
200   <+> text (show (denominator r)) <>  text "::" <> pty t)
201plit (Lchar c t) = parens (text ("\'" ++ escape [c] ++ "\'") <> text "::" <> pty t)
202plit (Lstring s t) = parens (pstring s <> text "::" <> pty t)
203
204pstring :: String -> Doc
205pstring s = doubleQuotes(text (escape s))
206
207escape :: String -> String
208escape s = foldr f [] (map ord s)
209    where 
210     f cv rest
211        | cv > 0xFF = '\\':'x':hs ++ rest
212        | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) = 
213         '\\':'x':h1:h0:rest
214           where (q1,r1) = quotRem cv 16
215                 h1 = intToDigit q1
216                 h0 = intToDigit r1
217                 hs = dropWhile (=='0') $ reverse $ mkHex cv
218                 mkHex 0 = ""
219                 mkHex cv = intToDigit r : mkHex q
220                    where (q,r) = quotRem cv 16
221     f cv rest = (chr cv):rest
222
223\end{code}
224
225
226
Note: See TracBrowser for help on using the browser.