{-# LANGUAGE CPP #-}
{-# Language ExistentialQuantification #-}
module Language.Scheme.Primitives (
car
, cdr
, cons
, eq
, equal
, makeList
, listCopy
, buildVector
, vectorLength
, vectorRef
, vectorCopy
, vectorToList
, listToVector
, makeVector
, makeByteVector
, byteVector
, byteVectorLength
, byteVectorRef
, byteVectorCopy
, byteVectorAppend
, byteVectorUtf2Str
, byteVectorStr2Utf
, hashTblExists
, hashTblSize
, hashTbl2List
, hashTblKeys
, hashTblValues
, hashTblCopy
, hashTblMake
, wrapHashTbl
, wrapLeadObj
, buildString
, makeString
, doMakeString
, stringLength
, stringRef
, substring
, stringCIEquals
, stringCIBoolBinop
, stringAppend
, stringToNumber
, stringToList
, listToString
, stringToVector
, vectorToString
, stringCopy
, symbol2String
, string2Symbol
, charCIBoolBinop
, charPredicate
, charUpper
, charLower
, charDigitValue
, char2Int
, int2Char
, isHashTbl
, isChar
, isString
, isBoolean
, isBooleanEq
, isSymbolEq
, isDottedList
, isProcedure
, isList
, isVector
, isRecord
, isByteVector
, isNull
, isEOFObject
, isSymbol
, Unpacker ()
, unpackEquals
, boolBinop
, unaryOp
, unaryOp'
, strBoolBinop
, charBoolBinop
, boolBoolBinop
, unpackStr
, unpackBool
, makePort
, makeBufferPort
, openInputString
, openOutputString
, getOutputString
, openInputByteVector
, openOutputByteVector
, getOutputByteVector
, closePort
, flushOutputPort
, currentOutputPort
, currentInputPort
, isTextPort
, isBinaryPort
, isOutputPort
, isInputPort
, isInputPortOpen
, isOutputPortOpen
, isCharReady
, readProc
, readCharProc
, readByteVector
, readString
, writeProc
, writeCharProc
, writeByteVector
, writeString
, readContents
, load
, readAll
, fileExists
, deleteFile
, eofObject
, gensym
, _gensym
, currentTimestamp
, system
, getEnvVars
) where
import Language.Scheme.Numerical
import Language.Scheme.Parser
import Language.Scheme.Types
import Language.Scheme.Variables
import Control.Monad.Except
import qualified Data.ByteString as BS
import qualified Data.ByteString.UTF8 as BSU
import Data.Char hiding (isSymbol)
import Data.Array
import qualified Data.Knob as DK
import qualified Data.Map
import qualified Data.Time.Clock.POSIX
import Data.Unique
import Data.Word
import System.Directory (doesFileExist, removeFile)
import qualified System.Environment as SE
import System.Exit (ExitCode(..))
import System.IO
import System.IO.Error
import qualified System.Process
#if __GLASGOW_HASKELL__ < 702
try' = try
#else
try' :: IO a -> IO (Either IOError a)
try' :: IO a -> IO (Either IOError a)
try' = IO a -> IO (Either IOError a)
forall a. IO a -> IO (Either IOError a)
tryIOError
#endif
makePort
:: (FilePath -> IOMode -> IO Handle)
-> IOMode
-> [LispVal]
-> IOThrowsError LispVal
makePort :: (FilePath -> IOMode -> IO Handle)
-> IOMode -> [LispVal] -> IOThrowsError LispVal
makePort FilePath -> IOMode -> IO Handle
openFnc IOMode
mode [String FilePath
filename] = do
Handle
h <- IO Handle -> ExceptT LispError IO Handle
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Handle -> ExceptT LispError IO Handle)
-> IO Handle -> ExceptT LispError IO Handle
forall a b. (a -> b) -> a -> b
$ FilePath -> IOMode -> IO Handle
openFnc FilePath
filename IOMode
mode
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Handle -> Maybe Knob -> LispVal
Port Handle
h Maybe Knob
forall a. Maybe a
Nothing
makePort FilePath -> IOMode -> IO Handle
fnc IOMode
mode [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (FilePath -> IOMode -> IO Handle)
-> IOMode -> [LispVal] -> IOThrowsError LispVal
makePort FilePath -> IOMode -> IO Handle
fnc IOMode
mode
makePort FilePath -> IOMode -> IO Handle
_ IOMode
_ [] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) []
makePort FilePath -> IOMode -> IO Handle
_ IOMode
_ args :: [LispVal]
args@(LispVal
_ : [LispVal]
_) = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
makeBufferPort :: Maybe LispVal -> IOThrowsError LispVal
makeBufferPort :: Maybe LispVal -> IOThrowsError LispVal
makeBufferPort Maybe LispVal
buf = do
let mode :: IOMode
mode = case Maybe LispVal
buf of
Maybe LispVal
Nothing -> IOMode
WriteMode
Maybe LispVal
_ -> IOMode
ReadMode
ByteString
bs <- case Maybe LispVal
buf of
Just (String FilePath
s)-> ByteString -> ExceptT LispError IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> ExceptT LispError IO ByteString)
-> ByteString -> ExceptT LispError IO ByteString
forall a b. (a -> b) -> a -> b
$ FilePath -> ByteString
BSU.fromString FilePath
s
Just (ByteVector ByteString
bv)-> ByteString -> ExceptT LispError IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
bv
Just LispVal
err -> LispError -> ExceptT LispError IO ByteString
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ExceptT LispError IO ByteString)
-> LispError -> ExceptT LispError IO ByteString
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string or bytevector" LispVal
err
Maybe LispVal
Nothing -> ByteString -> ExceptT LispError IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> ExceptT LispError IO ByteString)
-> ByteString -> ExceptT LispError IO ByteString
forall a b. (a -> b) -> a -> b
$ [Word8] -> ByteString
BS.pack []
Knob
k <- ByteString -> ExceptT LispError IO Knob
forall (m :: * -> *). MonadIO m => ByteString -> m Knob
DK.newKnob ByteString
bs
Handle
h <- IO Handle -> ExceptT LispError IO Handle
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Handle -> ExceptT LispError IO Handle)
-> IO Handle -> ExceptT LispError IO Handle
forall a b. (a -> b) -> a -> b
$ Knob -> FilePath -> IOMode -> IO Handle
forall (m :: * -> *).
MonadIO m =>
Knob -> FilePath -> IOMode -> m Handle
DK.newFileHandle Knob
k FilePath
"temp.buf" IOMode
mode
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Handle -> Maybe Knob -> LispVal
Port Handle
h (Knob -> Maybe Knob
forall a. a -> Maybe a
Just Knob
k)
getBufferFromPort :: LispVal -> IOThrowsError BSU.ByteString
getBufferFromPort :: LispVal -> ExceptT LispError IO ByteString
getBufferFromPort (Port Handle
h (Just Knob
k)) = do
()
_ <- IO () -> ExceptT LispError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT LispError IO ())
-> IO () -> ExceptT LispError IO ()
forall a b. (a -> b) -> a -> b
$ Handle -> IO ()
hFlush Handle
h
Knob -> ExceptT LispError IO ByteString
forall (m :: * -> *). MonadIO m => Knob -> m ByteString
DK.getContents Knob
k
getBufferFromPort LispVal
args = do
LispError -> ExceptT LispError IO ByteString
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ExceptT LispError IO ByteString)
-> LispError -> ExceptT LispError IO ByteString
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"output-port" LispVal
args
openInputString :: [LispVal] -> IOThrowsError LispVal
openInputString :: [LispVal] -> IOThrowsError LispVal
openInputString [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
openInputString
openInputString [buf :: LispVal
buf@(String FilePath
_)] = Maybe LispVal -> IOThrowsError LispVal
makeBufferPort (LispVal -> Maybe LispVal
forall a. a -> Maybe a
Just LispVal
buf)
openInputString [LispVal]
args = if [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
args Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1
then LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"(string)" (LispVal -> LispError) -> LispVal -> LispError
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
args
else LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
openOutputString :: [LispVal] -> IOThrowsError LispVal
openOutputString :: [LispVal] -> IOThrowsError LispVal
openOutputString [LispVal]
_ = Maybe LispVal -> IOThrowsError LispVal
makeBufferPort Maybe LispVal
forall a. Maybe a
Nothing
openInputByteVector :: [LispVal] -> IOThrowsError LispVal
openInputByteVector :: [LispVal] -> IOThrowsError LispVal
openInputByteVector [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
openInputByteVector
openInputByteVector [buf :: LispVal
buf@(ByteVector ByteString
_)] = Maybe LispVal -> IOThrowsError LispVal
makeBufferPort (LispVal -> Maybe LispVal
forall a. a -> Maybe a
Just LispVal
buf)
openInputByteVector [LispVal]
args = if [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
args Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1
then LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"(bytevector)" (LispVal -> LispError) -> LispVal -> LispError
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
args
else LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
openOutputByteVector :: [LispVal] -> IOThrowsError LispVal
openOutputByteVector :: [LispVal] -> IOThrowsError LispVal
openOutputByteVector [LispVal]
_ = Maybe LispVal -> IOThrowsError LispVal
makeBufferPort Maybe LispVal
forall a. Maybe a
Nothing
getOutputString :: [LispVal] -> IOThrowsError LispVal
getOutputString :: [LispVal] -> IOThrowsError LispVal
getOutputString [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
getOutputString
getOutputString [p :: LispVal
p@(Port Handle
port Maybe Knob
_)] = do
Bool
o <- IO Bool -> ExceptT LispError IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT LispError IO Bool)
-> IO Bool -> ExceptT LispError IO Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
hIsOpen Handle
port
if Bool
o then do
ByteString
bytes <- LispVal -> ExceptT LispError IO ByteString
getBufferFromPort LispVal
p
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String (FilePath -> LispVal) -> FilePath -> LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> FilePath
BSU.toString ByteString
bytes
else LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String FilePath
""
getOutputString [LispVal]
args = do
LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"output-port" (LispVal -> LispError) -> LispVal -> LispError
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
args
getOutputByteVector :: [LispVal] -> IOThrowsError LispVal
getOutputByteVector :: [LispVal] -> IOThrowsError LispVal
getOutputByteVector [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
getOutputByteVector
getOutputByteVector [p :: LispVal
p@(Port Handle
port Maybe Knob
_)] = do
Bool
o <- IO Bool -> ExceptT LispError IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT LispError IO Bool)
-> IO Bool -> ExceptT LispError IO Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
hIsOpen Handle
port
if Bool
o then do ByteString
bytes <- LispVal -> ExceptT LispError IO ByteString
getBufferFromPort LispVal
p
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> LispVal
ByteVector ByteString
bytes
else LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> LispVal
ByteVector (ByteString -> LispVal) -> ByteString -> LispVal
forall a b. (a -> b) -> a -> b
$ [Word8] -> ByteString
BS.pack []
getOutputByteVector [LispVal]
args = do
LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"output-port" (LispVal -> LispError) -> LispVal -> LispError
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
args
closePort :: [LispVal] -> IOThrowsError LispVal
closePort :: [LispVal] -> IOThrowsError LispVal
closePort [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
closePort
closePort [Port Handle
port Maybe Knob
_] = IO LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO LispVal -> IOThrowsError LispVal)
-> IO LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Handle -> IO ()
hClose Handle
port IO () -> IO LispVal -> IO LispVal
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (LispVal -> IO LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IO LispVal) -> LispVal -> IO LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True)
closePort [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
currentInputPort :: [LispVal] -> IOThrowsError LispVal
currentInputPort :: [LispVal] -> IOThrowsError LispVal
currentInputPort [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Handle -> Maybe Knob -> LispVal
Port Handle
stdin Maybe Knob
forall a. Maybe a
Nothing
currentOutputPort :: [LispVal] -> IOThrowsError LispVal
currentOutputPort :: [LispVal] -> IOThrowsError LispVal
currentOutputPort [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Handle -> Maybe Knob -> LispVal
Port Handle
stdout Maybe Knob
forall a. Maybe a
Nothing
flushOutputPort :: [LispVal] -> IOThrowsError LispVal
flushOutputPort :: [LispVal] -> IOThrowsError LispVal
flushOutputPort [] = IO LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO LispVal -> IOThrowsError LispVal)
-> IO LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Handle -> IO ()
hFlush Handle
stdout IO () -> IO LispVal -> IO LispVal
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (LispVal -> IO LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IO LispVal) -> LispVal -> IO LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True)
flushOutputPort [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
flushOutputPort
flushOutputPort [p :: LispVal
p@(Port Handle
_ Maybe Knob
_)] =
LispVal
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
withOpenPort LispVal
p ((Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal)
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ \Handle
port -> IO LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO LispVal -> IOThrowsError LispVal)
-> IO LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Handle -> IO ()
hFlush Handle
port IO () -> IO LispVal -> IO LispVal
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (LispVal -> IO LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IO LispVal) -> LispVal -> IO LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True)
flushOutputPort [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isTextPort :: [LispVal] -> IOThrowsError LispVal
isTextPort :: [LispVal] -> IOThrowsError LispVal
isTextPort [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
isTextPort
isTextPort [Port Handle
port Maybe Knob
_] = do
Bool
val <- IO Bool -> ExceptT LispError IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT LispError IO Bool)
-> IO Bool -> ExceptT LispError IO Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
isTextPort' Handle
port
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
val
isTextPort [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isBinaryPort :: [LispVal] -> IOThrowsError LispVal
isBinaryPort :: [LispVal] -> IOThrowsError LispVal
isBinaryPort [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
isBinaryPort
isBinaryPort [Port Handle
port Maybe Knob
_] = do
Bool
val <- IO Bool -> ExceptT LispError IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT LispError IO Bool)
-> IO Bool -> ExceptT LispError IO Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
isTextPort' Handle
port
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool (Bool -> LispVal) -> Bool -> LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not Bool
val
isBinaryPort [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isTextPort' :: Handle -> IO Bool
isTextPort' :: Handle -> IO Bool
isTextPort' Handle
port = do
Maybe TextEncoding
textEncoding <- Handle -> IO (Maybe TextEncoding)
hGetEncoding Handle
port
case Maybe TextEncoding
textEncoding of
Maybe TextEncoding
Nothing -> Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
Maybe TextEncoding
_ -> Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
isInputPortOpen :: [LispVal] -> IOThrowsError LispVal
isInputPortOpen :: [LispVal] -> IOThrowsError LispVal
isInputPortOpen [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
isInputPortOpen
isInputPortOpen [p :: LispVal
p@(Port Handle
_ Maybe Knob
_)] = do
LispVal
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
withOpenPort LispVal
p ((Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal)
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ \Handle
port -> do
Bool
r <- IO Bool -> ExceptT LispError IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT LispError IO Bool)
-> IO Bool -> ExceptT LispError IO Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
hIsReadable Handle
port
Bool
o <- IO Bool -> ExceptT LispError IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT LispError IO Bool)
-> IO Bool -> ExceptT LispError IO Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
hIsOpen Handle
port
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool (Bool -> LispVal) -> Bool -> LispVal
forall a b. (a -> b) -> a -> b
$ Bool
r Bool -> Bool -> Bool
&& Bool
o
isInputPortOpen [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
withOpenPort :: LispVal -> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
withOpenPort :: LispVal
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
withOpenPort p :: LispVal
p@(Pointer {}) Handle -> IOThrowsError LispVal
proc = do
LispVal
obj <- LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p
LispVal
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
withOpenPort LispVal
obj Handle -> IOThrowsError LispVal
proc
withOpenPort (Port Handle
port Maybe Knob
_) Handle -> IOThrowsError LispVal
proc = do
Bool
o <- IO Bool -> ExceptT LispError IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT LispError IO Bool)
-> IO Bool -> ExceptT LispError IO Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
hIsOpen Handle
port
if Bool
o then Handle -> IOThrowsError LispVal
proc Handle
port
else LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
withOpenPort LispVal
_ Handle -> IOThrowsError LispVal
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isOutputPortOpen :: [LispVal] -> IOThrowsError LispVal
isOutputPortOpen :: [LispVal] -> IOThrowsError LispVal
isOutputPortOpen [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
isOutputPortOpen
isOutputPortOpen [p :: LispVal
p@(Port Handle
_ Maybe Knob
_)] = do
LispVal
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
withOpenPort LispVal
p ((Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal)
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ \Handle
port -> do
Bool
w <- IO Bool -> ExceptT LispError IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT LispError IO Bool)
-> IO Bool -> ExceptT LispError IO Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
hIsWritable Handle
port
Bool
o <- IO Bool -> ExceptT LispError IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT LispError IO Bool)
-> IO Bool -> ExceptT LispError IO Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
hIsOpen Handle
port
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool (Bool -> LispVal) -> Bool -> LispVal
forall a b. (a -> b) -> a -> b
$ Bool
w Bool -> Bool -> Bool
&& Bool
o
isOutputPortOpen [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isInputPort :: [LispVal] -> IOThrowsError LispVal
isInputPort :: [LispVal] -> IOThrowsError LispVal
isInputPort [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
isInputPort
isInputPort [p :: LispVal
p@(Port Handle
_ Maybe Knob
_)] =
LispVal
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
withOpenPort LispVal
p ((Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal)
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ \Handle
port -> (Bool -> LispVal)
-> ExceptT LispError IO Bool -> IOThrowsError LispVal
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Bool -> LispVal
Bool (ExceptT LispError IO Bool -> IOThrowsError LispVal)
-> ExceptT LispError IO Bool -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ IO Bool -> ExceptT LispError IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT LispError IO Bool)
-> IO Bool -> ExceptT LispError IO Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
hIsReadable Handle
port
isInputPort [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isOutputPort :: [LispVal] -> IOThrowsError LispVal
isOutputPort :: [LispVal] -> IOThrowsError LispVal
isOutputPort [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
isOutputPort
isOutputPort [p :: LispVal
p@(Port Handle
_ Maybe Knob
_)] =
LispVal
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
withOpenPort LispVal
p ((Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal)
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ \Handle
port -> (Bool -> LispVal)
-> ExceptT LispError IO Bool -> IOThrowsError LispVal
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Bool -> LispVal
Bool (ExceptT LispError IO Bool -> IOThrowsError LispVal)
-> ExceptT LispError IO Bool -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ IO Bool -> ExceptT LispError IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT LispError IO Bool)
-> IO Bool -> ExceptT LispError IO Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
hIsWritable Handle
port
isOutputPort [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isCharReady :: [LispVal] -> IOThrowsError LispVal
isCharReady :: [LispVal] -> IOThrowsError LispVal
isCharReady [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
isCharReady
isCharReady [Port Handle
port Maybe Knob
_] = do
Either IOError Bool
result <- IO (Either IOError Bool)
-> ExceptT LispError IO (Either IOError Bool)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either IOError Bool)
-> ExceptT LispError IO (Either IOError Bool))
-> IO (Either IOError Bool)
-> ExceptT LispError IO (Either IOError Bool)
forall a b. (a -> b) -> a -> b
$ IO Bool -> IO (Either IOError Bool)
forall a. IO a -> IO (Either IOError a)
try' (IO Bool -> IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> IO Bool) -> IO Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
hReady Handle
port)
case Either IOError Bool
result of
Left IOError
e -> if IOError -> Bool
isEOFError IOError
e
then LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
else LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispError
Default FilePath
"I/O error reading from port"
Right Bool
_ -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isCharReady [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
readProc :: Bool -> [LispVal] -> IOThrowsError LispVal
readProc :: Bool -> [LispVal] -> IOThrowsError LispVal
readProc Bool
mode [] = Bool -> [LispVal] -> IOThrowsError LispVal
readProc Bool
mode [Handle -> Maybe Knob -> LispVal
Port Handle
stdin Maybe Knob
forall a. Maybe a
Nothing]
readProc Bool
mode [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> [LispVal] -> IOThrowsError LispVal
readProc Bool
mode
readProc Bool
mode [Port Handle
port Maybe Knob
_] = do
Either IOError FilePath
input <- IO (Either IOError FilePath)
-> ExceptT LispError IO (Either IOError FilePath)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either IOError FilePath)
-> ExceptT LispError IO (Either IOError FilePath))
-> IO (Either IOError FilePath)
-> ExceptT LispError IO (Either IOError FilePath)
forall a b. (a -> b) -> a -> b
$ IO FilePath -> IO (Either IOError FilePath)
forall a. IO a -> IO (Either IOError a)
try' (IO FilePath -> IO FilePath
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FilePath -> IO FilePath) -> IO FilePath -> IO FilePath
forall a b. (a -> b) -> a -> b
$ Handle -> IO FilePath
hGetLine Handle
port)
case Either IOError FilePath
input of
Left IOError
e -> if IOError -> Bool
isEOFError IOError
e
then LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ LispVal
EOF
else LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispError
Default FilePath
"I/O error reading from port"
Right FilePath
inpStr -> do
ThrowsError LispVal -> IOThrowsError LispVal
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError LispVal -> IOThrowsError LispVal)
-> ThrowsError LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$
case Bool
mode of
Bool
True -> FilePath -> ThrowsError LispVal
readExpr FilePath
inpStr
Bool
_ -> LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String FilePath
inpStr
readProc Bool
_ [LispVal]
args = if [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
args Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1
then LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"port" (LispVal -> LispError) -> LispVal -> LispError
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
args
else LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
readCharProc :: (Handle -> IO Char) -> [LispVal] -> IOThrowsError LispVal
readCharProc :: (Handle -> IO Char) -> [LispVal] -> IOThrowsError LispVal
readCharProc Handle -> IO Char
func [p :: LispVal
p@(Pointer {})] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Handle -> IO Char) -> [LispVal] -> IOThrowsError LispVal
readCharProc Handle -> IO Char
func
readCharProc Handle -> IO Char
func [] = (Handle -> IO Char) -> [LispVal] -> IOThrowsError LispVal
readCharProc Handle -> IO Char
func [Handle -> Maybe Knob -> LispVal
Port Handle
stdin Maybe Knob
forall a. Maybe a
Nothing]
readCharProc Handle -> IO Char
func [p :: LispVal
p@(Port Handle
_ Maybe Knob
_)] = do
LispVal
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
withOpenPort LispVal
p ((Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal)
-> (Handle -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ \Handle
port -> do
IO () -> ExceptT LispError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT LispError IO ())
-> IO () -> ExceptT LispError IO ()
forall a b. (a -> b) -> a -> b
$ Handle -> BufferMode -> IO ()
hSetBuffering Handle
port BufferMode
NoBuffering
Either IOError Char
input <- IO (Either IOError Char)
-> ExceptT LispError IO (Either IOError Char)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either IOError Char)
-> ExceptT LispError IO (Either IOError Char))
-> IO (Either IOError Char)
-> ExceptT LispError IO (Either IOError Char)
forall a b. (a -> b) -> a -> b
$ IO Char -> IO (Either IOError Char)
forall a. IO a -> IO (Either IOError a)
try' (IO Char -> IO Char
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Char -> IO Char) -> IO Char -> IO Char
forall a b. (a -> b) -> a -> b
$ Handle -> IO Char
func Handle
port)
IO () -> ExceptT LispError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT LispError IO ())
-> IO () -> ExceptT LispError IO ()
forall a b. (a -> b) -> a -> b
$ Handle -> BufferMode -> IO ()
hSetBuffering Handle
port BufferMode
LineBuffering
case Either IOError Char
input of
Left IOError
e -> if IOError -> Bool
isEOFError IOError
e
then LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ LispVal
EOF
else LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispError
Default FilePath
"I/O error reading from port"
Right Char
inpChr -> do
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Char -> LispVal
Char Char
inpChr
readCharProc Handle -> IO Char
_ [LispVal]
args = if [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
args Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1
then LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"port" (LispVal -> LispError) -> LispVal -> LispError
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
args
else LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
readByteVector :: [LispVal] -> IOThrowsError LispVal
readByteVector :: [LispVal] -> IOThrowsError LispVal
readByteVector [LispVal]
args = [LispVal] -> (ByteString -> LispVal) -> IOThrowsError LispVal
readBuffer [LispVal]
args ByteString -> LispVal
ByteVector
readString :: [LispVal] -> IOThrowsError LispVal
readString :: [LispVal] -> IOThrowsError LispVal
readString [LispVal]
args = [LispVal] -> (ByteString -> LispVal) -> IOThrowsError LispVal
readBuffer [LispVal]
args (FilePath -> LispVal
String (FilePath -> LispVal)
-> (ByteString -> FilePath) -> ByteString -> LispVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> FilePath
BSU.toString)
readBuffer :: [LispVal] -> (BSU.ByteString -> LispVal) -> IOThrowsError LispVal
readBuffer :: [LispVal] -> (ByteString -> LispVal) -> IOThrowsError LispVal
readBuffer [Number Integer
n, Port Handle
port Maybe Knob
_] ByteString -> LispVal
rvfnc = do
Either IOError ByteString
input <- IO (Either IOError ByteString)
-> ExceptT LispError IO (Either IOError ByteString)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either IOError ByteString)
-> ExceptT LispError IO (Either IOError ByteString))
-> IO (Either IOError ByteString)
-> ExceptT LispError IO (Either IOError ByteString)
forall a b. (a -> b) -> a -> b
$ IO ByteString -> IO (Either IOError ByteString)
forall a. IO a -> IO (Either IOError a)
try' (IO ByteString -> IO ByteString
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ByteString -> IO ByteString) -> IO ByteString -> IO ByteString
forall a b. (a -> b) -> a -> b
$ Handle -> Int -> IO ByteString
BS.hGet Handle
port (Int -> IO ByteString) -> Int -> IO ByteString
forall a b. (a -> b) -> a -> b
$ Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
n)
case Either IOError ByteString
input of
Left IOError
e -> if IOError -> Bool
isEOFError IOError
e
then LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ LispVal
EOF
else LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispError
Default FilePath
"I/O error reading from port"
Right ByteString
inBytes -> do
if ByteString -> Bool
BS.null ByteString
inBytes
then LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ LispVal
EOF
else LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> LispVal
rvfnc ByteString
inBytes
readBuffer [LispVal]
args ByteString -> LispVal
_ = if [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
args Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2
then LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"(k port)" (LispVal -> LispError) -> LispVal -> LispError
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
args
else LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
args
writeProc :: (Handle -> LispVal -> IO a)
-> [LispVal] -> ExceptT LispError IO LispVal
writeProc :: (Handle -> LispVal -> IO a) -> [LispVal] -> IOThrowsError LispVal
writeProc Handle -> LispVal -> IO a
func [LispVal
obj] = do
LispVal
dobj <- LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
obj
(Handle -> LispVal -> IO a) -> [LispVal] -> IOThrowsError LispVal
forall a.
(Handle -> LispVal -> IO a) -> [LispVal] -> IOThrowsError LispVal
writeProc Handle -> LispVal -> IO a
func [LispVal
dobj, Handle -> Maybe Knob -> LispVal
Port Handle
stdout Maybe Knob
forall a. Maybe a
Nothing]
writeProc Handle -> LispVal -> IO a
func [LispVal
obj, Port Handle
port Maybe Knob
_] = do
LispVal
dobj <- LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
obj
Either IOError a
output <- IO (Either IOError a) -> ExceptT LispError IO (Either IOError a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either IOError a) -> ExceptT LispError IO (Either IOError a))
-> IO (Either IOError a) -> ExceptT LispError IO (Either IOError a)
forall a b. (a -> b) -> a -> b
$ IO a -> IO (Either IOError a)
forall a. IO a -> IO (Either IOError a)
try' (IO a -> IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> IO a) -> IO a -> IO a
forall a b. (a -> b) -> a -> b
$ Handle -> LispVal -> IO a
func Handle
port LispVal
dobj)
case Either IOError a
output of
Left IOError
_ -> LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispError
Default FilePath
"I/O error writing to port"
Right a
_ -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
Nil FilePath
""
writeProc Handle -> LispVal -> IO a
_ [LispVal]
other = if [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
other Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2
then LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"(value port)" (LispVal -> LispError) -> LispVal -> LispError
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
other
else LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
other
writeCharProc :: [LispVal] -> IOThrowsError LispVal
writeCharProc :: [LispVal] -> IOThrowsError LispVal
writeCharProc [LispVal
obj] = [LispVal] -> IOThrowsError LispVal
writeCharProc [LispVal
obj, Handle -> Maybe Knob -> LispVal
Port Handle
stdout Maybe Knob
forall a. Maybe a
Nothing]
writeCharProc [obj :: LispVal
obj@(Char Char
_), Port Handle
port Maybe Knob
_] = do
Either IOError ()
output <- IO (Either IOError ()) -> ExceptT LispError IO (Either IOError ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either IOError ())
-> ExceptT LispError IO (Either IOError ()))
-> IO (Either IOError ())
-> ExceptT LispError IO (Either IOError ())
forall a b. (a -> b) -> a -> b
$ IO () -> IO (Either IOError ())
forall a. IO a -> IO (Either IOError a)
try' (IO () -> IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ (Handle -> FilePath -> IO ()
hPutStr Handle
port (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ LispVal -> FilePath
forall a. Show a => a -> FilePath
show LispVal
obj))
case Either IOError ()
output of
Left IOError
_ -> LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispError
Default FilePath
"I/O error writing to port"
Right ()
_ -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
Nil FilePath
""
writeCharProc [LispVal]
other = if [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
other Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2
then LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"(character port)" (LispVal -> LispError) -> LispVal -> LispError
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
other
else LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
other
writeByteVector :: [LispVal] -> IOThrowsError LispVal
writeByteVector :: [LispVal] -> IOThrowsError LispVal
writeByteVector [LispVal]
args = [LispVal]
-> (LispVal -> ExceptT LispError IO ByteString)
-> IOThrowsError LispVal
writeBuffer [LispVal]
args LispVal -> ExceptT LispError IO ByteString
bv2b
where
bv2b :: LispVal -> ExceptT LispError IO ByteString
bv2b LispVal
obj = do
ByteVector ByteString
bs <- LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
obj
ByteString -> ExceptT LispError IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
bs
writeString :: [LispVal] -> IOThrowsError LispVal
writeString :: [LispVal] -> IOThrowsError LispVal
writeString [LispVal]
args = [LispVal]
-> (LispVal -> ExceptT LispError IO ByteString)
-> IOThrowsError LispVal
writeBuffer [LispVal]
args LispVal -> ExceptT LispError IO ByteString
str2b
where
str2b :: LispVal -> ExceptT LispError IO ByteString
str2b LispVal
obj = do
String FilePath
str <- LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
obj
ByteString -> ExceptT LispError IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> ExceptT LispError IO ByteString)
-> ByteString -> ExceptT LispError IO ByteString
forall a b. (a -> b) -> a -> b
$ FilePath -> ByteString
BSU.fromString FilePath
str
writeBuffer :: [LispVal] -> (LispVal -> IOThrowsError BSU.ByteString) -> IOThrowsError LispVal
writeBuffer :: [LispVal]
-> (LispVal -> ExceptT LispError IO ByteString)
-> IOThrowsError LispVal
writeBuffer [LispVal
obj, Port Handle
port Maybe Knob
_] LispVal -> ExceptT LispError IO ByteString
getBS = do
ByteString
bs <- LispVal -> ExceptT LispError IO ByteString
getBS LispVal
obj
Either IOError ()
output <- IO (Either IOError ()) -> ExceptT LispError IO (Either IOError ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either IOError ())
-> ExceptT LispError IO (Either IOError ()))
-> IO (Either IOError ())
-> ExceptT LispError IO (Either IOError ())
forall a b. (a -> b) -> a -> b
$ IO () -> IO (Either IOError ())
forall a. IO a -> IO (Either IOError a)
try' (IO () -> IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle -> ByteString -> IO ()
BS.hPut Handle
port ByteString
bs)
case Either IOError ()
output of
Left IOError
_ -> LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispError
Default FilePath
"I/O error writing to port"
Right ()
_ -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
Nil FilePath
""
writeBuffer [LispVal]
other LispVal -> ExceptT LispError IO ByteString
_ =
if [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
other Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2
then LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"(bytevector port)" (LispVal -> LispError) -> LispVal -> LispError
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
other
else LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
other
fileExists :: [LispVal] -> IOThrowsError LispVal
fileExists :: [LispVal] -> IOThrowsError LispVal
fileExists [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
fileExists
fileExists [String FilePath
filename] = do
Bool
exists <- IO Bool -> ExceptT LispError IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT LispError IO Bool)
-> IO Bool -> ExceptT LispError IO Bool
forall a b. (a -> b) -> a -> b
$ FilePath -> IO Bool
doesFileExist FilePath
filename
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
exists
fileExists [] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) []
fileExists args :: [LispVal]
args@(LispVal
_ : [LispVal]
_) = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
deleteFile :: [LispVal] -> IOThrowsError LispVal
deleteFile :: [LispVal] -> IOThrowsError LispVal
deleteFile [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
deleteFile
deleteFile [String FilePath
filename] = do
Either IOError ()
output <- IO (Either IOError ()) -> ExceptT LispError IO (Either IOError ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either IOError ())
-> ExceptT LispError IO (Either IOError ()))
-> IO (Either IOError ())
-> ExceptT LispError IO (Either IOError ())
forall a b. (a -> b) -> a -> b
$ IO () -> IO (Either IOError ())
forall a. IO a -> IO (Either IOError a)
try' (IO () -> IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> IO ()
removeFile FilePath
filename)
case Either IOError ()
output of
Left IOError
_ -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
Right ()
_ -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
deleteFile [] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) []
deleteFile args :: [LispVal]
args@(LispVal
_ : [LispVal]
_) = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
readContents :: [LispVal] -> IOThrowsError LispVal
readContents :: [LispVal] -> IOThrowsError LispVal
readContents [String FilePath
filename] = (FilePath -> LispVal)
-> ExceptT LispError IO FilePath -> IOThrowsError LispVal
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM FilePath -> LispVal
String (ExceptT LispError IO FilePath -> IOThrowsError LispVal)
-> ExceptT LispError IO FilePath -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ IO FilePath -> ExceptT LispError IO FilePath
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FilePath -> ExceptT LispError IO FilePath)
-> IO FilePath -> ExceptT LispError IO FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FilePath
readFile FilePath
filename
readContents [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
readContents
readContents [] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) []
readContents args :: [LispVal]
args@(LispVal
_ : [LispVal]
_) = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
load :: String -> IOThrowsError [LispVal]
load :: FilePath -> ExceptT LispError IO [LispVal]
load FilePath
filename = do
Bool
result <- IO Bool -> ExceptT LispError IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ExceptT LispError IO Bool)
-> IO Bool -> ExceptT LispError IO Bool
forall a b. (a -> b) -> a -> b
$ FilePath -> IO Bool
doesFileExist FilePath
filename
if Bool
result
then do
FilePath
f <- IO FilePath -> ExceptT LispError IO FilePath
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FilePath -> ExceptT LispError IO FilePath)
-> IO FilePath -> ExceptT LispError IO FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FilePath
readFile FilePath
filename
case FilePath -> [FilePath]
lines FilePath
f of
((Char
'#':Char
'!':Char
'/' : FilePath
_) : [FilePath]
ls) -> ThrowsError [LispVal] -> ExceptT LispError IO [LispVal]
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError [LispVal] -> ExceptT LispError IO [LispVal])
-> (FilePath -> ThrowsError [LispVal])
-> FilePath
-> ExceptT LispError IO [LispVal]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ThrowsError [LispVal]
readExprList (FilePath -> ExceptT LispError IO [LispVal])
-> FilePath -> ExceptT LispError IO [LispVal]
forall a b. (a -> b) -> a -> b
$ [FilePath] -> FilePath
unlines [FilePath]
ls
((Char
'#':Char
'!':Char
' ':Char
'/' : FilePath
_) : [FilePath]
ls) -> ThrowsError [LispVal] -> ExceptT LispError IO [LispVal]
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError [LispVal] -> ExceptT LispError IO [LispVal])
-> (FilePath -> ThrowsError [LispVal])
-> FilePath
-> ExceptT LispError IO [LispVal]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ThrowsError [LispVal]
readExprList (FilePath -> ExceptT LispError IO [LispVal])
-> FilePath -> ExceptT LispError IO [LispVal]
forall a b. (a -> b) -> a -> b
$ [FilePath] -> FilePath
unlines [FilePath]
ls
[FilePath]
_ -> (ThrowsError [LispVal] -> ExceptT LispError IO [LispVal]
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError [LispVal] -> ExceptT LispError IO [LispVal])
-> (FilePath -> ThrowsError [LispVal])
-> FilePath
-> ExceptT LispError IO [LispVal]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ThrowsError [LispVal]
readExprList) FilePath
f
else LispError -> ExceptT LispError IO [LispVal]
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ExceptT LispError IO [LispVal])
-> LispError -> ExceptT LispError IO [LispVal]
forall a b. (a -> b) -> a -> b
$ FilePath -> LispError
Default (FilePath -> LispError) -> FilePath -> LispError
forall a b. (a -> b) -> a -> b
$ FilePath
"File does not exist: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
filename
readAll :: [LispVal] -> IOThrowsError LispVal
readAll :: [LispVal] -> IOThrowsError LispVal
readAll [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
readAll
readAll [String FilePath
filename] = ([LispVal] -> LispVal)
-> ExceptT LispError IO [LispVal] -> IOThrowsError LispVal
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [LispVal] -> LispVal
List (ExceptT LispError IO [LispVal] -> IOThrowsError LispVal)
-> ExceptT LispError IO [LispVal] -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> ExceptT LispError IO [LispVal]
load FilePath
filename
readAll [] = do
FilePath
input <- IO FilePath -> ExceptT LispError IO FilePath
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FilePath -> ExceptT LispError IO FilePath)
-> IO FilePath -> ExceptT LispError IO FilePath
forall a b. (a -> b) -> a -> b
$ IO FilePath
getContents
[LispVal]
lisp <- (ThrowsError [LispVal] -> ExceptT LispError IO [LispVal]
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError [LispVal] -> ExceptT LispError IO [LispVal])
-> (FilePath -> ThrowsError [LispVal])
-> FilePath
-> ExceptT LispError IO [LispVal]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ThrowsError [LispVal]
readExprList) FilePath
input
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
lisp
readAll args :: [LispVal]
args@(LispVal
_ : [LispVal]
_) = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
_gensym :: String -> IOThrowsError LispVal
_gensym :: FilePath -> IOThrowsError LispVal
_gensym FilePath
prefix = do
Unique
u <- IO Unique -> ExceptT LispError IO Unique
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Unique -> ExceptT LispError IO Unique)
-> IO Unique -> ExceptT LispError IO Unique
forall a b. (a -> b) -> a -> b
$ IO Unique
newUnique
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
Atom (FilePath -> LispVal) -> FilePath -> LispVal
forall a b. (a -> b) -> a -> b
$ FilePath
prefix FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ (LispVal -> FilePath
forall a. Show a => a -> FilePath
show (LispVal -> FilePath) -> LispVal -> FilePath
forall a b. (a -> b) -> a -> b
$ Integer -> LispVal
Number (Integer -> LispVal) -> Integer -> LispVal
forall a b. (a -> b) -> a -> b
$ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ Unique -> Int
hashUnique Unique
u)
gensym :: [LispVal] -> IOThrowsError LispVal
gensym :: [LispVal] -> IOThrowsError LispVal
gensym [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
gensym
gensym [String FilePath
prefix] = FilePath -> IOThrowsError LispVal
_gensym FilePath
prefix
gensym [] = FilePath -> IOThrowsError LispVal
_gensym FilePath
" g"
gensym args :: [LispVal]
args@(LispVal
_ : [LispVal]
_) = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
car :: [LispVal] -> IOThrowsError LispVal
car :: [LispVal] -> IOThrowsError LispVal
car [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
car
car [List (LispVal
x : [LispVal]
_)] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return LispVal
x
car [DottedList (LispVal
x : [LispVal]
_) LispVal
_] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return LispVal
x
car [LispVal
badArg] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"pair" LispVal
badArg
car [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
cdr :: [LispVal] -> IOThrowsError LispVal
cdr :: [LispVal] -> IOThrowsError LispVal
cdr [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
cdr
cdr [List (LispVal
_ : [LispVal]
xs)] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
xs
cdr [DottedList [LispVal
_] LispVal
x] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return LispVal
x
cdr [DottedList (LispVal
_ : [LispVal]
xs) LispVal
x] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal -> LispVal
DottedList [LispVal]
xs LispVal
x
cdr [LispVal
badArg] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"pair" LispVal
badArg
cdr [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
cons :: [LispVal] -> IOThrowsError LispVal
cons :: [LispVal] -> IOThrowsError LispVal
cons [LispVal
x, p :: LispVal
p@(Pointer FilePath
_ Env
_)] = do
LispVal
y <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
[LispVal] -> IOThrowsError LispVal
cons [LispVal
x, LispVal
y]
cons [LispVal
x1, List []] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal
x1]
cons [LispVal
x, List [LispVal]
xs] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ LispVal
x LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
xs
cons [LispVal
x, DottedList [LispVal]
xs LispVal
xlast] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal -> LispVal
DottedList (LispVal
x LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
xs) LispVal
xlast
cons [LispVal
x1, LispVal
x2] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal -> LispVal
DottedList [LispVal
x1] LispVal
x2
cons [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
badArgList
makeList :: [LispVal] -> ThrowsError LispVal
makeList :: [LispVal] -> ThrowsError LispVal
makeList [(Number Integer
n)] = [LispVal] -> ThrowsError LispVal
makeList [Integer -> LispVal
Number Integer
n, [LispVal] -> LispVal
List []]
makeList [(Number Integer
n), LispVal
a] = do
let l :: [LispVal]
l = Int -> LispVal -> [LispVal]
forall a. Int -> a -> [a]
replicate (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
n) LispVal
a
LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
l
makeList [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"integer" LispVal
badType
makeList [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
listCopy :: [LispVal] -> IOThrowsError LispVal
listCopy :: [LispVal] -> IOThrowsError LispVal
listCopy [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = do
LispVal
l <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
[LispVal] -> IOThrowsError LispVal
listCopy [LispVal
l]
listCopy [(List [LispVal]
ls)] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
ls
listCopy [LispVal
badType] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return LispVal
badType
listCopy [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
vectorCopy :: [LispVal] -> IOThrowsError LispVal
vectorCopy :: [LispVal] -> IOThrowsError LispVal
vectorCopy (p :: LispVal
p@(Pointer FilePath
_ Env
_) : [LispVal]
args) = do
LispVal
v <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
[LispVal] -> IOThrowsError LispVal
vectorCopy (LispVal
v LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
args)
vectorCopy [Vector Array Int LispVal
vs] = do
let l :: [LispVal]
l = Array Int LispVal -> [LispVal]
forall i e. Array i e -> [e]
elems Array Int LispVal
vs
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Array Int LispVal -> LispVal
Vector (Array Int LispVal -> LispVal) -> Array Int LispVal -> LispVal
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> [LispVal] -> Array Int LispVal
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
0, [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [LispVal]
l
vectorCopy [Vector Array Int LispVal
vs, Number Integer
start] = do
let l :: [LispVal]
l = Int -> [LispVal] -> [LispVal]
forall a. Int -> [a] -> [a]
drop (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
start) ([LispVal] -> [LispVal]) -> [LispVal] -> [LispVal]
forall a b. (a -> b) -> a -> b
$
Array Int LispVal -> [LispVal]
forall i e. Array i e -> [e]
elems Array Int LispVal
vs
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Array Int LispVal -> LispVal
Vector (Array Int LispVal -> LispVal) -> Array Int LispVal -> LispVal
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> [LispVal] -> Array Int LispVal
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
0, [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [LispVal]
l
vectorCopy [Vector Array Int LispVal
vs, Number Integer
start, Number Integer
end] = do
let l :: [LispVal]
l = Int -> [LispVal] -> [LispVal]
forall a. Int -> [a] -> [a]
take (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Integer
end Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
start) ([LispVal] -> [LispVal]) -> [LispVal] -> [LispVal]
forall a b. (a -> b) -> a -> b
$
Int -> [LispVal] -> [LispVal]
forall a. Int -> [a] -> [a]
drop (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
start) ([LispVal] -> [LispVal]) -> [LispVal] -> [LispVal]
forall a b. (a -> b) -> a -> b
$
Array Int LispVal -> [LispVal]
forall i e. Array i e -> [e]
elems Array Int LispVal
vs
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Array Int LispVal -> LispVal
Vector (Array Int LispVal -> LispVal) -> Array Int LispVal -> LispVal
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> [LispVal] -> Array Int LispVal
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
0, [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [LispVal]
l
vectorCopy [LispVal
badType] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return LispVal
badType
vectorCopy [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
eq :: [LispVal] -> IOThrowsError LispVal
eq :: [LispVal] -> IOThrowsError LispVal
eq [(Pointer FilePath
pA Env
envA), (Pointer FilePath
pB Env
envB)] = do
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool (Bool -> LispVal) -> Bool -> LispVal
forall a b. (a -> b) -> a -> b
$ (FilePath
pA FilePath -> FilePath -> Bool
forall a. Eq a => a -> a -> Bool
== FilePath
pB) Bool -> Bool -> Bool
&& ((Env -> IORef (Map FilePath (IORef LispVal))
bindings Env
envA) IORef (Map FilePath (IORef LispVal))
-> IORef (Map FilePath (IORef LispVal)) -> Bool
forall a. Eq a => a -> a -> Bool
== (Env -> IORef (Map FilePath (IORef LispVal))
bindings Env
envB))
eq [LispVal]
args = ([LispVal] -> ThrowsError LispVal)
-> [LispVal] -> IOThrowsError LispVal
recDerefToFnc [LispVal] -> ThrowsError LispVal
eqv [LispVal]
args
equal :: [LispVal] -> ThrowsError LispVal
equal :: [LispVal] -> ThrowsError LispVal
equal [(Vector Array Int LispVal
arg1), (Vector Array Int LispVal
arg2)] = ([LispVal] -> ThrowsError LispVal)
-> [LispVal] -> ThrowsError LispVal
eqvList [LispVal] -> ThrowsError LispVal
equal [[LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ (Array Int LispVal -> [LispVal]
forall i e. Array i e -> [e]
elems Array Int LispVal
arg1), [LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ (Array Int LispVal -> [LispVal]
forall i e. Array i e -> [e]
elems Array Int LispVal
arg2)]
equal [l1 :: LispVal
l1@(List [LispVal]
_), l2 :: LispVal
l2@(List [LispVal]
_)] = ([LispVal] -> ThrowsError LispVal)
-> [LispVal] -> ThrowsError LispVal
eqvList [LispVal] -> ThrowsError LispVal
equal [LispVal
l1, LispVal
l2]
equal [(DottedList [LispVal]
xs LispVal
x), (DottedList [LispVal]
ys LispVal
y)] = [LispVal] -> ThrowsError LispVal
equal [[LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal]
xs [LispVal] -> [LispVal] -> [LispVal]
forall a. [a] -> [a] -> [a]
++ [LispVal
x], [LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal]
ys [LispVal] -> [LispVal] -> [LispVal]
forall a. [a] -> [a] -> [a]
++ [LispVal
y]]
equal [LispVal
arg1, LispVal
arg2] = do
Bool
primitiveEquals <- ([Bool] -> Bool)
-> Either LispError [Bool] -> Either LispError Bool
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or (Either LispError [Bool] -> Either LispError Bool)
-> Either LispError [Bool] -> Either LispError Bool
forall a b. (a -> b) -> a -> b
$ (Unpacker -> Either LispError Bool)
-> [Unpacker] -> Either LispError [Bool]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (LispVal -> LispVal -> Unpacker -> Either LispError Bool
unpackEquals LispVal
arg1 LispVal
arg2)
[(LispVal -> ThrowsError Integer) -> Unpacker
forall a. Eq a => (LispVal -> ThrowsError a) -> Unpacker
AnyUnpacker LispVal -> ThrowsError Integer
unpackNum, (LispVal -> ThrowsError FilePath) -> Unpacker
forall a. Eq a => (LispVal -> ThrowsError a) -> Unpacker
AnyUnpacker LispVal -> ThrowsError FilePath
unpackStr, (LispVal -> Either LispError Bool) -> Unpacker
forall a. Eq a => (LispVal -> ThrowsError a) -> Unpacker
AnyUnpacker LispVal -> Either LispError Bool
unpackBool]
LispVal
eqvEquals <- [LispVal] -> ThrowsError LispVal
eqv [LispVal
arg1, LispVal
arg2]
LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool (Bool -> LispVal) -> Bool -> LispVal
forall a b. (a -> b) -> a -> b
$ (Bool
primitiveEquals Bool -> Bool -> Bool
|| let (Bool Bool
x) = LispVal
eqvEquals in Bool
x)
equal [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
badArgList
makeVector :: [LispVal] -> ThrowsError LispVal
makeVector :: [LispVal] -> ThrowsError LispVal
makeVector [(Number Integer
n)] = [LispVal] -> ThrowsError LispVal
makeVector [Integer -> LispVal
Number Integer
n, [LispVal] -> LispVal
List []]
makeVector [(Number Integer
n), LispVal
a] = do
let l :: [LispVal]
l = Int -> LispVal -> [LispVal]
forall a. Int -> a -> [a]
replicate (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
n) LispVal
a
LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Array Int LispVal -> LispVal
Vector (Array Int LispVal -> LispVal) -> Array Int LispVal -> LispVal
forall a b. (a -> b) -> a -> b
$ ((Int, Int) -> [LispVal] -> Array Int LispVal
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
0, [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) [LispVal]
l
makeVector [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"integer" LispVal
badType
makeVector [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
buildVector :: [LispVal] -> ThrowsError LispVal
buildVector :: [LispVal] -> ThrowsError LispVal
buildVector lst :: [LispVal]
lst@(LispVal
_ : [LispVal]
_) = do
LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Array Int LispVal -> LispVal
Vector (Array Int LispVal -> LispVal) -> Array Int LispVal -> LispVal
forall a b. (a -> b) -> a -> b
$ ((Int, Int) -> [LispVal] -> Array Int LispVal
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
0, [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
lst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) [LispVal]
lst
buildVector [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
vectorLength :: [LispVal] -> ThrowsError LispVal
vectorLength :: [LispVal] -> ThrowsError LispVal
vectorLength [(Vector Array Int LispVal
v)] = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> LispVal
Number (Integer -> LispVal) -> Integer -> LispVal
forall a b. (a -> b) -> a -> b
$ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (Array Int LispVal -> [LispVal]
forall i e. Array i e -> [e]
elems Array Int LispVal
v)
vectorLength [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"vector" LispVal
badType
vectorLength [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
vectorRef :: [LispVal] -> ThrowsError LispVal
vectorRef :: [LispVal] -> ThrowsError LispVal
vectorRef [(Vector Array Int LispVal
v), (Number Integer
n)] = do
let len :: Integer
len = Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ ([LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([LispVal] -> Int) -> [LispVal] -> Int
forall a b. (a -> b) -> a -> b
$ Array Int LispVal -> [LispVal]
forall i e. Array i e -> [e]
elems Array Int LispVal
v) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
if Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
len Bool -> Bool -> Bool
|| Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0
then LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispError
Default FilePath
"Invalid index"
else LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Array Int LispVal
v Array Int LispVal -> Int -> LispVal
forall i e. Ix i => Array i e -> i -> e
! (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
n)
vectorRef [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"vector integer" LispVal
badType
vectorRef [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
badArgList
vectorToList :: [LispVal] -> ThrowsError LispVal
vectorToList :: [LispVal] -> ThrowsError LispVal
vectorToList [(Vector Array Int LispVal
v)] = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ Array Int LispVal -> [LispVal]
forall i e. Array i e -> [e]
elems Array Int LispVal
v
vectorToList [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"vector" LispVal
badType
vectorToList [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
listToVector :: [LispVal] -> ThrowsError LispVal
listToVector :: [LispVal] -> ThrowsError LispVal
listToVector [(List [LispVal]
l)] = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Array Int LispVal -> LispVal
Vector (Array Int LispVal -> LispVal) -> Array Int LispVal -> LispVal
forall a b. (a -> b) -> a -> b
$ ((Int, Int) -> [LispVal] -> Array Int LispVal
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
0, [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) [LispVal]
l
listToVector [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"list" LispVal
badType
listToVector [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
makeByteVector :: [LispVal] -> ThrowsError LispVal
makeByteVector :: [LispVal] -> ThrowsError LispVal
makeByteVector [(Number Integer
n)] = do
let ls :: [Word8]
ls = Int -> Word8 -> [Word8]
forall a. Int -> a -> [a]
replicate (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
n) (Word8
0 :: Word8)
LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> LispVal
ByteVector (ByteString -> LispVal) -> ByteString -> LispVal
forall a b. (a -> b) -> a -> b
$ [Word8] -> ByteString
BS.pack [Word8]
ls
makeByteVector [Number Integer
n, Number Integer
byte] = do
let ls :: [Word8]
ls = Int -> Word8 -> [Word8]
forall a. Int -> a -> [a]
replicate (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
n) (Integer -> Word8
forall a. Num a => Integer -> a
fromInteger Integer
byte :: Word8)
LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> LispVal
ByteVector (ByteString -> LispVal) -> ByteString -> LispVal
forall a b. (a -> b) -> a -> b
$ [Word8] -> ByteString
BS.pack [Word8]
ls
makeByteVector [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"integer" LispVal
badType
makeByteVector [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
badArgList
byteVector :: [LispVal] -> ThrowsError LispVal
byteVector :: [LispVal] -> ThrowsError LispVal
byteVector [LispVal]
bs = do
LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> LispVal
ByteVector (ByteString -> LispVal) -> ByteString -> LispVal
forall a b. (a -> b) -> a -> b
$ [Word8] -> ByteString
BS.pack ([Word8] -> ByteString) -> [Word8] -> ByteString
forall a b. (a -> b) -> a -> b
$ (LispVal -> Word8) -> [LispVal] -> [Word8]
forall a b. (a -> b) -> [a] -> [b]
map LispVal -> Word8
conv [LispVal]
bs
where
conv :: LispVal -> Word8
conv (Number Integer
n) = Integer -> Word8
forall a. Num a => Integer -> a
fromInteger Integer
n :: Word8
conv LispVal
_ = Word8
0 :: Word8
byteVectorCopy :: [LispVal] -> IOThrowsError LispVal
byteVectorCopy :: [LispVal] -> IOThrowsError LispVal
byteVectorCopy (p :: LispVal
p@(Pointer FilePath
_ Env
_) : [LispVal]
lvs) = do
LispVal
bv <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
[LispVal] -> IOThrowsError LispVal
byteVectorCopy (LispVal
bv LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
lvs)
byteVectorCopy [ByteVector ByteString
bv] = do
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> LispVal
ByteVector (ByteString -> LispVal) -> ByteString -> LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
BS.copy
ByteString
bv
byteVectorCopy [ByteVector ByteString
bv, Number Integer
start] = do
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> LispVal
ByteVector (ByteString -> LispVal) -> ByteString -> LispVal
forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> ByteString
BS.drop
(Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
start)
ByteString
bv
byteVectorCopy [ByteVector ByteString
bv, Number Integer
start, Number Integer
end] = do
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> LispVal
ByteVector (ByteString -> LispVal) -> ByteString -> LispVal
forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> ByteString
BS.take
(Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Integer
end Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
start)
(Int -> ByteString -> ByteString
BS.drop
(Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
start)
ByteString
bv)
byteVectorCopy [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"bytevector" LispVal
badType
byteVectorCopy [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
byteVectorAppend :: [LispVal] -> IOThrowsError LispVal
byteVectorAppend :: [LispVal] -> IOThrowsError LispVal
byteVectorAppend [LispVal]
bs = do
let conv :: LispVal -> IOThrowsError BSU.ByteString
conv :: LispVal -> ExceptT LispError IO ByteString
conv p :: LispVal
p@(Pointer FilePath
_ Env
_) = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO ByteString)
-> ExceptT LispError IO ByteString
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO ByteString
conv
conv (ByteVector ByteString
bvs) = ByteString -> ExceptT LispError IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
bvs
conv LispVal
_ = ByteString -> ExceptT LispError IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
BS.empty
[ByteString]
bs' <- (LispVal -> ExceptT LispError IO ByteString)
-> [LispVal] -> ExceptT LispError IO [ByteString]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM LispVal -> ExceptT LispError IO ByteString
conv [LispVal]
bs
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> LispVal
ByteVector (ByteString -> LispVal) -> ByteString -> LispVal
forall a b. (a -> b) -> a -> b
$ [ByteString] -> ByteString
BS.concat [ByteString]
bs'
byteVectorLength :: [LispVal] -> IOThrowsError LispVal
byteVectorLength :: [LispVal] -> IOThrowsError LispVal
byteVectorLength [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
byteVectorLength
byteVectorLength [(ByteVector ByteString
bv)] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> LispVal
Number (Integer -> LispVal) -> Integer -> LispVal
forall a b. (a -> b) -> a -> b
$ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ ByteString -> Int
BS.length ByteString
bv
byteVectorLength [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"bytevector" LispVal
badType
byteVectorLength [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
byteVectorRef :: [LispVal] -> IOThrowsError LispVal
byteVectorRef :: [LispVal] -> IOThrowsError LispVal
byteVectorRef (p :: LispVal
p@(Pointer FilePath
_ Env
_) : [LispVal]
lvs) = do
LispVal
bv <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
[LispVal] -> IOThrowsError LispVal
byteVectorRef (LispVal
bv LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
lvs)
byteVectorRef [(ByteVector ByteString
bv), (Number Integer
n)] = do
let len :: Integer
len = Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ (ByteString -> Int
BS.length ByteString
bv) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
if Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
len Bool -> Bool -> Bool
|| Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0
then LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispError
Default FilePath
"Invalid index"
else LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> LispVal
Number (Integer -> LispVal) -> Integer -> LispVal
forall a b. (a -> b) -> a -> b
$ Word8 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word8 -> Integer) -> Word8 -> Integer
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> Word8
BS.index ByteString
bv (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
n)
byteVectorRef [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"bytevector integer" LispVal
badType
byteVectorRef [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
badArgList
byteVectorUtf2Str :: [LispVal] -> IOThrowsError LispVal
byteVectorUtf2Str :: [LispVal] -> IOThrowsError LispVal
byteVectorUtf2Str [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
byteVectorUtf2Str
byteVectorUtf2Str [(ByteVector ByteString
bv)] = do
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String (FilePath -> LispVal) -> FilePath -> LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> FilePath
BSU.toString ByteString
bv
byteVectorUtf2Str [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"bytevector" LispVal
badType
byteVectorUtf2Str [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
byteVectorStr2Utf :: [LispVal] -> IOThrowsError LispVal
byteVectorStr2Utf :: [LispVal] -> IOThrowsError LispVal
byteVectorStr2Utf [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
byteVectorStr2Utf
byteVectorStr2Utf [(String FilePath
s)] = do
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ ByteString -> LispVal
ByteVector (ByteString -> LispVal) -> ByteString -> LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> ByteString
BSU.fromString FilePath
s
byteVectorStr2Utf [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string" LispVal
badType
byteVectorStr2Utf [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
wrapHashTbl :: ([LispVal] -> ThrowsError LispVal) -> [LispVal] -> IOThrowsError LispVal
wrapHashTbl :: ([LispVal] -> ThrowsError LispVal)
-> [LispVal] -> IOThrowsError LispVal
wrapHashTbl [LispVal] -> ThrowsError LispVal
fnc [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = do
LispVal
val <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
ThrowsError LispVal -> IOThrowsError LispVal
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError LispVal -> IOThrowsError LispVal)
-> ThrowsError LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> ThrowsError LispVal
fnc [LispVal
val]
wrapHashTbl [LispVal] -> ThrowsError LispVal
fnc (p :: LispVal
p@(Pointer FilePath
_ Env
_) : LispVal
key : [LispVal]
args) = do
LispVal
ht <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
LispVal
k <- LispVal -> IOThrowsError LispVal
recDerefPtrs LispVal
key
ThrowsError LispVal -> IOThrowsError LispVal
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError LispVal -> IOThrowsError LispVal)
-> ThrowsError LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> ThrowsError LispVal
fnc (LispVal
ht LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: LispVal
k LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
args)
wrapHashTbl [LispVal] -> ThrowsError LispVal
fnc [LispVal]
args = ThrowsError LispVal -> IOThrowsError LispVal
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError LispVal -> IOThrowsError LispVal)
-> ThrowsError LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> ThrowsError LispVal
fnc [LispVal]
args
wrapLeadObj :: ([LispVal] -> ThrowsError LispVal) -> [LispVal] -> IOThrowsError LispVal
wrapLeadObj :: ([LispVal] -> ThrowsError LispVal)
-> [LispVal] -> IOThrowsError LispVal
wrapLeadObj [LispVal] -> ThrowsError LispVal
fnc [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = do
LispVal
val <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
ThrowsError LispVal -> IOThrowsError LispVal
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError LispVal -> IOThrowsError LispVal)
-> ThrowsError LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> ThrowsError LispVal
fnc [LispVal
val]
wrapLeadObj [LispVal] -> ThrowsError LispVal
fnc (p :: LispVal
p@(Pointer FilePath
_ Env
_) : [LispVal]
args) = do
LispVal
obj <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
ThrowsError LispVal -> IOThrowsError LispVal
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError LispVal -> IOThrowsError LispVal)
-> ThrowsError LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> ThrowsError LispVal
fnc (LispVal
obj LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
args)
wrapLeadObj [LispVal] -> ThrowsError LispVal
fnc [LispVal]
args = ThrowsError LispVal -> IOThrowsError LispVal
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError LispVal -> IOThrowsError LispVal)
-> ThrowsError LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> ThrowsError LispVal
fnc [LispVal]
args
hashTblMake :: [LispVal] -> ThrowsError LispVal
hashTblMake :: [LispVal] -> ThrowsError LispVal
hashTblMake [LispVal]
_ = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Map LispVal LispVal -> LispVal
HashTable (Map LispVal LispVal -> LispVal) -> Map LispVal LispVal -> LispVal
forall a b. (a -> b) -> a -> b
$ [(LispVal, LispVal)] -> Map LispVal LispVal
forall k a. Ord k => [(k, a)] -> Map k a
Data.Map.fromList []
isHashTbl :: [LispVal] -> ThrowsError LispVal
isHashTbl :: [LispVal] -> ThrowsError LispVal
isHashTbl [(HashTable Map LispVal LispVal
_)] = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isHashTbl [LispVal]
_ = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
hashTblExists :: [LispVal] -> ThrowsError LispVal
hashTblExists :: [LispVal] -> ThrowsError LispVal
hashTblExists [(HashTable Map LispVal LispVal
ht), LispVal
key] = do
case LispVal -> Map LispVal LispVal -> Maybe LispVal
forall k a. Ord k => k -> Map k a -> Maybe a
Data.Map.lookup LispVal
key Map LispVal LispVal
ht of
Just LispVal
_ -> LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
Maybe LispVal
Nothing -> LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
hashTblExists [] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) []
hashTblExists args :: [LispVal]
args@(LispVal
_ : [LispVal]
_) = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
args
hashTblSize :: [LispVal] -> ThrowsError LispVal
hashTblSize :: [LispVal] -> ThrowsError LispVal
hashTblSize [(HashTable Map LispVal LispVal
ht)] = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> LispVal
Number (Integer -> LispVal) -> Integer -> LispVal
forall a b. (a -> b) -> a -> b
$ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ Map LispVal LispVal -> Int
forall k a. Map k a -> Int
Data.Map.size Map LispVal LispVal
ht
hashTblSize [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"hash-table" LispVal
badType
hashTblSize [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
hashTbl2List :: [LispVal] -> ThrowsError LispVal
hashTbl2List :: [LispVal] -> ThrowsError LispVal
hashTbl2List [(HashTable Map LispVal LispVal
ht)] = do
LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ ((LispVal, LispVal) -> LispVal)
-> [(LispVal, LispVal)] -> [LispVal]
forall a b. (a -> b) -> [a] -> [b]
map (\ (LispVal
k, LispVal
v) -> [LispVal] -> LispVal
List [LispVal
k, LispVal
v]) ([(LispVal, LispVal)] -> [LispVal])
-> [(LispVal, LispVal)] -> [LispVal]
forall a b. (a -> b) -> a -> b
$ Map LispVal LispVal -> [(LispVal, LispVal)]
forall k a. Map k a -> [(k, a)]
Data.Map.toList Map LispVal LispVal
ht
hashTbl2List [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"hash-table" LispVal
badType
hashTbl2List [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
hashTblKeys :: [LispVal] -> ThrowsError LispVal
hashTblKeys :: [LispVal] -> ThrowsError LispVal
hashTblKeys [(HashTable Map LispVal LispVal
ht)] = do
LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ ((LispVal, LispVal) -> LispVal)
-> [(LispVal, LispVal)] -> [LispVal]
forall a b. (a -> b) -> [a] -> [b]
map (LispVal, LispVal) -> LispVal
forall a b. (a, b) -> a
fst ([(LispVal, LispVal)] -> [LispVal])
-> [(LispVal, LispVal)] -> [LispVal]
forall a b. (a -> b) -> a -> b
$ Map LispVal LispVal -> [(LispVal, LispVal)]
forall k a. Map k a -> [(k, a)]
Data.Map.toList Map LispVal LispVal
ht
hashTblKeys [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"hash-table" LispVal
badType
hashTblKeys [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
hashTblValues :: [LispVal] -> ThrowsError LispVal
hashTblValues :: [LispVal] -> ThrowsError LispVal
hashTblValues [(HashTable Map LispVal LispVal
ht)] = do
LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ ((LispVal, LispVal) -> LispVal)
-> [(LispVal, LispVal)] -> [LispVal]
forall a b. (a -> b) -> [a] -> [b]
map (LispVal, LispVal) -> LispVal
forall a b. (a, b) -> b
snd ([(LispVal, LispVal)] -> [LispVal])
-> [(LispVal, LispVal)] -> [LispVal]
forall a b. (a -> b) -> a -> b
$ Map LispVal LispVal -> [(LispVal, LispVal)]
forall k a. Map k a -> [(k, a)]
Data.Map.toList Map LispVal LispVal
ht
hashTblValues [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"hash-table" LispVal
badType
hashTblValues [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
hashTblCopy :: [LispVal] -> ThrowsError LispVal
hashTblCopy :: [LispVal] -> ThrowsError LispVal
hashTblCopy [(HashTable Map LispVal LispVal
ht)] = do
LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Map LispVal LispVal -> LispVal
HashTable (Map LispVal LispVal -> LispVal) -> Map LispVal LispVal -> LispVal
forall a b. (a -> b) -> a -> b
$ [(LispVal, LispVal)] -> Map LispVal LispVal
forall k a. Ord k => [(k, a)] -> Map k a
Data.Map.fromList ([(LispVal, LispVal)] -> Map LispVal LispVal)
-> [(LispVal, LispVal)] -> Map LispVal LispVal
forall a b. (a -> b) -> a -> b
$ Map LispVal LispVal -> [(LispVal, LispVal)]
forall k a. Map k a -> [(k, a)]
Data.Map.toList Map LispVal LispVal
ht
hashTblCopy [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"hash-table" LispVal
badType
hashTblCopy [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
buildString :: [LispVal] -> ThrowsError LispVal
buildString :: [LispVal] -> ThrowsError LispVal
buildString [(Char Char
c)] = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String [Char
c]
buildString (Char Char
c : [LispVal]
rest) = do
LispVal
cs <- [LispVal] -> ThrowsError LispVal
buildString [LispVal]
rest
case LispVal
cs of
String FilePath
s -> LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String (FilePath -> LispVal) -> FilePath -> LispVal
forall a b. (a -> b) -> a -> b
$ Char
cChar -> FilePath -> FilePath
forall a. a -> [a] -> [a]
:FilePath
s
LispVal
badType -> LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"character" LispVal
badType
buildString [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"character" LispVal
badType
buildString [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
makeString :: [LispVal] -> ThrowsError LispVal
makeString :: [LispVal] -> ThrowsError LispVal
makeString [(Number Integer
n)] = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> Char -> FilePath -> LispVal
forall a. (Num a, Eq a) => a -> Char -> FilePath -> LispVal
doMakeString Integer
n Char
' ' FilePath
""
makeString [(Number Integer
n), (Char Char
c)] = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> Char -> FilePath -> LispVal
forall a. (Num a, Eq a) => a -> Char -> FilePath -> LispVal
doMakeString Integer
n Char
c FilePath
""
makeString [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
doMakeString :: forall a . (Num a, Eq a) => a -> Char -> String -> LispVal
doMakeString :: a -> Char -> FilePath -> LispVal
doMakeString a
n Char
char FilePath
s =
if a
n a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0
then FilePath -> LispVal
String FilePath
s
else a -> Char -> FilePath -> LispVal
forall a. (Num a, Eq a) => a -> Char -> FilePath -> LispVal
doMakeString (a
n a -> a -> a
forall a. Num a => a -> a -> a
- a
1) Char
char (FilePath
s FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ [Char
char])
stringLength :: [LispVal] -> IOThrowsError LispVal
stringLength :: [LispVal] -> IOThrowsError LispVal
stringLength [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
stringLength
stringLength [String FilePath
s] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> LispVal
Number (Integer -> LispVal) -> Integer -> LispVal
forall a b. (a -> b) -> a -> b
$ (Char -> Integer -> Integer) -> Integer -> FilePath -> Integer
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((Integer -> Integer) -> Char -> Integer -> Integer
forall a b. a -> b -> a
const (Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1)) Integer
0 FilePath
s
stringLength [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string" LispVal
badType
stringLength [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
stringRef :: [LispVal] -> IOThrowsError LispVal
stringRef :: [LispVal] -> IOThrowsError LispVal
stringRef [p :: LispVal
p@(Pointer FilePath
_ Env
_), k :: LispVal
k@(Number Integer
_)] = do
LispVal
s <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
[LispVal] -> IOThrowsError LispVal
stringRef [LispVal
s, LispVal
k]
stringRef [(String FilePath
s), (Number Integer
k)] = do
let len :: Integer
len = Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ (FilePath -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length FilePath
s) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
if Integer
k Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
len Bool -> Bool -> Bool
|| Integer
k Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0
then LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispError
Default (FilePath -> LispError) -> FilePath -> LispError
forall a b. (a -> b) -> a -> b
$ FilePath
"Invalid index " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ (Integer -> FilePath
forall a. Show a => a -> FilePath
show Integer
k)
else LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Char -> LispVal
Char (Char -> LispVal) -> Char -> LispVal
forall a b. (a -> b) -> a -> b
$ FilePath
s FilePath -> Int -> Char
forall a. [a] -> Int -> a
!! Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
k
stringRef [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string number" LispVal
badType
stringRef [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
badArgList
substring :: [LispVal] -> IOThrowsError LispVal
substring :: [LispVal] -> IOThrowsError LispVal
substring (p :: LispVal
p@(Pointer FilePath
_ Env
_) : [LispVal]
lvs) = do
LispVal
s <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
[LispVal] -> IOThrowsError LispVal
substring (LispVal
s LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
lvs)
substring [(String FilePath
s), (Number Integer
start), (Number Integer
end)] =
do let slength :: Int
slength = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Integer
end Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
start
let begin :: Int
begin = Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
start
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String (FilePath -> LispVal) -> FilePath -> LispVal
forall a b. (a -> b) -> a -> b
$ (Int -> FilePath -> FilePath
forall a. Int -> [a] -> [a]
take Int
slength (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> FilePath -> FilePath
forall a. Int -> [a] -> [a]
drop Int
begin) FilePath
s
substring [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string number number" LispVal
badType
substring [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
3) [LispVal]
badArgList
stringCIEquals :: [LispVal] -> IOThrowsError LispVal
stringCIEquals :: [LispVal] -> IOThrowsError LispVal
stringCIEquals [LispVal]
args = do
List [LispVal]
dargs <- LispVal -> IOThrowsError LispVal
recDerefPtrs (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
args
case [LispVal]
dargs of
[(String FilePath
str1), (String FilePath
str2)] -> do
if (FilePath -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length FilePath
str1) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= (FilePath -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length FilePath
str2)
then LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
else LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool (Bool -> LispVal) -> Bool -> LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> Int -> Bool
ciCmp FilePath
str1 FilePath
str2 Int
0
[LispVal
badType] -> LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string string" LispVal
badType
[LispVal]
badArgList -> LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
badArgList
where ciCmp :: FilePath -> FilePath -> Int -> Bool
ciCmp FilePath
s1 FilePath
s2 Int
idx =
(Int
idx Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== (FilePath -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length FilePath
s1)) Bool -> Bool -> Bool
||
(((Char -> Char
toLower (Char -> Char) -> Char -> Char
forall a b. (a -> b) -> a -> b
$ FilePath
s1 FilePath -> Int -> Char
forall a. [a] -> Int -> a
!! Int
idx) Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== (Char -> Char
toLower (Char -> Char) -> Char -> Char
forall a b. (a -> b) -> a -> b
$ FilePath
s2 FilePath -> Int -> Char
forall a. [a] -> Int -> a
!! Int
idx)) Bool -> Bool -> Bool
&&
FilePath -> FilePath -> Int -> Bool
ciCmp FilePath
s1 FilePath
s2 (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
stringCIBoolBinop :: (String -> String -> Bool) -> [LispVal] -> IOThrowsError LispVal
stringCIBoolBinop :: (FilePath -> FilePath -> Bool)
-> [LispVal] -> IOThrowsError LispVal
stringCIBoolBinop FilePath -> FilePath -> Bool
op [LispVal]
args = do
List [LispVal]
dargs <- LispVal -> IOThrowsError LispVal
recDerefPtrs (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
args
case [LispVal]
dargs of
[(String FilePath
s1), (String FilePath
s2)] ->
ThrowsError LispVal -> IOThrowsError LispVal
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError LispVal -> IOThrowsError LispVal)
-> ThrowsError LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ (LispVal -> ThrowsError FilePath)
-> (FilePath -> FilePath -> Bool)
-> [LispVal]
-> ThrowsError LispVal
forall a.
(LispVal -> ThrowsError a)
-> (a -> a -> Bool) -> [LispVal] -> ThrowsError LispVal
boolBinop LispVal -> ThrowsError FilePath
unpackStr FilePath -> FilePath -> Bool
op [(FilePath -> LispVal
String (FilePath -> LispVal) -> FilePath -> LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath
strToLower FilePath
s1), (FilePath -> LispVal
String (FilePath -> LispVal) -> FilePath -> LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath
strToLower FilePath
s2)]
[LispVal
badType] -> LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string string" LispVal
badType
[LispVal]
badArgList -> LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
badArgList
where strToLower :: FilePath -> FilePath
strToLower = (Char -> Char) -> FilePath -> FilePath
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower
charCIBoolBinop :: (Char -> Char -> Bool) -> [LispVal] -> ThrowsError LispVal
charCIBoolBinop :: (Char -> Char -> Bool) -> [LispVal] -> ThrowsError LispVal
charCIBoolBinop Char -> Char -> Bool
op [(Char Char
s1), (Char Char
s2)] = (LispVal -> ThrowsError Char)
-> (Char -> Char -> Bool) -> [LispVal] -> ThrowsError LispVal
forall a.
(LispVal -> ThrowsError a)
-> (a -> a -> Bool) -> [LispVal] -> ThrowsError LispVal
boolBinop LispVal -> ThrowsError Char
unpackChar Char -> Char -> Bool
op [(Char -> LispVal
Char (Char -> LispVal) -> Char -> LispVal
forall a b. (a -> b) -> a -> b
$ Char -> Char
toLower Char
s1), (Char -> LispVal
Char (Char -> LispVal) -> Char -> LispVal
forall a b. (a -> b) -> a -> b
$ Char -> Char
toLower Char
s2)]
charCIBoolBinop Char -> Char -> Bool
_ [LispVal
badType] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"character character" LispVal
badType
charCIBoolBinop Char -> Char -> Bool
_ [LispVal]
badArgList = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
badArgList
stringAppend :: [LispVal] -> IOThrowsError LispVal
stringAppend :: [LispVal] -> IOThrowsError LispVal
stringAppend (p :: LispVal
p@(Pointer FilePath
_ Env
_) : [LispVal]
lvs) = do
LispVal
s <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
[LispVal] -> IOThrowsError LispVal
stringAppend (LispVal
s LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
lvs)
stringAppend [(String FilePath
s)] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String FilePath
s
stringAppend (String FilePath
st : [LispVal]
sts) = do
LispVal
rest <- [LispVal] -> IOThrowsError LispVal
stringAppend [LispVal]
sts
case LispVal
rest of
String FilePath
s -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String (FilePath -> LispVal) -> FilePath -> LispVal
forall a b. (a -> b) -> a -> b
$ FilePath
st FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
s
LispVal
other -> LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string" LispVal
other
stringAppend [] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String FilePath
""
stringAppend [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string" LispVal
badType
stringAppend [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
stringToNumber :: [LispVal] -> IOThrowsError LispVal
stringToNumber :: [LispVal] -> IOThrowsError LispVal
stringToNumber (p :: LispVal
p@(Pointer FilePath
_ Env
_) : [LispVal]
lvs) = do
LispVal
s <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
[LispVal] -> IOThrowsError LispVal
stringToNumber (LispVal
s LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
lvs)
stringToNumber [(String FilePath
s)] = do
LispVal
result <- ThrowsError LispVal -> IOThrowsError LispVal
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError LispVal -> IOThrowsError LispVal)
-> ThrowsError LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> ThrowsError LispVal
readExpr FilePath
s
case LispVal
result of
n :: LispVal
n@(Number Integer
_) -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return LispVal
n
n :: LispVal
n@(Rational Rational
_) -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return LispVal
n
n :: LispVal
n@(Float Double
_) -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return LispVal
n
n :: LispVal
n@(Complex Complex Double
_) -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return LispVal
n
LispVal
_ -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
stringToNumber [(String FilePath
s), Number Integer
radix] = do
case Integer
radix of
Integer
2 -> [LispVal] -> IOThrowsError LispVal
stringToNumber [FilePath -> LispVal
String (FilePath -> LispVal) -> FilePath -> LispVal
forall a b. (a -> b) -> a -> b
$ FilePath
"#b" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
s]
Integer
8 -> [LispVal] -> IOThrowsError LispVal
stringToNumber [FilePath -> LispVal
String (FilePath -> LispVal) -> FilePath -> LispVal
forall a b. (a -> b) -> a -> b
$ FilePath
"#o" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
s]
Integer
10 -> [LispVal] -> IOThrowsError LispVal
stringToNumber [FilePath -> LispVal
String FilePath
s]
Integer
16 -> [LispVal] -> IOThrowsError LispVal
stringToNumber [FilePath -> LispVal
String (FilePath -> LispVal) -> FilePath -> LispVal
forall a b. (a -> b) -> a -> b
$ FilePath
"#x" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
s]
Integer
_ -> LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispError
Default (FilePath -> LispError) -> FilePath -> LispError
forall a b. (a -> b) -> a -> b
$ FilePath
"Invalid radix: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Integer -> FilePath
forall a. Show a => a -> FilePath
show Integer
radix
stringToNumber [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string" LispVal
badType
stringToNumber [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
stringToList :: [LispVal] -> IOThrowsError LispVal
stringToList :: [LispVal] -> IOThrowsError LispVal
stringToList (p :: LispVal
p@(Pointer FilePath
_ Env
_) : [LispVal]
ps) = do
LispVal
p' <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
[LispVal] -> IOThrowsError LispVal
stringToList (LispVal
p' LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
ps)
stringToList [(String FilePath
s)] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ (Char -> LispVal) -> FilePath -> [LispVal]
forall a b. (a -> b) -> [a] -> [b]
map Char -> LispVal
Char FilePath
s
stringToList [String FilePath
s, Number Integer
start] =
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ (Char -> LispVal) -> FilePath -> [LispVal]
forall a b. (a -> b) -> [a] -> [b]
map Char -> LispVal
Char (FilePath -> [LispVal]) -> FilePath -> [LispVal]
forall a b. (a -> b) -> a -> b
$ Integer -> FilePath -> FilePath
forall a. Integer -> [a] -> [a]
trimStart Integer
start FilePath
s
stringToList [String FilePath
s, Number Integer
start, Number Integer
end] =
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ (Char -> LispVal) -> FilePath -> [LispVal]
forall a b. (a -> b) -> [a] -> [b]
map Char -> LispVal
Char (FilePath -> [LispVal]) -> FilePath -> [LispVal]
forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> FilePath -> FilePath
forall a. Integer -> Integer -> [a] -> [a]
trimStartEnd Integer
start Integer
end FilePath
s
stringToList [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string" LispVal
badType
stringToList [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
badArgList
trimStart :: Integer -> [a] -> [a]
trimStart :: Integer -> [a] -> [a]
trimStart Integer
start = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
start)
trimStartEnd :: Integer -> Integer -> [a] -> [a]
trimStartEnd :: Integer -> Integer -> [a] -> [a]
trimStartEnd Integer
start Integer
end [a]
ls =
Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Integer
end Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
start) ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
start) [a]
ls
listToString :: [LispVal] -> IOThrowsError LispVal
listToString :: [LispVal] -> IOThrowsError LispVal
listToString [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
listToString
listToString [(List [])] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String FilePath
""
listToString [(List [LispVal]
l)] = ThrowsError LispVal -> IOThrowsError LispVal
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError LispVal -> IOThrowsError LispVal)
-> ThrowsError LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> ThrowsError LispVal
buildString [LispVal]
l
listToString [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"list" LispVal
badType
listToString [] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) []
listToString args :: [LispVal]
args@(LispVal
_ : [LispVal]
_) = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
stringToVector :: [LispVal] -> IOThrowsError LispVal
stringToVector :: [LispVal] -> IOThrowsError LispVal
stringToVector [LispVal]
args = do
List [LispVal]
l <- [LispVal] -> IOThrowsError LispVal
stringToList [LispVal]
args
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Array Int LispVal -> LispVal
Vector (Array Int LispVal -> LispVal) -> Array Int LispVal -> LispVal
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> [LispVal] -> Array Int LispVal
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
0, [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [LispVal]
l
vectorToString :: [LispVal] -> IOThrowsError LispVal
vectorToString :: [LispVal] -> IOThrowsError LispVal
vectorToString (p :: LispVal
p@(Pointer FilePath
_ Env
_) : [LispVal]
ps) = do
LispVal
p' <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
[LispVal] -> IOThrowsError LispVal
vectorToString (LispVal
p' LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
ps)
vectorToString [(Vector Array Int LispVal
v)] = do
let l :: [LispVal]
l = Array Int LispVal -> [LispVal]
forall i e. Array i e -> [e]
elems Array Int LispVal
v
case [LispVal]
l of
[] -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String FilePath
""
[LispVal]
_ -> ThrowsError LispVal -> IOThrowsError LispVal
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError LispVal -> IOThrowsError LispVal)
-> ThrowsError LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> ThrowsError LispVal
buildString [LispVal]
l
vectorToString [Vector Array Int LispVal
v, Number Integer
start] = do
[LispVal] -> IOThrowsError LispVal
listToString [[LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> [LispVal] -> [LispVal]
forall a. Integer -> [a] -> [a]
trimStart Integer
start (Array Int LispVal -> [LispVal]
forall i e. Array i e -> [e]
elems Array Int LispVal
v)]
vectorToString [Vector Array Int LispVal
v, Number Integer
start, Number Integer
end] = do
[LispVal] -> IOThrowsError LispVal
listToString [[LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> [LispVal] -> [LispVal]
forall a. Integer -> Integer -> [a] -> [a]
trimStartEnd Integer
start Integer
end (Array Int LispVal -> [LispVal]
forall i e. Array i e -> [e]
elems Array Int LispVal
v)]
vectorToString [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"vector" LispVal
badType
vectorToString [] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) []
vectorToString args :: [LispVal]
args@(LispVal
_ : [LispVal]
_) = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
stringCopy :: [LispVal] -> IOThrowsError LispVal
stringCopy :: [LispVal] -> IOThrowsError LispVal
stringCopy (p :: LispVal
p@(Pointer FilePath
_ Env
_) : [LispVal]
args) = do
LispVal
s <- LispVal -> IOThrowsError LispVal
derefPtr LispVal
p
[LispVal] -> IOThrowsError LispVal
stringCopy (LispVal
s LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
args)
stringCopy [String FilePath
s] = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String FilePath
s
stringCopy [String FilePath
s, Number Integer
start] = do
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String (FilePath -> LispVal) -> FilePath -> LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> FilePath -> FilePath
forall a. Integer -> [a] -> [a]
trimStart Integer
start FilePath
s
stringCopy [String FilePath
s, Number Integer
start, Number Integer
end] = do
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String (FilePath -> LispVal) -> FilePath -> LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> FilePath -> FilePath
forall a. Integer -> Integer -> [a] -> [a]
trimStartEnd Integer
start Integer
end FilePath
s
stringCopy [LispVal
badType] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string" LispVal
badType
stringCopy [LispVal]
badArgList = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
badArgList
isDottedList :: [LispVal] -> IOThrowsError LispVal
isDottedList :: [LispVal] -> IOThrowsError LispVal
isDottedList ([p :: LispVal
p@(Pointer FilePath
_ Env
_)]) = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
isDottedList
isDottedList ([DottedList [LispVal]
_ LispVal
_]) = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isDottedList ([List []]) = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isDottedList ([List [LispVal]
_]) = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isDottedList [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isProcedure :: [LispVal] -> ThrowsError LispVal
isProcedure :: [LispVal] -> ThrowsError LispVal
isProcedure ([Continuation {}]) = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isProcedure ([PrimitiveFunc [LispVal] -> ThrowsError LispVal
_]) = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isProcedure ([Func {}]) = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isProcedure ([HFunc {}]) = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isProcedure ([IOFunc [LispVal] -> IOThrowsError LispVal
_]) = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isProcedure ([EvalFunc [LispVal] -> IOThrowsError LispVal
_]) = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isProcedure ([CustFunc [LispVal] -> IOThrowsError LispVal
_]) = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isProcedure [LispVal]
_ = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isVector :: LispVal -> IOThrowsError LispVal
isVector :: LispVal -> IOThrowsError LispVal
isVector p :: LispVal
p@(Pointer FilePath
_ Env
_) = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> IOThrowsError LispVal
isVector
isVector (Vector Array Int LispVal
vs) = do
case Array Int LispVal -> [LispVal]
forall i e. Array i e -> [e]
elems Array Int LispVal
vs of
((Atom FilePath
" record-marker ") : [LispVal]
_) -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
[LispVal]
_ -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isVector LispVal
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isRecord :: LispVal -> IOThrowsError LispVal
isRecord :: LispVal -> IOThrowsError LispVal
isRecord p :: LispVal
p@(Pointer FilePath
_ Env
_) = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> IOThrowsError LispVal
isRecord
isRecord (Vector Array Int LispVal
vs) = do
case (Array Int LispVal -> [LispVal]
forall i e. Array i e -> [e]
elems Array Int LispVal
vs) of
((Atom FilePath
" record-marker ") : [LispVal]
_) -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
[LispVal]
_ -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isRecord LispVal
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isByteVector :: LispVal -> IOThrowsError LispVal
isByteVector :: LispVal -> IOThrowsError LispVal
isByteVector p :: LispVal
p@(Pointer FilePath
_ Env
_) = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> IOThrowsError LispVal
isVector
isByteVector (ByteVector ByteString
_) = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isByteVector LispVal
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isList :: LispVal -> IOThrowsError LispVal
isList :: LispVal -> IOThrowsError LispVal
isList p :: LispVal
p@(Pointer FilePath
_ Env
_) = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> IOThrowsError LispVal
isList
isList (List [LispVal]
_) = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isList LispVal
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isNull :: [LispVal] -> IOThrowsError LispVal
isNull :: [LispVal] -> IOThrowsError LispVal
isNull ([p :: LispVal
p@(Pointer FilePath
_ Env
_)]) = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
isNull
isNull ([List []]) = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isNull [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isEOFObject :: [LispVal] -> ThrowsError LispVal
isEOFObject :: [LispVal] -> ThrowsError LispVal
isEOFObject ([LispVal
EOF]) = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isEOFObject [LispVal]
_ = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
eofObject :: [LispVal] -> ThrowsError LispVal
eofObject :: [LispVal] -> ThrowsError LispVal
eofObject [LispVal]
_ = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ LispVal
EOF
isSymbol :: [LispVal] -> ThrowsError LispVal
isSymbol :: [LispVal] -> ThrowsError LispVal
isSymbol ([Atom FilePath
_]) = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isSymbol [LispVal]
_ = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
symbol2String :: [LispVal] -> ThrowsError LispVal
symbol2String :: [LispVal] -> ThrowsError LispVal
symbol2String ([Atom FilePath
a]) = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
String FilePath
a
symbol2String [LispVal
notAtom] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"symbol" LispVal
notAtom
symbol2String [] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) []
symbol2String args :: [LispVal]
args@(LispVal
_ : [LispVal]
_) = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
string2Symbol :: [LispVal] -> IOThrowsError LispVal
string2Symbol :: [LispVal] -> IOThrowsError LispVal
string2Symbol ([p :: LispVal
p@(Pointer FilePath
_ Env
_)]) = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
string2Symbol
string2Symbol ([String FilePath
s]) = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal
Atom FilePath
s
string2Symbol [] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) []
string2Symbol [LispVal
notString] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string" LispVal
notString
string2Symbol args :: [LispVal]
args@(LispVal
_ : [LispVal]
_) = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
charUpper :: [LispVal] -> ThrowsError LispVal
charUpper :: [LispVal] -> ThrowsError LispVal
charUpper [Char Char
c] = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Char -> LispVal
Char (Char -> LispVal) -> Char -> LispVal
forall a b. (a -> b) -> a -> b
$ Char -> Char
toUpper Char
c
charUpper [LispVal
notChar] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"char" LispVal
notChar
charUpper [LispVal]
args = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
charLower :: [LispVal] -> ThrowsError LispVal
charLower :: [LispVal] -> ThrowsError LispVal
charLower [Char Char
c] = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Char -> LispVal
Char (Char -> LispVal) -> Char -> LispVal
forall a b. (a -> b) -> a -> b
$ Char -> Char
toLower Char
c
charLower [LispVal
notChar] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"char" LispVal
notChar
charLower [LispVal]
args = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
charDigitValue :: [LispVal] -> ThrowsError LispVal
charDigitValue :: [LispVal] -> ThrowsError LispVal
charDigitValue [Char Char
c] = do
if Char -> Bool
isHexDigit Char
c
then LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> LispVal
Number (Integer -> LispVal) -> Integer -> LispVal
forall a b. (a -> b) -> a -> b
$ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ Char -> Int
digitToInt Char
c
else LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
charDigitValue [LispVal
notChar] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"char" LispVal
notChar
charDigitValue [LispVal]
args = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
char2Int :: [LispVal] -> ThrowsError LispVal
char2Int :: [LispVal] -> ThrowsError LispVal
char2Int [Char Char
c] = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> LispVal
Number (Integer -> LispVal) -> Integer -> LispVal
forall a b. (a -> b) -> a -> b
$ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ Char -> Int
ord Char
c
char2Int [LispVal
notChar] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"char" LispVal
notChar
char2Int [LispVal]
args = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
int2Char :: [LispVal] -> ThrowsError LispVal
int2Char :: [LispVal] -> ThrowsError LispVal
int2Char [Number Integer
n] = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Char -> LispVal
Char (Char -> LispVal) -> Char -> LispVal
forall a b. (a -> b) -> a -> b
$ Int -> Char
chr (Int -> Char) -> Int -> Char
forall a b. (a -> b) -> a -> b
$ Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
n
int2Char [LispVal
notInt] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"integer" LispVal
notInt
int2Char [LispVal]
args = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
charPredicate :: (Char -> Bool) -> [LispVal] -> ThrowsError LispVal
charPredicate :: (Char -> Bool) -> [LispVal] -> ThrowsError LispVal
charPredicate Char -> Bool
cpred ([Char Char
c]) = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool (Bool -> LispVal) -> Bool -> LispVal
forall a b. (a -> b) -> a -> b
$ Char -> Bool
cpred Char
c
charPredicate Char -> Bool
_ [LispVal]
_ = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isChar :: [LispVal] -> ThrowsError LispVal
isChar :: [LispVal] -> ThrowsError LispVal
isChar ([Char Char
_]) = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isChar [LispVal]
_ = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isString :: [LispVal] -> IOThrowsError LispVal
isString :: [LispVal] -> IOThrowsError LispVal
isString [p :: LispVal
p@(Pointer FilePath
_ Env
_)] = LispVal -> IOThrowsError LispVal
derefPtr LispVal
p IOThrowsError LispVal
-> (LispVal -> ExceptT LispError IO [LispVal])
-> ExceptT LispError IO [LispVal]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LispVal -> ExceptT LispError IO [LispVal]
box ExceptT LispError IO [LispVal]
-> ([LispVal] -> IOThrowsError LispVal) -> IOThrowsError LispVal
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [LispVal] -> IOThrowsError LispVal
isString
isString ([String FilePath
_]) = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isString [LispVal]
_ = LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isBoolean :: [LispVal] -> ThrowsError LispVal
isBoolean :: [LispVal] -> ThrowsError LispVal
isBoolean ([Bool Bool
_]) = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isBoolean [LispVal]
_ = LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isBooleanEq :: Monad m => [LispVal] -> m LispVal
isBooleanEq :: [LispVal] -> m LispVal
isBooleanEq (Bool Bool
a : Bool Bool
b : [LispVal]
bs)
| Bool
a Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool
b = [LispVal] -> m LispVal
forall (m :: * -> *). Monad m => [LispVal] -> m LispVal
isBooleanEq (Bool -> LispVal
Bool Bool
b LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
bs)
| Bool
otherwise = LispVal -> m LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> m LispVal) -> LispVal -> m LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isBooleanEq [Bool Bool
_] = LispVal -> m LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> m LispVal) -> LispVal -> m LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isBooleanEq [LispVal]
_ = LispVal -> m LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> m LispVal) -> LispVal -> m LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isSymbolEq :: Monad m => [LispVal] -> m LispVal
isSymbolEq :: [LispVal] -> m LispVal
isSymbolEq (Atom FilePath
a : Atom FilePath
b : [LispVal]
bs)
| FilePath
a FilePath -> FilePath -> Bool
forall a. Eq a => a -> a -> Bool
== FilePath
b = [LispVal] -> m LispVal
forall (m :: * -> *). Monad m => [LispVal] -> m LispVal
isSymbolEq (FilePath -> LispVal
Atom FilePath
b LispVal -> [LispVal] -> [LispVal]
forall a. a -> [a] -> [a]
: [LispVal]
bs)
| Bool
otherwise = LispVal -> m LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> m LispVal) -> LispVal -> m LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
isSymbolEq [Atom FilePath
_] = LispVal -> m LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> m LispVal) -> LispVal -> m LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
True
isSymbolEq [LispVal]
_ = LispVal -> m LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> m LispVal) -> LispVal -> m LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
False
data Unpacker = forall a . Eq a => AnyUnpacker (LispVal -> ThrowsError a)
unpackEquals :: LispVal -> LispVal -> Unpacker -> ThrowsError Bool
unpackEquals :: LispVal -> LispVal -> Unpacker -> Either LispError Bool
unpackEquals LispVal
arg1 LispVal
arg2 (AnyUnpacker LispVal -> ThrowsError a
unpacker) =
do a
unpacked1 <- LispVal -> ThrowsError a
unpacker LispVal
arg1
a
unpacked2 <- LispVal -> ThrowsError a
unpacker LispVal
arg2
Bool -> Either LispError Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Either LispError Bool) -> Bool -> Either LispError Bool
forall a b. (a -> b) -> a -> b
$ a
unpacked1 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
unpacked2
Either LispError Bool
-> (LispError -> Either LispError Bool) -> Either LispError Bool
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
`catchError` (Either LispError Bool -> LispError -> Either LispError Bool
forall a b. a -> b -> a
const (Either LispError Bool -> LispError -> Either LispError Bool)
-> Either LispError Bool -> LispError -> Either LispError Bool
forall a b. (a -> b) -> a -> b
$ Bool -> Either LispError Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False)
boolBinop :: (LispVal -> ThrowsError a) -> (a -> a -> Bool) -> [LispVal] -> ThrowsError LispVal
boolBinop :: (LispVal -> ThrowsError a)
-> (a -> a -> Bool) -> [LispVal] -> ThrowsError LispVal
boolBinop LispVal -> ThrowsError a
unpacker a -> a -> Bool
op [LispVal]
args = if [LispVal] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LispVal]
args Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
2
then LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
2) [LispVal]
args
else do
Bool
result <- LispVal -> [LispVal] -> Either LispError Bool
cmp ([LispVal] -> LispVal
forall a. [a] -> a
head [LispVal]
args) ([LispVal] -> [LispVal]
forall a. [a] -> [a]
tail [LispVal]
args)
LispVal -> ThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> ThrowsError LispVal) -> LispVal -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Bool -> LispVal
Bool Bool
result
where
cmp :: LispVal -> [LispVal] -> Either LispError Bool
cmp LispVal
b1 (LispVal
b2 : [LispVal]
bs) = do
a
b1' <- LispVal -> ThrowsError a
unpacker LispVal
b1
a
b2' <- LispVal -> ThrowsError a
unpacker LispVal
b2
let result :: Bool
result = a -> a -> Bool
op a
b1' a
b2'
if Bool
result
then LispVal -> [LispVal] -> Either LispError Bool
cmp LispVal
b2 [LispVal]
bs
else Bool -> Either LispError Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
cmp LispVal
_ [LispVal]
_ = Bool -> Either LispError Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
unaryOp :: (LispVal -> ThrowsError LispVal) -> [LispVal] -> ThrowsError LispVal
unaryOp :: (LispVal -> ThrowsError LispVal)
-> [LispVal] -> ThrowsError LispVal
unaryOp LispVal -> ThrowsError LispVal
f [LispVal
v] = LispVal -> ThrowsError LispVal
f LispVal
v
unaryOp LispVal -> ThrowsError LispVal
_ [] = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) []
unaryOp LispVal -> ThrowsError LispVal
_ args :: [LispVal]
args@(LispVal
_ : [LispVal]
_) = LispError -> ThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError LispVal)
-> LispError -> ThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
unaryOp' :: (LispVal -> IOThrowsError LispVal) -> [LispVal] -> IOThrowsError LispVal
unaryOp' :: (LispVal -> IOThrowsError LispVal)
-> [LispVal] -> IOThrowsError LispVal
unaryOp' LispVal -> IOThrowsError LispVal
f [LispVal
v] = LispVal -> IOThrowsError LispVal
f LispVal
v
unaryOp' LispVal -> IOThrowsError LispVal
_ [] = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) []
unaryOp' LispVal -> IOThrowsError LispVal
_ args :: [LispVal]
args@(LispVal
_ : [LispVal]
_) = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Maybe Integer -> [LispVal] -> LispError
NumArgs (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
1) [LispVal]
args
strBoolBinop :: (String -> String -> Bool) -> [LispVal] -> IOThrowsError LispVal
strBoolBinop :: (FilePath -> FilePath -> Bool)
-> [LispVal] -> IOThrowsError LispVal
strBoolBinop FilePath -> FilePath -> Bool
fnc [LispVal]
args = do
List [LispVal]
dargs <- LispVal -> IOThrowsError LispVal
recDerefPtrs (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
args
ThrowsError LispVal -> IOThrowsError LispVal
forall a. ThrowsError a -> IOThrowsError a
liftThrows (ThrowsError LispVal -> IOThrowsError LispVal)
-> ThrowsError LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ (LispVal -> ThrowsError FilePath)
-> (FilePath -> FilePath -> Bool)
-> [LispVal]
-> ThrowsError LispVal
forall a.
(LispVal -> ThrowsError a)
-> (a -> a -> Bool) -> [LispVal] -> ThrowsError LispVal
boolBinop LispVal -> ThrowsError FilePath
unpackStr FilePath -> FilePath -> Bool
fnc [LispVal]
dargs
charBoolBinop :: (Char -> Char -> Bool)
-> [LispVal] -> ThrowsError LispVal
charBoolBinop :: (Char -> Char -> Bool) -> [LispVal] -> ThrowsError LispVal
charBoolBinop = (LispVal -> ThrowsError Char)
-> (Char -> Char -> Bool) -> [LispVal] -> ThrowsError LispVal
forall a.
(LispVal -> ThrowsError a)
-> (a -> a -> Bool) -> [LispVal] -> ThrowsError LispVal
boolBinop LispVal -> ThrowsError Char
unpackChar
boolBoolBinop :: (Bool -> Bool -> Bool) -> [LispVal] -> ThrowsError LispVal
boolBoolBinop :: (Bool -> Bool -> Bool) -> [LispVal] -> ThrowsError LispVal
boolBoolBinop = (LispVal -> Either LispError Bool)
-> (Bool -> Bool -> Bool) -> [LispVal] -> ThrowsError LispVal
forall a.
(LispVal -> ThrowsError a)
-> (a -> a -> Bool) -> [LispVal] -> ThrowsError LispVal
boolBinop LispVal -> Either LispError Bool
unpackBool
unpackChar :: LispVal -> ThrowsError Char
unpackChar :: LispVal -> ThrowsError Char
unpackChar (Char Char
c) = Char -> ThrowsError Char
forall (m :: * -> *) a. Monad m => a -> m a
return Char
c
unpackChar LispVal
notChar = LispError -> ThrowsError Char
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError Char) -> LispError -> ThrowsError Char
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"character" LispVal
notChar
unpackStr :: LispVal -> ThrowsError String
unpackStr :: LispVal -> ThrowsError FilePath
unpackStr (String FilePath
s) = FilePath -> ThrowsError FilePath
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
s
unpackStr (Number Integer
s) = FilePath -> ThrowsError FilePath
forall (m :: * -> *) a. Monad m => a -> m a
return (FilePath -> ThrowsError FilePath)
-> FilePath -> ThrowsError FilePath
forall a b. (a -> b) -> a -> b
$ Integer -> FilePath
forall a. Show a => a -> FilePath
show Integer
s
unpackStr (Bool Bool
s) = FilePath -> ThrowsError FilePath
forall (m :: * -> *) a. Monad m => a -> m a
return (FilePath -> ThrowsError FilePath)
-> FilePath -> ThrowsError FilePath
forall a b. (a -> b) -> a -> b
$ Bool -> FilePath
forall a. Show a => a -> FilePath
show Bool
s
unpackStr LispVal
notString = LispError -> ThrowsError FilePath
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> ThrowsError FilePath)
-> LispError -> ThrowsError FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string" LispVal
notString
unpackBool :: LispVal -> ThrowsError Bool
unpackBool :: LispVal -> Either LispError Bool
unpackBool (Bool Bool
b) = Bool -> Either LispError Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
b
unpackBool LispVal
notBool = LispError -> Either LispError Bool
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> Either LispError Bool)
-> LispError -> Either LispError Bool
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"boolean" LispVal
notBool
currentTimestamp :: [LispVal] -> IOThrowsError LispVal
currentTimestamp :: [LispVal] -> IOThrowsError LispVal
currentTimestamp [LispVal]
_ = do
POSIXTime
cur <- IO POSIXTime -> ExceptT LispError IO POSIXTime
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO POSIXTime -> ExceptT LispError IO POSIXTime)
-> IO POSIXTime -> ExceptT LispError IO POSIXTime
forall a b. (a -> b) -> a -> b
$ IO POSIXTime
Data.Time.Clock.POSIX.getPOSIXTime
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Double -> LispVal
Float (Double -> LispVal) -> Double -> LispVal
forall a b. (a -> b) -> a -> b
$ POSIXTime -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac POSIXTime
cur
system :: [LispVal] -> IOThrowsError LispVal
system :: [LispVal] -> IOThrowsError LispVal
system [String FilePath
cmd] = do
ExitCode
result <- IO ExitCode -> ExceptT LispError IO ExitCode
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ExitCode -> ExceptT LispError IO ExitCode)
-> IO ExitCode -> ExceptT LispError IO ExitCode
forall a b. (a -> b) -> a -> b
$ FilePath -> IO ExitCode
System.Process.system FilePath
cmd
case ExitCode
result of
ExitCode
ExitSuccess -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> LispVal
Number Integer
0
ExitFailure Int
code -> LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ Integer -> LispVal
Number (Integer -> LispVal) -> Integer -> LispVal
forall a b. (a -> b) -> a -> b
$ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
code
system [LispVal]
err = LispError -> IOThrowsError LispVal
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (LispError -> IOThrowsError LispVal)
-> LispError -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ FilePath -> LispVal -> LispError
TypeMismatch FilePath
"string" (LispVal -> LispError) -> LispVal -> LispError
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List [LispVal]
err
getEnvVars :: [LispVal] -> IOThrowsError LispVal
getEnvVars :: [LispVal] -> IOThrowsError LispVal
getEnvVars [LispVal]
_ = do
[(FilePath, FilePath)]
vars <- IO [(FilePath, FilePath)]
-> ExceptT LispError IO [(FilePath, FilePath)]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [(FilePath, FilePath)]
-> ExceptT LispError IO [(FilePath, FilePath)])
-> IO [(FilePath, FilePath)]
-> ExceptT LispError IO [(FilePath, FilePath)]
forall a b. (a -> b) -> a -> b
$ IO [(FilePath, FilePath)]
SE.getEnvironment
LispVal -> IOThrowsError LispVal
forall (m :: * -> *) a. Monad m => a -> m a
return (LispVal -> IOThrowsError LispVal)
-> LispVal -> IOThrowsError LispVal
forall a b. (a -> b) -> a -> b
$ [LispVal] -> LispVal
List ([LispVal] -> LispVal) -> [LispVal] -> LispVal
forall a b. (a -> b) -> a -> b
$ ((FilePath, FilePath) -> LispVal)
-> [(FilePath, FilePath)] -> [LispVal]
forall a b. (a -> b) -> [a] -> [b]
map (\ (FilePath
k, FilePath
v) -> [LispVal] -> LispVal -> LispVal
DottedList [FilePath -> LispVal
String FilePath
k] (FilePath -> LispVal
String FilePath
v)) [(FilePath, FilePath)]
vars