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 Stm where
92 SDecl type' id exp -> prPrec i 0 (concatD [prt 0 type' , prt 0 id , doc (showString "=") , prt 0 exp , doc (showString ";")])
93 SExp exp -> prPrec i 0 (concatD [prt 0 exp , doc (showString ";")])
94 SBlock stms -> prPrec i 0 (concatD [doc (showString "{") , prt 0 stms , doc (showString "}")])
95 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])
96 SWhile exp stm -> prPrec i 0 (concatD [doc (showString "while") , doc (showString "(") , prt 0 exp , doc (showString ")") , prt 0 stm])
97 SPrint exp -> prPrec i 0 (concatD [doc (showString "print") , prt 0 exp , doc (showString ";")])
98 SNoop -> prPrec i 0 (concatD [])
100 prtList es = case es of
102 x:xs -> (concatD [prt 0 x , prt 0 xs])
104 instance Print Exp where
106 EAss id exp -> prPrec i 0 (concatD [prt 0 id , doc (showString "=") , prt 0 exp])
107 EVar id -> prPrec i 3 (concatD [prt 0 id])
108 EInt n -> prPrec i 3 (concatD [prt 0 n])
109 ENeg exp -> prPrec i 3 (concatD [doc (showString "-") , prt 3 exp])
110 ENot exp -> prPrec i 3 (concatD [doc (showString "!") , prt 3 exp])
111 EBool bool -> prPrec i 3 (concatD [prt 0 bool])
112 EReadI -> prPrec i 3 (concatD [doc (showString "readInt")])
113 EReadB -> prPrec i 3 (concatD [doc (showString "readBool")])
114 ExpT type' exp -> prPrec i 0 (concatD [prt 0 type' , prt 0 exp])
115 EDefault -> prPrec i 0 (concatD [])
116 BiOpExp exp0 op exp -> prPrec i 0 (concatD [prt 0 exp0 , prt 0 op , prt 0 exp])
117 EPost id op -> prPrec i 0 (concatD [prt 0 id , prt 1 op])
120 instance Print Stms where
122 Program stms -> prPrec i 0 (concatD [prt 0 stms])
125 instance Print Op where
127 Lt -> prPrec i 0 (concatD [doc (showString "<")])
128 ELt -> prPrec i 0 (concatD [doc (showString "<=")])
129 Gt -> prPrec i 0 (concatD [doc (showString ">")])
130 EGt -> prPrec i 0 (concatD [doc (showString ">=")])
131 Eq -> prPrec i 0 (concatD [doc (showString "==")])
132 NEq -> prPrec i 0 (concatD [doc (showString "!=")])
133 Plus -> prPrec i 1 (concatD [doc (showString "+")])
134 Minus -> prPrec i 1 (concatD [doc (showString "-")])
135 Times -> prPrec i 2 (concatD [doc (showString "*")])
136 Div -> prPrec i 2 (concatD [doc (showString "/")])
139 instance Print Type where
141 TInt -> prPrec i 0 (concatD [doc (showString "int")])
142 TBool -> prPrec i 0 (concatD [doc (showString "bool")])
143 NoType -> prPrec i 0 (concatD [])