2 % (c) The University of Glasgow 2001-2006
6 module PprExternalCore () where
15 instance Show Module where
16 showsPrec _ m = shows (pmodule m)
18 instance Show Tdef where
19 showsPrec _ t = shows (ptdef t)
21 instance Show Cdef where
22 showsPrec _ c = shows (pcdef c)
24 instance Show Vdefg where
25 showsPrec _ v = shows (pvdefg v)
27 instance Show Exp where
28 showsPrec _ e = shows (pexp e)
30 instance Show Alt where
31 showsPrec _ a = shows (palt a)
33 instance Show Ty where
34 showsPrec _ t = shows (pty t)
36 instance Show Kind where
37 showsPrec _ k = shows (pkind k)
39 instance Show Lit where
40 showsPrec _ l = shows (plit l)
46 pmodule :: Module -> Doc
47 pmodule (Module mname tdefs vdefgs) =
48 (text "%module" <+> text mname)
49 $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
50 $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
53 ptdef (Data tcon tbinds cdefs) =
54 (text "%data" <+> pqname tcon <+> (hsep (map ptbind tbinds)) <+> char '=')
55 $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
57 ptdef (Newtype tcon coercion tbinds rep) =
58 text "%newtype" <+> pqname tcon <+> pqname coercion
59 <+> (hsep (map ptbind tbinds)) $$ indent repclause
60 where repclause = case rep of
61 Just ty -> char '=' <+> pty ty
66 pcdef (Constr dcon tbinds tys) =
67 (pqname dcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
68 pcdef (GadtConstr dcon ty) =
69 (pqname dcon) <+> text "::" <+> pty ty
72 pname id = text (zEncodeString id)
74 pqname :: Qual Id -> Doc
75 pqname ("",id) = pname id
76 pqname (m,id) = text m <> char '.' <> pname id
78 ptbind, pattbind :: Tbind -> Doc
79 ptbind (t,Klifted) = pname t
80 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
82 pattbind (t,k) = char '@' <> ptbind (t,k)
84 pakind, pkind :: Kind -> Doc
85 pakind (Klifted) = char '*'
86 pakind (Kunlifted) = char '#'
87 pakind (Kopen) = char '?'
88 pakind k = parens (pkind k)
90 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
91 pkind (Keq t1 t2) = parens (parens (pty t1) <+> text ":=:" <+>
95 paty, pbty, pty :: Ty -> Doc
96 paty (Tvar n) = pname n
97 paty (Tcon c) = pqname c
98 paty t = parens (pty t)
100 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
101 pbty (Tapp t1 t2) = parens $ pappty t1 [t2]
104 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
105 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
108 pappty :: Ty -> [Ty] -> Doc
109 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
110 pappty t ts = sep (map paty (t:ts))
112 pforall :: [Tbind] -> Ty -> Doc
113 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
114 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
116 pvdefg :: Vdefg -> Doc
117 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
118 pvdefg (Nonrec vdef) = pvdef vdef
121 -- TODO: Think about whether %local annotations are actually needed.
122 -- Right now, the local flag is never used, because the Core doc doesn't
123 -- explain the meaning of %local.
124 pvdef (_l,v,t,e) = sep [(pqname v <+> text "::" <+> pty t <+> char '='),
127 paexp, pfexp, pexp :: Exp -> Doc
128 paexp (Var x) = pqname x
129 paexp (Dcon x) = pqname x
130 paexp (Lit l) = plit l
131 paexp e = parens(pexp e)
133 plamexp :: [Bind] -> Exp -> Doc
134 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
135 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
139 pbind (Tb tb) = char '@' <+> ptbind tb
140 pbind (Vb vb) = pvbind vb
142 pfexp (App e1 e2) = pappexp e1 [Left e2]
143 pfexp (Appt e t) = pappexp e [Right t]
146 pappexp :: Exp -> [Either Exp Ty] -> Doc
147 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
148 pappexp (Appt e t) as = pappexp e (Right t:as)
149 pappexp e as = fsep (paexp e : map pa as)
150 where pa (Left e) = paexp e
151 pa (Right t) = char '@' <+> paty t
153 pexp (Lam b e) = char '\\' <+> plamexp [b] e
154 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
155 pexp (Case e vb ty alts) = sep [text "%case" <+> paty ty <+> paexp e,
156 text "%of" <+> pvbind vb]
157 $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
158 pexp (Cast e co) = (text "%cast" <+> parens (pexp e)) $$ paty co
159 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
160 pexp (External n cc t) = (text "%external" <+> text cc <+> pstring n) $$ paty t
161 pexp (DynExternal cc t) = (text "%dynexternal" <+> text cc) $$ paty t
162 pexp (Label n) = (text "%label" <+> pstring n)
165 pvbind :: Vbind -> Doc
166 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
169 palt (Acon c tbs vbs e) =
171 sep (map pattbind tbs),
172 sep (map pvbind vbs) <+> text "->"]
175 (plit l <+> text "->")
182 plit (Lint i t) = parens (integer i <> text "::" <> pty t)
183 -- we use (text (show r)) because "(rational r)" was printing out things
184 -- like "2.0e-2" (which isn't External Core)
185 plit (Lrational r t) = parens (text (show r) <> text "::" <> pty t)
186 plit (Lchar c t) = parens (text ("\'" ++ escape [c] ++ "\'") <> text "::" <> pty t)
187 plit (Lstring s t) = parens (pstring s <> text "::" <> pty t)
189 pstring :: String -> Doc
190 pstring s = doubleQuotes(text (escape s))
192 escape :: String -> String
193 escape s = foldr f [] (map ord s)
196 | cv > 0xFF = '\\':'x':hs ++ rest
197 | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) =
199 where (q1,r1) = quotRem cv 16
202 hs = dropWhile (=='0') $ reverse $ mkHex cv
204 mkHex cv = intToDigit r : mkHex q
205 where (q,r) = quotRem cv 16
206 f cv rest = (chr cv):rest