{-# LANGUAGE FlexibleInstances, OverlappingInstances #-}
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}

-- | Pretty-printer for PrintRl.
--   Generated by the BNF converter.

module PrintRl where

import AbsRl
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@(p:_) | closingOrPunctuation p -> showString t . 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)

  closingOrPunctuation :: String -> Bool
  closingOrPunctuation [c] = c `elem` closerOrPunct
  closingOrPunctuation _   = False

  closerOrPunct :: String
  closerOrPunct = ")],;"

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 Rl where
  prt i e = case e of
    RLBLK1 -> prPrec i 0 (concatD [])
    RLBLK rlblk rl -> prPrec i 0 (concatD [prt 0 rlblk, doc (showString ";"), prt 0 rl])

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 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 Label where
  prt i e = case e of
    Label n -> prPrec i 0 (concatD [doc (showString "l"), prt 0 n])

instance Print Step where
  prt i e = case e of
    Plus_Eq var oplus exp -> prPrec i 0 (concatD [prt 0 var, prt 0 oplus, doc (showString "="), prt 0 exp])
    Plus_In 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 Exp where
  prt i e = case e of
    EConst n -> prPrec i 0 (concatD [prt 0 n])
    EVar id -> prPrec i 0 (concatD [prt 0 id])
    EIn var exp -> prPrec i 0 (concatD [prt 0 var, doc (showString "["), prt 0 exp, doc (showString "]")])
    ETime exp1 otime exp2 -> prPrec i 0 (concatD [prt 0 exp1, prt 0 otime, prt 0 exp2])
    ETop var1 var2 -> prPrec i 0 (concatD [doc (showString "top"), prt 0 var1, prt 0 var2])
    EEmpty var1 var2 -> prPrec i 0 (concatD [doc (showString "empty"), prt 0 var1, prt 0 var2])

instance Print Otime where
  prt i e = case e of
    OPlus oplus -> prPrec i 0 (concatD [prt 0 oplus])
    Time -> prPrec i 0 (concatD [doc (showString "*")])
    Div -> prPrec i 0 (concatD [doc (showString "/")])
    Equal -> 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 Oplus where
  prt i e = case e of
    Plus -> prPrec i 0 (concatD [doc (showString "+")])
    Minus -> prPrec i 0 (concatD [doc (showString "-")])
    Caret -> prPrec i 0 (concatD [doc (showString "^")])

instance Print Var where
  prt i e = case e of
    Var id -> prPrec i 0 (concatD [prt 0 id])

