2 % (c) The University of Glasgow 2001-2006
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/Commentary/CodingStyle#Warnings
13 module PprExternalCore () where
20 instance Show Module where
21 showsPrec d m = shows (pmodule m)
23 instance Show Tdef where
24 showsPrec d t = shows (ptdef t)
26 instance Show Cdef where
27 showsPrec d c = shows (pcdef c)
29 instance Show Vdefg where
30 showsPrec d v = shows (pvdefg v)
32 instance Show Exp where
33 showsPrec d e = shows (pexp e)
35 instance Show Alt where
36 showsPrec d a = shows (palt a)
38 instance Show Ty where
39 showsPrec d t = shows (pty t)
41 instance Show Kind where
42 showsPrec d k = shows (pkind k)
44 instance Show Lit where
45 showsPrec d l = shows (plit l)
50 pmodule (Module mname tdefs vdefgs) =
51 (text "%module" <+> text mname)
52 $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
53 $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
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)))))
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
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
70 pname id = text (zEncodeString id)
72 pqname ("",id) = pname id
73 pqname (m,id) = text m <> char '.' <> pname id
75 ptbind (t,Klifted) = pname t
76 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
78 pattbind (t,k) = char '@' <> ptbind (t,k)
80 pakind (Klifted) = char '*'
81 pakind (Kunlifted) = char '#'
82 pakind (Kopen) = char '?'
83 pakind k = parens (pkind k)
85 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
86 pkind (Keq t1 t2) = parens (pty t1 <> text ":=:" <> pty t2)
89 paty (Tvar n) = pname n
90 paty (Tcon c) = pqname c
91 paty t = parens (pty t)
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]
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
101 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
102 pappty t ts = sep (map paty (t:ts))
104 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
105 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
107 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
108 pvdefg (Nonrec vdef) = pvdef vdef
110 pvdef (l,v,t,e) = sep [plocal l <+> pname v <+> text "::" <+> pty t <+> char '=',
113 plocal True = text "%local"
116 paexp (Var x) = pqname x
117 paexp (Dcon x) = pqname x
118 paexp (Lit l) = plit l
119 paexp e = parens(pexp e)
121 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
122 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
125 pbind (Tb tb) = char '@' <+> ptbind tb
126 pbind (Vb vb) = pvbind vb
128 pfexp (App e1 e2) = pappexp e1 [Left e2]
129 pfexp (Appt e t) = pappexp e [Right t]
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
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)
151 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
153 palt (Acon c tbs vbs e) =
155 sep (map pattbind tbs),
156 sep (map pvbind vbs) <+> text "->"]
159 (plit l <+> text "->")
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)
170 pstring s = doubleQuotes(text (escape s))
172 escape s = foldr f [] (map ord s)
175 | cv > 0xFF = '\\':'x':hs ++ rest
176 | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) =
178 where (q1,r1) = quotRem cv 16
181 hs = dropWhile (=='0') $ reverse $ mkHex cv
183 mkHex cv = intToDigit r : mkHex q
184 where (q,r) = quotRem cv 16
185 f cv rest = (chr cv):rest