module Language.Bash.Cond
( CondExpr(..)
, UnaryOp(..)
, BinaryOp(..)
) where
import Text.PrettyPrint
import Language.Bash.Operator
import Language.Bash.Pretty
data CondExpr
= Unary UnaryOp String
| Binary String BinaryOp String
| Not CondExpr
| And CondExpr CondExpr
| Or CondExpr CondExpr
deriving (Eq, Read, Show)
instance Pretty CondExpr where
pretty = go (0 :: Int)
where
go _ (Unary op a) = pretty op <+> text a
go _ (Binary a op b) = text a <+> pretty op <+> text b
go _ (Not e) = "!" <+> go 2 e
go p (And e1 e2) = paren (p > 1) $ go 1 e1 <+> "&&" <+> go 1 e2
go p (Or e1 e2) = paren (p > 0) $ go 0 e1 <+> "||" <+> go 0 e2
paren False d = d
paren True d = "(" <+> d <+> ")"
data UnaryOp
= BlockFile
| CharacterFile
| Directory
| FileExists
| RegularFile
| SetGID
| Sticky
| NamedPipe
| Readable
| FileSize
| Terminal
| SetUID
| Writable
| Executable
| GroupOwned
| SymbolicLink
| Modified
| UserOwned
| Socket
| Optname
| Varname
| ZeroString
| NonzeroString
deriving (Eq, Ord, Read, Show, Enum, Bounded)
instance Operator UnaryOp where
operatorTable =
zip [minBound .. maxBound]
(map (\c -> ['-', c]) "bcdefgkprstuwxGLNOSovzn") ++
[ (FileExists , "-a")
, (SymbolicLink, "-h")
]
instance Pretty UnaryOp where
pretty = prettyOperator
data BinaryOp
= SameFile
| NewerThan
| OlderThan
| StrMatch
| StrEQ
| StrNE
| StrLT
| StrGT
| ArithEQ
| ArithNE
| ArithLT
| ArithLE
| ArithGT
| ArithGE
deriving (Eq, Ord, Read, Show, Enum, Bounded)
instance Operator BinaryOp where
operatorTable =
zip [minBound .. maxBound]
[ "-ef", "-nt", "-ot"
, "=~", "==", "!=", "<", ">"
, "-eq", "-ne", "-lt", "-le", "-gt", "-ge"
] ++
[ (StrEQ, "=") ]
instance Pretty BinaryOp where
pretty = prettyOperator