{-# OPTIONS -fno-warn-incomplete-patterns #-} module PrintLambdaMini where -- pretty-printer generated by the BNF converter import AbsLambdaMini import Data.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 prPrec i 0 (concatD [prt 0 funcs]) instance Print Func where prt i e = case e of Func fident exp -> prPrec i 0 (concatD [doc (showString "def") , prt 0 fident , doc (showString "=") , prt 0 exp]) prtList es = case es of [] -> (concatD []) x:xs -> (concatD [prt 0 x , doc (showString ";") , prt 0 xs]) instance Print Exp where prt i e = case e of EVar fident -> prPrec i 9 (concatD [prt 0 fident]) EInt n -> prPrec i 9 (concatD [prt 0 n]) ETrue -> prPrec i 9 (concatD [doc (showString "true")]) EFalse -> prPrec i 9 (concatD [doc (showString "false")]) EApp exp0 exp -> prPrec i 8 (concatD [prt 8 exp0 , prt 9 exp]) ENeg exp -> prPrec i 7 (concatD [doc (showString "-") , prt 7 exp]) EMul exp0 exp -> prPrec i 6 (concatD [prt 6 exp0 , doc (showString "*") , prt 7 exp]) EDiv exp0 exp -> prPrec i 6 (concatD [prt 6 exp0 , doc (showString "/") , prt 7 exp]) EAdd exp0 exp -> prPrec i 5 (concatD [prt 5 exp0 , doc (showString "+") , prt 6 exp]) ESub exp0 exp -> prPrec i 5 (concatD [prt 5 exp0 , doc (showString "-") , prt 6 exp]) ELt exp0 exp -> prPrec i 4 (concatD [prt 4 exp0 , doc (showString "<") , prt 5 exp]) EGt exp0 exp -> prPrec i 4 (concatD [prt 4 exp0 , doc (showString ">") , prt 5 exp]) ELEq exp0 exp -> prPrec i 4 (concatD [prt 4 exp0 , doc (showString "<=") , prt 5 exp]) EGEq exp0 exp -> prPrec i 4 (concatD [prt 4 exp0 , doc (showString ">=") , prt 5 exp]) EEq exp0 exp -> prPrec i 3 (concatD [prt 3 exp0 , doc (showString "==") , prt 4 exp]) ENEq exp0 exp -> prPrec i 3 (concatD [prt 3 exp0 , doc (showString "!=") , prt 4 exp]) EIf exp0 exp1 exp -> prPrec i 2 (concatD [doc (showString "if") , prt 3 exp0 , doc (showString "then") , prt 3 exp1 , doc (showString "else") , prt 3 exp]) ELambda fident type' exp -> prPrec i 0 (concatD [doc (showString "\\") , prt 0 fident , doc (showString ":") , prt 0 type' , doc (showString ".") , prt 0 exp]) prtList es = case es of [] -> (concatD []) [x] -> (concatD [prt 0 x]) x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs]) instance Print Type where prt i e = case e of TBool -> prPrec i 1 (concatD [doc (showString "Bool")]) TInt -> prPrec i 1 (concatD [doc (showString "Int")]) TFun type'0 type' -> prPrec i 0 (concatD [prt 1 type'0 , doc (showString "->") , prt 0 type'])