{-
-- EPITECH PROJECT, 2023
-- GLaDOS
-- File description:
-- SExpr
-}

module SExpr (SExpr(..), getSymbol, getInteger, getList, printTree) where

-- | S-Expression
data SExpr = Value Int
            | Symbol String
            | List [SExpr]
    deriving(Int -> SExpr -> ShowS
[SExpr] -> ShowS
SExpr -> String
(Int -> SExpr -> ShowS)
-> (SExpr -> String) -> ([SExpr] -> ShowS) -> Show SExpr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SExpr -> ShowS
showsPrec :: Int -> SExpr -> ShowS
$cshow :: SExpr -> String
show :: SExpr -> String
$cshowList :: [SExpr] -> ShowS
showList :: [SExpr] -> ShowS
Show, SExpr -> SExpr -> Bool
(SExpr -> SExpr -> Bool) -> (SExpr -> SExpr -> Bool) -> Eq SExpr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SExpr -> SExpr -> Bool
== :: SExpr -> SExpr -> Bool
$c/= :: SExpr -> SExpr -> Bool
/= :: SExpr -> SExpr -> Bool
Eq)

-- instance Show SExpr where
--     show (Value i) = show i
--     show (Symbol s) = show s
--     show (List l) = filter (/= '\"')
--         ("( " ++ foldr (++) "" ((++ " ") <$> (show <$> l)) ++ ")")

-- | Get the 'Symbol' contained in this expression
getSymbol :: SExpr -> Maybe String
getSymbol :: SExpr -> Maybe String
getSymbol (Value Int
_) = Maybe String
forall a. Maybe a
Nothing
getSymbol (List [SExpr]
_) = Maybe String
forall a. Maybe a
Nothing
getSymbol (Symbol String
s) = String -> Maybe String
forall a. a -> Maybe a
Just String
s

-- | Get the 'Integer' contained in this expression
getInteger :: SExpr -> Maybe Int
getInteger :: SExpr -> Maybe Int
getInteger (Value Int
int) = Int -> Maybe Int
forall a. a -> Maybe a
Just Int
int
getInteger (List [SExpr]
_) = Maybe Int
forall a. Maybe a
Nothing
getInteger (Symbol String
_) = Maybe Int
forall a. Maybe a
Nothing

-- | Get the 'List' contained in this expression
getList :: SExpr -> Maybe [SExpr]
getList :: SExpr -> Maybe [SExpr]
getList (Value Int
_) = Maybe [SExpr]
forall a. Maybe a
Nothing
getList (List [SExpr]
l) = [SExpr] -> Maybe [SExpr]
forall a. a -> Maybe a
Just [SExpr]
l
getList (Symbol String
_) = Maybe [SExpr]
forall a. Maybe a
Nothing

-- | Return a string representation of the S-Expression
printTree :: SExpr -> Maybe String
printTree :: SExpr -> Maybe String
printTree SExpr
s = String -> Maybe String
forall a. a -> Maybe a
Just (SExpr -> String
forall a. Show a => a -> String
show SExpr
s)