module Language.Scheme.Libraries
(
findModuleFile
, moduleImport
) where
import qualified Paths_husk_scheme as PHS (getDataFileName)
import Language.Scheme.Types
import Language.Scheme.Util
import Language.Scheme.Variables
import Control.Monad.Error
findModuleFile
:: [LispVal]
-> IOThrowsError LispVal
findModuleFile [String file]
| file == "r5rs/base.sld" ||
file == "scheme/base.sld" ||
file == "scheme/write.sld" = do
path <- liftIO $ PHS.getDataFileName $ "lib/" ++ file
return $ String path
| otherwise = return $ String file
findModuleFile _ = return $ Bool False
moduleImport
:: Env
-> Env
-> [LispVal]
-> IOThrowsError LispVal
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 from [] = do
return $ LispEnv to
divertBinding
:: Env
-> Env
-> String
-> String
-> IOThrowsError LispVal
divertBinding to from nameOrig nameNew = do
isMacroBound <- liftIO $ isNamespacedRecBound from macroNamespace nameOrig
namespace <- liftIO $ case isMacroBound of
True -> return macroNamespace
_ -> return varNamespace
m <- getNamespacedVar from namespace nameOrig
defineNamespacedVar to namespace nameNew m