{-# OPTIONS -fno-warn-incomplete-patterns #-} module Core.Print where -- pretty-printer generated by the BNF converter import Core.Abs 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 (concatD [prt 0 x]) x:xs -> (concatD [prt 0 x , prt 0 xs]) instance Print CaseTk where prt _ (CaseTk (_,i)) = doc (showString i) instance Print DataTk where prt _ (DataTk (_,i)) = doc (showString i) instance Print Exp where prt i e = case e of ELam patt exp -> prPrec i 1 (concatD [doc (showString "\\") , prt 1 patt , doc (showString ".") , prt 1 exp]) ESet -> prPrec i 3 (concatD [doc (showString "U")]) EPi patt exp0 exp -> prPrec i 1 (concatD [doc (showString "Pi") , prt 1 patt , doc (showString ":") , prt 1 exp0 , doc (showString ".") , prt 1 exp]) ESig patt exp0 exp -> prPrec i 1 (concatD [doc (showString "Sig") , prt 1 patt , doc (showString ":") , prt 1 exp0 , doc (showString ".") , prt 1 exp]) EOne -> prPrec i 3 (concatD [doc (showString "1")]) Eunit -> prPrec i 3 (concatD [doc (showString "0")]) EPair exp0 exp -> prPrec i 0 (concatD [prt 1 exp0 , doc (showString ",") , prt 0 exp]) ECon id exp -> prPrec i 2 (concatD [doc (showString "$") , prt 0 id , prt 3 exp]) EData datatk summands -> prPrec i 1 (concatD [prt 0 datatk , doc (showString "(") , prt 0 summands , doc (showString ")")]) ECase casetk branchs -> prPrec i 1 (concatD [prt 0 casetk , doc (showString "(") , prt 0 branchs , doc (showString ")")]) EFst exp -> prPrec i 3 (concatD [prt 3 exp , doc (showString ".1")]) ESnd exp -> prPrec i 3 (concatD [prt 3 exp , doc (showString ".2")]) EApp exp0 exp -> prPrec i 2 (concatD [prt 2 exp0 , prt 3 exp]) EVar id -> prPrec i 3 (concatD [prt 0 id]) EVoid -> prPrec i 3 (concatD [doc (showString "Void")]) EDec decl exp -> prPrec i 1 (concatD [prt 0 decl , doc (showString ";") , prt 1 exp]) EPN -> prPrec i 3 (concatD [doc (showString "PN")]) instance Print Decl where prt i e = case e of Def patt exp0 exp -> prPrec i 0 (concatD [doc (showString "let") , prt 1 patt , doc (showString ":") , prt 1 exp0 , doc (showString "=") , prt 1 exp]) Drec patt exp0 exp -> prPrec i 0 (concatD [doc (showString "letrec") , prt 1 patt , doc (showString ":") , prt 1 exp0 , doc (showString "=") , prt 1 exp]) instance Print Patt where prt i e = case e of PPair patt0 patt -> prPrec i 0 (concatD [prt 1 patt0 , doc (showString ",") , prt 0 patt]) Punit -> prPrec i 1 (concatD [doc (showString "_")]) PVar id -> prPrec i 1 (concatD [prt 0 id]) instance Print Summand where prt i e = case e of Summand id exp -> prPrec i 0 (concatD [prt 0 id , prt 3 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 Branch where prt i e = case e of Branch id exp -> prPrec i 0 (concatD [prt 0 id , doc (showString "->") , prt 1 exp]) prtList es = case es of [] -> (concatD []) [x] -> (concatD [prt 0 x]) x:xs -> (concatD [prt 0 x , doc (showString "|") , prt 0 xs])