-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintExp.hs
More file actions
271 lines (221 loc) · 10.7 KB
/
Copy pathPrintExp.hs
File metadata and controls
271 lines (221 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
-- File generated by the BNF Converter (bnfc 2.9.4).
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
#if __GLASGOW_HASKELL__ <= 708
{-# LANGUAGE OverlappingInstances #-}
#endif
-- | Pretty-printer for PrintExp.
module PrintExp where
import Prelude
( ($), (.)
, Bool(..), (==), (<)
, Int, Integer, Double, (+), (-), (*)
, String, (++)
, ShowS, showChar, showString
, all, elem, foldr, id, map, null, replicate, shows, span
)
import Data.Char ( Char, isSpace )
import qualified AbsExp
-- | The top-level printing method.
printTree :: Print a => a -> String
printTree = render . prt 0
type Doc = [ShowS] -> [ShowS]
doc :: ShowS -> Doc
doc = (:)
render :: Doc -> String
render d = rend 0 False (map ($ "") $ d []) ""
where
rend
:: Int -- ^ Indentation level.
-> Bool -- ^ Pending indentation to be output before next character?
-> [String]
-> ShowS
rend i p = \case
"[" :ts -> char '[' . rend i False ts
"(" :ts -> char '(' . rend i False ts
"{" :ts -> onNewLine i p . showChar '{' . new (i+1) ts
"}" : ";":ts -> onNewLine (i-1) p . showString "};" . new (i-1) ts
"}" :ts -> onNewLine (i-1) p . showChar '}' . new (i-1) ts
[";"] -> char ';'
";" :ts -> char ';' . new i ts
t : ts@(s:_) | closingOrPunctuation s
-> pending . showString t . rend i False ts
t :ts -> pending . space t . rend i False ts
[] -> id
where
-- Output character after pending indentation.
char :: Char -> ShowS
char c = pending . showChar c
-- Output pending indentation.
pending :: ShowS
pending = if p then indent i else id
-- Indentation (spaces) for given indentation level.
indent :: Int -> ShowS
indent i = replicateS (2*i) (showChar ' ')
-- Continue rendering in new line with new indentation.
new :: Int -> [String] -> ShowS
new j ts = showChar '\n' . rend j True ts
-- Make sure we are on a fresh line.
onNewLine :: Int -> Bool -> ShowS
onNewLine i p = (if p then id else showChar '\n') . indent i
-- Separate given string from following text by a space (if needed).
space :: String -> ShowS
space t s =
case (all isSpace t', null spc, null rest) of
(True , _ , True ) -> [] -- remove trailing space
(False, _ , True ) -> t' -- remove trailing space
(False, True, False) -> t' ++ ' ' : s -- add space if none
_ -> t' ++ s
where
t' = showString t []
(spc, rest) = span isSpace s
closingOrPunctuation :: String -> Bool
closingOrPunctuation [c] = c `elem` closerOrPunct
closingOrPunctuation _ = False
closerOrPunct :: String
closerOrPunct = ")],;"
parenth :: Doc -> Doc
parenth ss = doc (showChar '(') . ss . doc (showChar ')')
concatS :: [ShowS] -> ShowS
concatS = foldr (.) id
concatD :: [Doc] -> Doc
concatD = foldr (.) id
replicateS :: Int -> ShowS -> ShowS
replicateS n f = concatS (replicate n f)
-- | The printer class does the job.
class Print a where
prt :: Int -> a -> Doc
instance {-# OVERLAPPABLE #-} Print a => Print [a] where
prt i = concatD . map (prt i)
instance Print Char where
prt _ c = doc (showChar '\'' . mkEsc '\'' c . showChar '\'')
instance Print String where
prt _ = printString
printString :: String -> Doc
printString s = doc (showChar '"' . concatS (map (mkEsc '"') s) . showChar '"')
mkEsc :: Char -> Char -> ShowS
mkEsc q = \case
s | s == q -> showChar '\\' . showChar s
'\\' -> showString "\\\\"
'\n' -> showString "\\n"
'\t' -> showString "\\t"
s -> showChar s
prPrec :: Int -> Int -> Doc -> Doc
prPrec i j = if j < i then parenth else id
instance Print Integer where
prt _ x = doc (shows x)
instance Print Double where
prt _ x = doc (shows x)
instance Print AbsExp.Ident where
prt _ (AbsExp.Ident i) = doc $ showString i
instance Print (AbsExp.Program' a) where
prt i = \case
AbsExp.Program _ topdefs -> prPrec i 0 (concatD [prt 0 topdefs])
instance Print (AbsExp.TopDef' a) where
prt i = \case
AbsExp.FnDef _ type_ id_ args block -> prPrec i 0 (concatD [prt 0 type_, prt 0 id_, doc (showString "("), prt 0 args, doc (showString ")"), prt 0 block])
instance Print [AbsExp.TopDef' a] where
prt _ [] = concatD []
prt _ [x] = concatD [prt 0 x]
prt _ (x:xs) = concatD [prt 0 x, prt 0 xs]
instance Print (AbsExp.Arg' a) where
prt i = \case
AbsExp.Arg _ type_ id_ -> prPrec i 0 (concatD [prt 0 type_, prt 0 id_])
AbsExp.ArgRef _ type_ id_ -> prPrec i 0 (concatD [prt 0 type_, doc (showString "&"), prt 0 id_])
instance Print [AbsExp.Arg' a] where
prt _ [] = concatD []
prt _ [x] = concatD [prt 0 x]
prt _ (x:xs) = concatD [prt 0 x, doc (showString ","), prt 0 xs]
instance Print (AbsExp.Block' a) where
prt i = \case
AbsExp.Block _ stmts -> prPrec i 0 (concatD [doc (showString "{"), prt 0 stmts, doc (showString "}")])
instance Print [AbsExp.Stmt' a] where
prt _ [] = concatD []
prt _ (x:xs) = concatD [prt 0 x, prt 0 xs]
instance Print (AbsExp.Stmt' a) where
prt i = \case
AbsExp.Empty _ -> prPrec i 0 (concatD [doc (showString ";")])
AbsExp.BStmt _ block -> prPrec i 0 (concatD [prt 0 block])
AbsExp.Decl _ type_ items -> prPrec i 0 (concatD [prt 0 type_, prt 0 items, doc (showString ";")])
AbsExp.LocFun _ topdef -> prPrec i 0 (concatD [prt 0 topdef])
AbsExp.Ass _ lval expr -> prPrec i 0 (concatD [prt 0 lval, doc (showString "="), prt 0 expr, doc (showString ";")])
AbsExp.AssTup _ tupls expr -> prPrec i 0 (concatD [doc (showString "("), prt 0 tupls, doc (showString ")"), doc (showString "="), prt 0 expr, doc (showString ";")])
AbsExp.TAss _ lval exprs -> prPrec i 0 (concatD [prt 0 lval, doc (showString "="), doc (showString "("), prt 0 exprs, doc (showString ")"), doc (showString ";")])
AbsExp.Incr _ lval -> prPrec i 0 (concatD [prt 0 lval, doc (showString "++"), doc (showString ";")])
AbsExp.Decr _ lval -> prPrec i 0 (concatD [prt 0 lval, doc (showString "--"), doc (showString ";")])
AbsExp.Ret _ expr -> prPrec i 0 (concatD [doc (showString "return"), prt 0 expr, doc (showString ";")])
AbsExp.Cont _ -> prPrec i 0 (concatD [doc (showString "continue"), doc (showString ";")])
AbsExp.Break _ -> prPrec i 0 (concatD [doc (showString "break"), doc (showString ";")])
AbsExp.Cond _ expr stmt -> prPrec i 0 (concatD [doc (showString "if"), doc (showString "("), prt 0 expr, doc (showString ")"), prt 0 stmt])
AbsExp.CondElse _ expr stmt1 stmt2 -> prPrec i 0 (concatD [doc (showString "if"), doc (showString "("), prt 0 expr, doc (showString ")"), prt 0 stmt1, doc (showString "else"), prt 0 stmt2])
AbsExp.While _ expr stmt -> prPrec i 0 (concatD [doc (showString "while"), doc (showString "("), prt 0 expr, doc (showString ")"), prt 0 stmt])
AbsExp.SExp _ expr -> prPrec i 0 (concatD [prt 0 expr, doc (showString ";")])
instance Print (AbsExp.Item' a) where
prt i = \case
AbsExp.NoInit _ id_ -> prPrec i 0 (concatD [prt 0 id_])
AbsExp.Init _ id_ expr -> prPrec i 0 (concatD [prt 0 id_, doc (showString "="), prt 0 expr])
instance Print [AbsExp.Item' a] where
prt _ [] = concatD []
prt _ [x] = concatD [prt 0 x]
prt _ (x:xs) = concatD [prt 0 x, doc (showString ","), prt 0 xs]
instance Print (AbsExp.Lval' a) where
prt i = \case
AbsExp.SmplVal _ id_ -> prPrec i 0 (concatD [prt 0 id_])
AbsExp.CmplVal _ lval expr -> prPrec i 0 (concatD [prt 0 lval, doc (showString "["), prt 0 expr, doc (showString "]")])
instance Print (AbsExp.Tupl' a) where
prt i = \case
AbsExp.Tup _ lval -> prPrec i 0 (concatD [prt 0 lval])
AbsExp.ETup _ -> prPrec i 0 (concatD [doc (showString "_")])
instance Print [AbsExp.Tupl' a] where
prt _ [] = concatD []
prt _ [x] = concatD [prt 0 x]
prt _ (x:xs) = concatD [prt 0 x, doc (showString ","), prt 0 xs]
instance Print (AbsExp.Type' a) where
prt i = \case
AbsExp.Int _ -> prPrec i 0 (concatD [doc (showString "int")])
AbsExp.Str _ -> prPrec i 0 (concatD [doc (showString "string")])
AbsExp.Bool _ -> prPrec i 0 (concatD [doc (showString "boolean")])
AbsExp.Tuple _ types -> prPrec i 0 (concatD [doc (showString "("), prt 0 types, doc (showString ")")])
AbsExp.Array _ type_ n -> prPrec i 0 (concatD [prt 0 type_, doc (showString "["), prt 0 n, doc (showString "]")])
AbsExp.Fun _ type_ types -> prPrec i 0 (concatD [prt 0 type_, doc (showString "("), prt 0 types, doc (showString ")")])
instance Print [AbsExp.Type' a] where
prt _ [] = concatD []
prt _ [x] = concatD [prt 0 x]
prt _ (x:xs) = concatD [prt 0 x, doc (showString ","), prt 0 xs]
instance Print (AbsExp.Expr' a) where
prt i = \case
AbsExp.EVar _ lval -> prPrec i 6 (concatD [prt 0 lval])
AbsExp.ELitInt _ n -> prPrec i 6 (concatD [prt 0 n])
AbsExp.ELitTrue _ -> prPrec i 6 (concatD [doc (showString "true")])
AbsExp.ELitFalse _ -> prPrec i 6 (concatD [doc (showString "false")])
AbsExp.EApp _ id_ exprs -> prPrec i 6 (concatD [prt 0 id_, doc (showString "("), prt 0 exprs, doc (showString ")")])
AbsExp.EString _ str -> prPrec i 6 (concatD [printString str])
AbsExp.Neg _ expr -> prPrec i 5 (concatD [doc (showString "-"), prt 6 expr])
AbsExp.Not _ expr -> prPrec i 5 (concatD [doc (showString "!"), prt 6 expr])
AbsExp.EMul _ expr1 mulop expr2 -> prPrec i 4 (concatD [prt 4 expr1, prt 0 mulop, prt 5 expr2])
AbsExp.EAdd _ expr1 addop expr2 -> prPrec i 3 (concatD [prt 3 expr1, prt 0 addop, prt 4 expr2])
AbsExp.ERel _ expr1 relop expr2 -> prPrec i 2 (concatD [prt 2 expr1, prt 0 relop, prt 3 expr2])
AbsExp.EAnd _ expr1 expr2 -> prPrec i 1 (concatD [prt 2 expr1, doc (showString "&&"), prt 1 expr2])
AbsExp.EOr _ expr1 expr2 -> prPrec i 0 (concatD [prt 1 expr1, doc (showString "||"), prt 0 expr2])
instance Print [AbsExp.Expr' a] where
prt _ [] = concatD []
prt _ [x] = concatD [prt 0 x]
prt _ (x:xs) = concatD [prt 0 x, doc (showString ","), prt 0 xs]
instance Print (AbsExp.AddOp' a) where
prt i = \case
AbsExp.Plus _ -> prPrec i 0 (concatD [doc (showString "+")])
AbsExp.Minus _ -> prPrec i 0 (concatD [doc (showString "-")])
instance Print (AbsExp.MulOp' a) where
prt i = \case
AbsExp.Times _ -> prPrec i 0 (concatD [doc (showString "*")])
AbsExp.Div _ -> prPrec i 0 (concatD [doc (showString "/")])
AbsExp.Mod _ -> prPrec i 0 (concatD [doc (showString "%")])
instance Print (AbsExp.RelOp' a) where
prt i = \case
AbsExp.LTH _ -> prPrec i 0 (concatD [doc (showString "<")])
AbsExp.LE _ -> prPrec i 0 (concatD [doc (showString "<=")])
AbsExp.GTH _ -> prPrec i 0 (concatD [doc (showString ">")])
AbsExp.GE _ -> prPrec i 0 (concatD [doc (showString ">=")])
AbsExp.EQU _ -> prPrec i 0 (concatD [doc (showString "==")])
AbsExp.NE _ -> prPrec i 0 (concatD [doc (showString "!=")])