1 module Printsyntax where
3 -- pretty-printer generated by the BNF converter
8 -- the top-level printing method
9 printTree :: Print a => a -> String
10 printTree = render . prt 0
12 type Doc = [ShowS] -> [ShowS]
17 render :: Doc -> String
18 render d = rend 0 (map ($ "") $ d []) "" where
19 rend i ss = case ss of
20 "[" :ts -> showChar '[' . rend i ts
21 "(" :ts -> showChar '(' . rend i ts
22 "{" :ts -> showChar '{' . new (i+1) . rend (i+1) ts
23 "}" : ";":ts -> new (i-1) . space "}" . showChar ';' . new (i-1) . rend (i-1) ts
24 "}" :ts -> new (i-1) . showChar '}' . new (i-1) . rend (i-1) ts
25 ";" :ts -> showChar ';' . new i . rend i ts
26 t : "," :ts -> showString t . space "," . rend i ts
27 t : ")" :ts -> showString t . showChar ')' . rend i ts
28 t : "]" :ts -> showString t . showChar ']' . rend i ts
29 t :ts -> space t . rend i ts
31 new i = showChar '\n' . replicateS (2*i) (showChar ' ') . dropWhile isSpace
32 space t = showString t . (\s -> if null s then "" else (' ':s))
35 parenth ss = doc (showChar '(') . ss . doc (showChar ')')
37 concatS :: [ShowS] -> ShowS
38 concatS = foldr (.) id
40 concatD :: [Doc] -> Doc
41 concatD = foldr (.) id
43 replicateS :: Int -> ShowS -> ShowS
44 replicateS n f = concatS (replicate n f)
46 -- the printer class does the job
48 prt :: Int -> a -> Doc
50 prtList = concatD . map (prt 0)
52 instance Print a => Print [a] where
55 instance Print Char where
56 prt _ s = doc (showChar '\'' . mkEsc '\'' s . showChar '\'')
57 prtList s = doc (showChar '"' . concatS (map (mkEsc '"') s) . showChar '"')
59 mkEsc :: Char -> Char -> ShowS
61 _ | s == q -> showChar '\\' . showChar s
62 '\\'-> showString "\\\\"
63 '\n' -> showString "\\n"
64 '\t' -> showString "\\t"
67 prPrec :: Int -> Int -> Doc -> Doc
68 prPrec i j = if j<i then parenth else id
71 instance Print Integer where
72 prt _ x = doc (shows x)
75 instance Print Double where
76 prt _ x = doc (shows x)
79 instance Print Ident where
80 prt _ (Ident i) = doc (showString i)
84 instance Print Bool where
86 True -> prPrec i 0 (concatD [doc (showString "true")])
87 False -> prPrec i 0 (concatD [doc (showString "false")])
90 instance Print Type where
92 TInt -> prPrec i 0 (concatD [doc (showString "int")])
93 TBool -> prPrec i 0 (concatD [doc (showString "bool")])
96 instance Print Program where
98 Program funcstms -> prPrec i 0 (concatD [prt 0 funcstms])
101 instance Print Stm where
103 SExp exp -> prPrec i 0 (concatD [prt 0 exp , doc (showString ";")])
104 SBlock stms -> prPrec i 0 (concatD [doc (showString "{") , prt 0 stms , doc (showString "}")])
105 SDecl type' id exp -> prPrec i 0 (concatD [prt 0 type' , prt 0 id , doc (showString "=") , prt 0 exp , doc (showString ";")])
106 SDeclD type' id -> prPrec i 0 (concatD [prt 0 type' , prt 0 id , doc (showString ";")])
107 SWhile exp stm -> prPrec i 0 (concatD [doc (showString "while") , doc (showString "(") , prt 0 exp , doc (showString ")") , prt 0 stm])
108 SIf exp stm0 stm -> prPrec i 0 (concatD [doc (showString "if") , doc (showString "(") , prt 0 exp , doc (showString ")") , prt 0 stm0 , doc (showString "else") , prt 0 stm])
109 SPrint exp -> prPrec i 0 (concatD [doc (showString "print") , prt 0 exp , doc (showString ";")])
110 SReturn exp -> prPrec i 0 (concatD [doc (showString "return") , prt 0 exp , doc (showString ";")])
111 SNoop -> prPrec i 0 (concatD [])
113 prtList es = case es of
115 x:xs -> (concatD [prt 0 x , prt 0 xs])
117 instance Print Exp where
119 EAss id exp -> prPrec i 0 (concatD [prt 0 id , doc (showString "=") , prt 0 exp])
120 EVar id -> prPrec i 3 (concatD [prt 0 id])
121 EInt n -> prPrec i 3 (concatD [prt 0 n])
122 EBool bool -> prPrec i 3 (concatD [prt 0 bool])
123 ENeg exp -> prPrec i 3 (concatD [doc (showString "-") , prt 3 exp])
124 ENot exp -> prPrec i 3 (concatD [doc (showString "!") , prt 3 exp])
125 EReadI -> prPrec i 3 (concatD [doc (showString "readInt")])
126 EReadB -> prPrec i 3 (concatD [doc (showString "readBool")])
127 EFunc id exps -> prPrec i 3 (concatD [prt 0 id , doc (showString "(") , prt 0 exps , doc (showString ")")])
128 BiOpExp exp0 op exp -> prPrec i 0 (concatD [prt 0 exp0 , prt 0 op , prt 0 exp])
129 EPost id op -> prPrec i 0 (concatD [prt 0 id , prt 1 op])
131 prtList es = case es of
133 [x] -> (concatD [prt 0 x])
134 x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
136 instance Print Decl where
138 Decl type' id -> prPrec i 0 (concatD [prt 0 type' , prt 0 id])
140 prtList es = case es of
142 [x] -> (concatD [prt 0 x])
143 x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
145 instance Print Func where
147 Func type' id decls stms -> prPrec i 0 (concatD [prt 0 type' , prt 0 id , doc (showString "(") , prt 0 decls , doc (showString ")") , doc (showString "{") , prt 0 stms , doc (showString "}")])
149 prtList es = case es of
151 x:xs -> (concatD [prt 0 x , prt 0 xs])
153 instance Print FuncStm where
155 S stm -> prPrec i 0 (concatD [prt 0 stm])
156 F func -> prPrec i 0 (concatD [prt 0 func])
158 prtList es = case es of
160 x:xs -> (concatD [prt 0 x , prt 0 xs])
162 instance Print Op where
164 Lt -> prPrec i 0 (concatD [doc (showString "<")])
165 ELt -> prPrec i 0 (concatD [doc (showString "<=")])
166 Gt -> prPrec i 0 (concatD [doc (showString ">")])
167 EGt -> prPrec i 0 (concatD [doc (showString ">=")])
168 Eq -> prPrec i 0 (concatD [doc (showString "==")])
169 NEq -> prPrec i 0 (concatD [doc (showString "!=")])
170 Plus -> prPrec i 1 (concatD [doc (showString "+")])
171 Minus -> prPrec i 1 (concatD [doc (showString "-")])
172 Times -> prPrec i 2 (concatD [doc (showString "*")])
173 Div -> prPrec i 2 (concatD [doc (showString "/")])