{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}
module PrintSRLRL where

-- pretty-printer generated by the BNF converter

import AbsSRLRL
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 :: Int -> [a] -> Doc
  prtList i = concatD . map (prt i)

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 Var where
  prt i e = case e of
    IVar id -> prPrec i 0 (concatD [prt 0 id])

instance Print Oplus where
  prt i e = case e of
    Plus -> prPrec i 0 (concatD [doc (showString "+")])
    Minus -> prPrec i 0 (concatD [doc (showString "-")])
    Index -> prPrec i 0 (concatD [doc (showString "^")])

instance Print Otime where
  prt i e = case e of
    Calc oplus -> prPrec i 0 (concatD [prt 0 oplus])
    Time -> prPrec i 0 (concatD [doc (showString "*")])
    Div -> prPrec i 0 (concatD [doc (showString "/")])
    Eq -> prPrec i 0 (concatD [doc (showString "=")])
    Less -> prPrec i 0 (concatD [doc (showString "<")])
    Greater -> prPrec i 0 (concatD [doc (showString ">")])
    Less_Eq -> prPrec i 0 (concatD [doc (showString "<=")])
    Greater_Eq -> prPrec i 0 (concatD [doc (showString ">=")])
    Not_Eq -> prPrec i 0 (concatD [doc (showString "!=")])

instance Print Const where
  prt i e = case e of
    IConst n -> prPrec i 0 (concatD [prt 0 n])

instance Print Exp where
  prt i e = case e of
    EConst const -> prPrec i 0 (concatD [prt 0 const])
    EVar id -> prPrec i 0 (concatD [prt 0 id])
    EVar_List var exp -> prPrec i 0 (concatD [prt 0 var, doc (showString "["), prt 0 exp, doc (showString "]")])
    EExp exp1 otime exp2 -> prPrec i 0 (concatD [prt 0 exp1, prt 0 otime, prt 0 exp2])
    ETop var -> prPrec i 0 (concatD [doc (showString "top"), prt 0 var])
    EEmpty var -> prPrec i 0 (concatD [doc (showString "empty"), prt 0 var])

instance Print Step where
  prt i e = case e of
    Oplus_Eq var oplus exp -> prPrec i 0 (concatD [prt 0 var, prt 0 oplus, doc (showString "="), prt 0 exp])
    OPlus_Eq_List var exp1 oplus exp2 -> prPrec i 0 (concatD [prt 0 var, doc (showString "["), prt 0 exp1, doc (showString "]"), prt 0 oplus, doc (showString "="), prt 0 exp2])
    Push var1 var2 -> prPrec i 0 (concatD [doc (showString "push"), prt 0 var1, prt 0 var2])
    Pop var1 var2 -> prPrec i 0 (concatD [doc (showString "pop"), prt 0 var1, prt 0 var2])
    Skip -> prPrec i 0 (concatD [doc (showString "skip")])

instance Print Blk where
  prt i e = case e of
    Sstep step -> prPrec i 0 (concatD [prt 0 step])
    Sif exp1 blk1 blk2 exp2 -> prPrec i 0 (concatD [doc (showString "if"), prt 0 exp1, doc (showString "then"), prt 0 blk1, doc (showString "else"), prt 0 blk2, doc (showString "fi"), prt 0 exp2])
    Sblk blk1 blk2 -> prPrec i 0 (concatD [prt 0 blk1, prt 0 blk2])
    Sfrom exp1 blk1 blk2 exp2 -> prPrec i 0 (concatD [doc (showString "from"), prt 0 exp1, doc (showString "do"), prt 0 blk1, doc (showString "loop"), prt 0 blk2, doc (showString "until"), prt 0 exp2])

instance Print SRL where
  prt i e = case e of
    ESRL blk -> prPrec i 0 (concatD [prt 0 blk])

instance Print Label where
  prt i e = case e of
    ELabel n -> prPrec i 0 (concatD [doc (showString "l"), prt 0 n])

instance Print Jump where
  prt i e = case e of
    Rgoto label -> prPrec i 0 (concatD [doc (showString "goto"), prt 0 label])
    Rif exp label1 label2 -> prPrec i 0 (concatD [doc (showString "if"), prt 0 exp, doc (showString "goto"), prt 0 label1, doc (showString "else"), prt 0 label2])
    Rexit -> prPrec i 0 (concatD [doc (showString "exit")])

instance Print From where
  prt i e = case e of
    Rfrom label -> prPrec i 0 (concatD [doc (showString "from"), prt 0 label])
    Rfi exp label1 label2 -> prPrec i 0 (concatD [doc (showString "fi"), prt 0 exp, doc (showString "from"), prt 0 label1, doc (showString "else"), prt 0 label2])
    Rentry -> prPrec i 0 (concatD [doc (showString "entry")])

instance Print Rlblk where
  prt i e = case e of
    Rblk1 label from jump -> prPrec i 0 (concatD [prt 0 label, doc (showString ":"), prt 0 from, prt 0 jump])
    Rblk2 label from step jump -> prPrec i 0 (concatD [prt 0 label, doc (showString ":"), prt 0 from, prt 0 step, prt 0 jump])

instance Print Rl where
  prt i e = case e of
    Rl1 -> prPrec i 0 (concatD [])
    Rl2 rlblk rl -> prPrec i 0 (concatD [prt 0 rlblk, doc (showString ";"), prt 0 rl])


