+module Printsyntax where
+
+-- pretty-printer generated by the BNF converter
+
+import Abssyntax
+import Char
+
+-- 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 (map ($ "") $ d []) "" where
+ rend i ss = case ss of
+ "[" :ts -> showChar '[' . rend i ts
+ "(" :ts -> showChar '(' . rend i ts
+ "{" :ts -> showChar '{' . new (i+1) . rend (i+1) ts
+ "}" : ";":ts -> new (i-1) . space "}" . showChar ';' . new (i-1) . rend (i-1) ts
+ "}" :ts -> new (i-1) . showChar '}' . new (i-1) . rend (i-1) ts
+ ";" :ts -> showChar ';' . new i . rend i ts
+ t : "," :ts -> showString t . space "," . rend i ts
+ t : ")" :ts -> showString t . showChar ')' . rend i ts
+ t : "]" :ts -> showString t . showChar ']' . rend i ts
+ t :ts -> space t . rend i ts
+ _ -> id
+ new i = showChar '\n' . replicateS (2*i) (showChar ' ') . dropWhile isSpace
+ space t = showString t . (\s -> if null s then "" else (' ':s))
+
+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
+ prtList :: [a] -> Doc
+ prtList = concatD . map (prt 0)
+
+instance Print a => Print [a] where
+ prt _ = prtList
+
+instance Print Char where
+ prt _ s = doc (showChar '\'' . mkEsc '\'' s . showChar '\'')
+ prtList s = doc (showChar '"' . concatS (map (mkEsc '"') s) . showChar '"')
+
+mkEsc :: Char -> Char -> ShowS
+mkEsc q s = case s of
+ _ | s == q -> showChar '\\' . showChar s
+ '\\'-> showString "\\\\"
+ '\n' -> showString "\\n"
+ '\t' -> showString "\\t"
+ _ -> 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 Ident where
+ prt _ (Ident i) = doc (showString i)
+
+
+
+instance Print Bool where
+ prt i e = case e of
+ True -> prPrec i 0 (concatD [doc (showString "true")])
+ False -> prPrec i 0 (concatD [doc (showString "false")])
+
+
+instance Print Stm where
+ prt i e = case e of
+ SDecl type' id exp -> prPrec i 0 (concatD [prt 0 type' , prt 0 id , doc (showString "=") , prt 0 exp , doc (showString ";")])
+ SExp exp -> prPrec i 0 (concatD [prt 0 exp , doc (showString ";")])
+ SBlock stms -> prPrec i 0 (concatD [doc (showString "{") , prt 0 stms , doc (showString "}")])
+ 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])
+ SWhile exp stm -> prPrec i 0 (concatD [doc (showString "while") , doc (showString "(") , prt 0 exp , doc (showString ")") , prt 0 stm])
+ SPrint exp -> prPrec i 0 (concatD [doc (showString "print") , prt 0 exp , doc (showString ";")])
+ SNoop -> prPrec i 0 (concatD [])
+
+ prtList es = case es of
+ [] -> (concatD [])
+ x:xs -> (concatD [prt 0 x , prt 0 xs])
+
+instance Print Exp where
+ prt i e = case e of
+ BExp exp0 bop exp -> prPrec i 0 (concatD [prt 1 exp0 , prt 0 bop , prt 1 exp])
+ EVar id -> prPrec i 3 (concatD [prt 0 id])
+ EAss id exp -> prPrec i 3 (concatD [prt 0 id , doc (showString "=") , prt 0 exp])
+ EInt n -> prPrec i 3 (concatD [prt 0 n])
+ ENeg exp -> prPrec i 3 (concatD [doc (showString "-") , prt 3 exp])
+ EBool bool -> prPrec i 3 (concatD [prt 0 bool])
+ EReadI -> prPrec i 3 (concatD [doc (showString "readInt")])
+ EReadB -> prPrec i 3 (concatD [doc (showString "readBool")])
+ ExpT type' exp -> prPrec i 0 (concatD [prt 0 type' , prt 0 exp])
+ EDefault -> prPrec i 0 (concatD [])
+ OpExp exp0 op exp -> prPrec i 0 (concatD [prt 0 exp0 , prt 0 op , prt 0 exp])
+ EPost id op -> prPrec i 0 (concatD [prt 0 id , prt 1 op])
+
+
+instance Print Stms where
+ prt i e = case e of
+ Program stms -> prPrec i 0 (concatD [prt 0 stms])
+
+
+instance Print BOp where
+ prt i e = case e of
+ Lt -> prPrec i 0 (concatD [doc (showString "<")])
+ ELt -> prPrec i 0 (concatD [doc (showString "<=")])
+ Gt -> prPrec i 0 (concatD [doc (showString ">")])
+ EGt -> prPrec i 0 (concatD [doc (showString ">=")])
+ Eq -> prPrec i 0 (concatD [doc (showString "==")])
+ NEq -> prPrec i 0 (concatD [doc (showString "!=")])
+
+
+instance Print Op where
+ prt i e = case e of
+ Plus -> prPrec i 1 (concatD [doc (showString "+")])
+ Minus -> prPrec i 1 (concatD [doc (showString "-")])
+ Times -> prPrec i 2 (concatD [doc (showString "*")])
+ Div -> prPrec i 2 (concatD [doc (showString "/")])
+
+
+instance Print Type where
+ prt i e = case e of
+ TInt -> prPrec i 0 (concatD [doc (showString "int")])
+ TBool -> prPrec i 0 (concatD [doc (showString "bool")])
+ NoType -> prPrec i 0 (concatD [])
+
+
+