{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ <= 708
{-# LANGUAGE OverlappingInstances #-}
#endif
{-# LANGUAGE FlexibleInstances #-}
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}

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

module PrintSRL where

import qualified AbsSRL
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
    [";"]        -> showChar ';'
    ";"      :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 s =
    case (all isSpace t', null spc, null rest) of
      (True , _   , True ) -> []              -- remove trailing space
      (False, _   , True ) -> t'              -- remove trailing space
      (False, True, False) -> t' ++ ' ' : s   -- add space if none
      _                    -> t' ++ s
    where
      t'          = showString t []
      (spc, rest) = span isSpace 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 {-# OVERLAPPABLE #-} 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 AbsSRL.Ident where
  prt _ (AbsSRL.Ident i) = doc $ showString $ i

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

instance Print [AbsSRL.Blk] where
  prt = prtList

instance Print AbsSRL.Blk where
  prt i e = case e of
    AbsSRL.Bstep step -> prPrec i 0 (concatD [prt 0 step])
    AbsSRL.Bif exp1 blks1 blks2 exp2 -> prPrec i 0 (concatD [doc (showString "if"), prt 0 exp1, doc (showString "then"), prt 0 blks1, doc (showString "else"), prt 0 blks2, doc (showString "fi"), prt 0 exp2])
    AbsSRL.Bfrom exp1 blks1 blks2 exp2 -> prPrec i 0 (concatD [doc (showString "from"), prt 0 exp1, doc (showString "do"), prt 0 blks1, doc (showString "loop"), prt 0 blks2, doc (showString "until"), prt 0 exp2])
    AbsSRL.Bfor exp1 blks1 blks2 blks3 blks4 exp2 -> prPrec i 0 (concatD [doc (showString "for"), prt 0 exp1, doc (showString "ttimes"), prt 0 blks1, doc (showString "do"), prt 0 blks2, doc (showString "loop"), prt 0 blks3, doc (showString "ftimes"), prt 0 blks4, doc (showString "until"), prt 0 exp2])
  prtList _ [x] = concatD [prt 0 x]
  prtList _ (x:xs) = concatD [prt 0 x, prt 0 xs]

instance Print AbsSRL.Step where
  prt i e = case e of
    AbsSRL.SasnAdd id exp -> prPrec i 0 (concatD [prt 0 id, doc (showString "+="), prt 0 exp])
    AbsSRL.SasnMin id exp -> prPrec i 0 (concatD [prt 0 id, doc (showString "-="), prt 0 exp])
    AbsSRL.SasnXor id exp -> prPrec i 0 (concatD [prt 0 id, doc (showString "^="), prt 0 exp])
    AbsSRL.SasnArrAdd id exp1 exp2 -> prPrec i 0 (concatD [prt 0 id, doc (showString "["), prt 0 exp1, doc (showString "]"), doc (showString "+="), prt 0 exp2])
    AbsSRL.SasnArrMin id exp1 exp2 -> prPrec i 0 (concatD [prt 0 id, doc (showString "["), prt 0 exp1, doc (showString "]"), doc (showString "-="), prt 0 exp2])
    AbsSRL.SasnArrXor id exp1 exp2 -> prPrec i 0 (concatD [prt 0 id, doc (showString "["), prt 0 exp1, doc (showString "]"), doc (showString "^="), prt 0 exp2])
    AbsSRL.Sinc id -> prPrec i 0 (concatD [prt 0 id, doc (showString "++")])
    AbsSRL.Sdec id -> prPrec i 0 (concatD [prt 0 id, doc (showString "--")])
    AbsSRL.Spush id1 id2 -> prPrec i 0 (concatD [doc (showString "push"), prt 0 id1, prt 0 id2])
    AbsSRL.Spop id1 id2 -> prPrec i 0 (concatD [doc (showString "pop"), prt 0 id1, prt 0 id2])
    AbsSRL.Sskip -> prPrec i 0 (concatD [doc (showString "skip")])

instance Print AbsSRL.Exp where
  prt i e = case e of
    AbsSRL.Etop exp -> prPrec i 0 (concatD [doc (showString "top"), prt 2 exp])
    AbsSRL.Eempty exp -> prPrec i 2 (concatD [doc (showString "empty"), prt 3 exp])
    AbsSRL.Exor exp1 exp2 -> prPrec i 3 (concatD [prt 3 exp1, doc (showString "^"), prt 4 exp2])
    AbsSRL.Eeq exp1 exp2 -> prPrec i 4 (concatD [prt 4 exp1, doc (showString "="), prt 5 exp2])
    AbsSRL.Ene exp1 exp2 -> prPrec i 4 (concatD [prt 4 exp1, doc (showString "!="), prt 5 exp2])
    AbsSRL.Elt exp1 exp2 -> prPrec i 5 (concatD [prt 5 exp1, doc (showString "<"), prt 6 exp2])
    AbsSRL.Egt exp1 exp2 -> prPrec i 5 (concatD [prt 5 exp1, doc (showString ">"), prt 6 exp2])
    AbsSRL.Ele exp1 exp2 -> prPrec i 5 (concatD [prt 5 exp1, doc (showString ">="), prt 6 exp2])
    AbsSRL.Ege exp1 exp2 -> prPrec i 5 (concatD [prt 5 exp1, doc (showString "<="), prt 6 exp2])
    AbsSRL.Eadd exp1 exp2 -> prPrec i 6 (concatD [prt 6 exp1, doc (showString "+"), prt 7 exp2])
    AbsSRL.Emin exp1 exp2 -> prPrec i 6 (concatD [prt 6 exp1, doc (showString "-"), prt 7 exp2])
    AbsSRL.Emul exp1 exp2 -> prPrec i 7 (concatD [prt 7 exp1, doc (showString "*"), prt 8 exp2])
    AbsSRL.Ediv exp1 exp2 -> prPrec i 7 (concatD [prt 7 exp1, doc (showString "/"), prt 8 exp2])
    AbsSRL.Earr exp1 exp2 -> prPrec i 8 (concatD [prt 9 exp1, doc (showString "["), prt 0 exp2, doc (showString "]")])
    AbsSRL.Econ n -> prPrec i 9 (concatD [prt 0 n])
    AbsSRL.Evar id -> prPrec i 9 (concatD [prt 0 id])

