module Language.Perl.Internal.Types
(
Interpreter(..)
, SV(..)
, AV(..)
, CV(..)
, Callback
, IV
, NV
, Context(..)
, numContext
, asSVList
, svTail
, svEither
, mkSVList
, withSVArray
)
where
import Foreign
import Foreign.C.Types
import Language.Perl.Internal.Constants
{-# ANN module ("HLint: ignore Missing NOINLINE pragma" :: String) #-}
newtype Interpreter = Interpreter { unInterpreter :: Ptr Interpreter } deriving (Show, Eq)
newtype SV = SV { unSV :: Ptr SV } deriving (Show, Eq)
newtype AV = AV { unAV :: Ptr AV } deriving (Show, Eq)
newtype CV = CV { unCV :: Ptr CV } deriving (Show, Eq)
type Callback = Ptr SV -> CInt -> IO (Ptr SV)
asSVList :: Ptr SV -> IO [SV]
asSVList ptrs = do
let ptrs' :: Ptr (Ptr SV)
ptrs' = castPtr ptrs
map SV <$> peekArray0 nullPtr ptrs'
svTail :: Ptr a -> IO [SV]
svTail ptrs = do
let ptrs' :: Ptr (Ptr SV)
ptrs' = castPtr ptrs
ptrsTail = ptrs' `advancePtr` 1
map SV <$> peekArray0 nullPtr ptrsTail
svEither :: Ptr SV -> IO (Either [SV] [SV])
svEither ptrs = do
errList <- asSVList ptrs
if null errList
then Right <$> svTail ptrs
else return $ Left errList
mkSVList :: [SV] -> IO (Ptr SV)
mkSVList svs = do
arr <- newArray0 nullPtr (map unSV svs)
let arr' :: Ptr SV
arr' = castPtr arr
return arr'
withSVArray :: [SV] -> (Ptr SV -> IO b) -> IO b
withSVArray svs f = withArray0 nullPtr svs' (f . castPtr)
where
svs' = map unSV svs
data Context = VoidCtx | ScalarCtx | ListCtx
deriving (Eq, Show)
instance Enum Context where
fromEnum = numContext
toEnum n = case n of
G_VOID -> VoidCtx
G_SCALAR -> ScalarCtx
G_ARRAY -> ListCtx
_ -> error "not a context"
numContext :: (Eq p, Num p) => Context -> p
numContext VoidCtx = G_VOID
numContext ScalarCtx = G_SCALAR
numContext ListCtx = G_ARRAY