module Language.Scheme.Libraries
(
findModuleFile
, moduleImport
) where
import Language.Scheme.Types
import Language.Scheme.Variables
import Control.Monad.Error
findModuleFile
:: [LispVal]
-> IOThrowsError LispVal
findModuleFile [p@(Pointer _ _)] = recDerefPtrs p >>= box >>= findModuleFile
findModuleFile [String file] = do
return $ String file
findModuleFile _ = return $ Bool False
moduleImport
:: Env
-> Env
-> [LispVal]
-> IOThrowsError LispVal
moduleImport to from (p@(Pointer _ _) : is) = do
i <- derefPtr p
moduleImport to from (i : is)
moduleImport to from (Atom i : is) = do
_ <- divertBinding to from i i
moduleImport to from is
moduleImport to from (DottedList [Atom iRenamed] (Atom iOrig) : is) = do
_ <- divertBinding to from iOrig iRenamed
moduleImport to from is
moduleImport to _ [] = do
return $ LispEnv to
moduleImport _ _ err = do
throwError $ Default $ "Unexpected argument to moduleImport: " ++ show err
divertBinding
:: Env
-> Env
-> String
-> String
-> IOThrowsError LispVal
divertBinding to from nameOrig nameNew = do
isMacroBound <- liftIO $ isNamespacedRecBound from macroNamespace nameOrig
namespace <- liftIO $ if isMacroBound then return macroNamespace
else return varNamespace
m <- getNamespacedVar from namespace nameOrig
defineNamespacedVar to namespace nameNew m