module Language.Scheme.Variables
(
printEnv
, exportsFromEnv
, copyEnv
, extendEnv
, importEnv
, topmostEnv
, nullEnvWithParent
, findNamespacedEnv
, macroNamespace
, varNamespace
, getVar
, getNamespacedVar
, defineVar
, defineNamespacedVar
, setVar
, setNamespacedVar
, updateObject
, updateNamespacedObject
, isBound
, isRecBound
, isNamespacedBound
, isNamespacedRecBound
, derefPtr
, recDerefPtrs
) where
import Language.Scheme.Types
import Control.Monad.Error
import Data.Array
import Data.IORef
import qualified Data.Map
macroNamespace :: [Char]
macroNamespace = "m"
varNamespace :: [Char]
varNamespace = "v"
getVarName :: String -> String -> String
getVarName namespace name = namespace ++ "_" ++ name
printEnv :: Env
-> IO String
printEnv env = do
binds <- liftIO $ readIORef $ bindings env
l <- mapM showVar $ Data.Map.toList binds
return $ unlines l
where
showVar (name, val) = do
v <- liftIO $ readIORef val
return $ "[" ++ name ++ "]" ++ ": " ++ show v
exportsFromEnv :: Env
-> IO [LispVal]
exportsFromEnv env = do
binds <- liftIO $ readIORef $ bindings env
return $ getExports [] $ fst $ unzip $ Data.Map.toList binds
where
getExports acc (('m':'_':b) : bs) = getExports (Atom b:acc) bs
getExports acc (('v':'_':b) : bs) = getExports (Atom b:acc) bs
getExports acc (_ : bs) = getExports acc bs
getExports acc [] = acc
copyEnv :: Env
-> IO Env
copyEnv env = do
ptrs <- liftIO $ readIORef $ pointers env
ptrList <- newIORef ptrs
binds <- liftIO $ readIORef $ bindings env
bindingListT <- mapM addBinding $ Data.Map.toList binds
bindingList <- newIORef $ Data.Map.fromList bindingListT
return $ Environment (parentEnv env) bindingList ptrList
where addBinding (name, val) = do
x <- liftIO $ readIORef val
ref <- newIORef x
return (name, ref)
importEnv
:: Env
-> Env
-> IO Env
importEnv dEnv sEnv = do
sPtrs <- liftIO $ readIORef $ pointers sEnv
dPtrs <- liftIO $ readIORef $ pointers dEnv
writeIORef (pointers dEnv) $ Data.Map.union sPtrs dPtrs
sBinds <- liftIO $ readIORef $ bindings sEnv
dBinds <- liftIO $ readIORef $ bindings dEnv
writeIORef (bindings dEnv) $ Data.Map.union sBinds dBinds
case parentEnv sEnv of
Just ps -> importEnv dEnv ps
Nothing -> return dEnv
extendEnv :: Env
-> [((String, String), LispVal)]
-> IO Env
extendEnv envRef abindings = do
bindinglistT <- (mapM addBinding abindings)
bindinglist <- newIORef $ Data.Map.fromList bindinglistT
nullPointers <- newIORef $ Data.Map.fromList []
return $ Environment (Just envRef) bindinglist nullPointers
where addBinding ((namespace, name), val) = do ref <- newIORef val
return (getVarName namespace name, ref)
topmostEnv :: Env -> IO Env
topmostEnv envRef = do
case parentEnv envRef of
Just p -> topmostEnv p
Nothing -> return envRef
nullEnvWithParent :: Env -> IO Env
nullEnvWithParent p = do
Environment _ binds ptrs <- nullEnv
return $ Environment (Just p) binds ptrs
findNamespacedEnv
:: Env
-> String
-> String
-> IO (Maybe Env)
findNamespacedEnv envRef namespace var = do
found <- liftIO $ isNamespacedBound envRef namespace var
if found
then return (Just envRef)
else case parentEnv envRef of
(Just par) -> findNamespacedEnv par namespace var
Nothing -> return Nothing
isBound :: Env
-> String
-> IO Bool
isBound envRef var = isNamespacedBound envRef varNamespace var
isRecBound :: Env
-> String
-> IO Bool
isRecBound envRef var = isNamespacedRecBound envRef varNamespace var
isNamespacedBound
:: Env
-> String
-> String
-> IO Bool
isNamespacedBound envRef namespace var =
(readIORef $ bindings envRef) >>= return . Data.Map.member (getVarName namespace var)
isNamespacedRecBound
:: Env
-> String
-> String
-> IO Bool
isNamespacedRecBound envRef namespace var = do
env <- findNamespacedEnv envRef namespace var
case env of
(Just e) -> isNamespacedBound e namespace var
Nothing -> return False
getVar :: Env
-> String
-> IOThrowsError LispVal
getVar envRef var = getNamespacedVar envRef varNamespace var
getNamespacedVar :: Env
-> String
-> String
-> IOThrowsError LispVal
getNamespacedVar envRef
namespace
var = do binds <- liftIO $ readIORef $ bindings envRef
case Data.Map.lookup (getVarName namespace var) binds of
(Just a) -> liftIO $ readIORef a
Nothing -> case parentEnv envRef of
(Just par) -> getNamespacedVar par namespace var
Nothing -> (throwError $ UnboundVar "Getting an unbound variable" var)
setVar
:: Env
-> String
-> LispVal
-> IOThrowsError LispVal
setVar envRef var value = setNamespacedVar envRef varNamespace var value
setNamespacedVar
:: Env
-> String
-> String
-> LispVal
-> IOThrowsError LispVal
setNamespacedVar envRef
namespace
var value = do
case value of
Pointer p env -> do
if p == var
then return value
else next
_ -> next
where
next = do
_ <- updatePointers envRef namespace var
_setNamespacedVar envRef namespace var value
_setNamespacedVar
:: Env
-> String
-> String
-> LispVal
-> IOThrowsError LispVal
_setNamespacedVar envRef
namespace
var value = do
valueToStore <- getValueToStore namespace var envRef value
_setNamespacedVarDirect envRef namespace var valueToStore
_setNamespacedVarDirect envRef
namespace
var valueToStore = do
env <- liftIO $ readIORef $ bindings envRef
case Data.Map.lookup (getVarName namespace var) env of
(Just a) -> do
liftIO $ writeIORef a valueToStore
return valueToStore
Nothing -> case parentEnv envRef of
(Just par) -> _setNamespacedVarDirect par namespace var valueToStore
Nothing -> throwError $ UnboundVar "Setting an unbound variable: " var
updatePointers :: Env -> String -> String -> IOThrowsError LispVal
updatePointers envRef namespace var = do
ptrs <- liftIO $ readIORef $ pointers envRef
case Data.Map.lookup (getVarName namespace var) ptrs of
(Just valIORef) -> do
val <- liftIO $ readIORef valIORef
case val of
(Pointer pVar pEnv : ps) -> do
liftIO $ writeIORef valIORef []
movePointers pEnv namespace pVar ps
pointToNewVar pEnv namespace pVar ps
existingValue <- getNamespacedVar envRef namespace var
_setNamespacedVar pEnv namespace pVar existingValue
[] -> return $ Nil ""
_ -> throwError $ InternalError
"non-pointer value found in updatePointers"
Nothing -> return $ Nil ""
where
movePointers :: Env -> String -> String -> [LispVal] -> IOThrowsError LispVal
movePointers envRef namespace var ptrs = do
env <- liftIO $ readIORef $ pointers envRef
case Data.Map.lookup (getVarName namespace var) env of
Just ps' -> do
ps <- liftIO $ readIORef ps'
liftIO $ writeIORef ps' $ ps ++ ptrs
return $ Nil ""
Nothing -> do
valueRef <- liftIO $ newIORef ptrs
liftIO $ writeIORef (pointers envRef) (Data.Map.insert (getVarName namespace var) valueRef env)
return $ Nil ""
pointToNewVar pEnv namespace pVar (Pointer v e : ps) = do
_ <- _setNamespacedVarDirect e namespace v (Pointer pVar pEnv)
pointToNewVar pEnv namespace pVar ps
pointToNewVar pEnv namespace pVar [] = return $ Nil ""
pointToNewVar pEnv namespace pVar _ = throwError $ InternalError "pointToNewVar"
updateObject :: Env -> String -> LispVal -> IOThrowsError LispVal
updateObject env var value =
updateNamespacedObject env varNamespace var value
updateNamespacedObject :: Env -> String -> String -> LispVal -> IOThrowsError LispVal
updateNamespacedObject env namespace var value = do
varContents <- getNamespacedVar env namespace var
obj <- findPointerTo varContents
case obj of
Pointer pVar pEnv -> do
_setNamespacedVar pEnv namespace pVar value
_ -> _setNamespacedVar env namespace var value
defineVar
:: Env
-> String
-> LispVal
-> IOThrowsError LispVal
defineVar envRef var value = defineNamespacedVar envRef varNamespace var value
defineNamespacedVar
:: Env
-> String
-> String
-> LispVal
-> IOThrowsError LispVal
defineNamespacedVar envRef
namespace
var value = do
alreadyDefined <- liftIO $ isNamespacedBound envRef namespace var
if alreadyDefined
then setNamespacedVar envRef namespace var value >> return value
else do
valueToStore <- getValueToStore namespace var envRef value
liftIO $ do
valueRef <- newIORef valueToStore
env <- readIORef $ bindings envRef
writeIORef (bindings envRef) (Data.Map.insert (getVarName namespace var) valueRef env)
return valueToStore
getValueToStore :: String -> String -> Env -> LispVal -> IOThrowsError LispVal
getValueToStore namespace var env (Pointer p pEnv) = do
addReversePointer namespace p pEnv namespace var env
getValueToStore _ _ _ value = return value
addReversePointer :: String -> String -> Env -> String -> String -> Env -> IOThrowsError LispVal
addReversePointer namespace var envRef ptrNamespace ptrVar ptrEnvRef = do
env <- liftIO $ readIORef $ bindings envRef
case Data.Map.lookup (getVarName namespace var) env of
(Just a) -> do
v <- liftIO $ readIORef a
if isObject v
then do
ptrs <- liftIO $ readIORef $ pointers envRef
case Data.Map.lookup (getVarName namespace var) ptrs of
(Just valueRef) -> liftIO $ do
value <- readIORef valueRef
writeIORef valueRef (value ++ [Pointer ptrVar ptrEnvRef])
return $ Pointer var envRef
Nothing -> liftIO $ do
valueRef <- newIORef [Pointer ptrVar ptrEnvRef]
writeIORef (pointers envRef) (Data.Map.insert (getVarName namespace var) valueRef ptrs)
return $ Pointer var envRef
else return v
Nothing -> case parentEnv envRef of
(Just par) -> addReversePointer namespace var par ptrNamespace ptrVar ptrEnvRef
Nothing -> throwError $ UnboundVar "Getting an unbound variable: " var
derefPtr :: LispVal -> IOThrowsError LispVal
derefPtr (Pointer p env) = do
result <- getVar env p
derefPtr result
derefPtr v = return v
recDerefPtrs :: LispVal -> IOThrowsError LispVal
#ifdef UsePointers
recDerefPtrs (List l) = do
result <- mapM recDerefPtrs l
return $ List result
recDerefPtrs (DottedList ls l) = do
ds <- mapM recDerefPtrs ls
d <- recDerefPtrs l
return $ DottedList ds d
recDerefPtrs (Vector v) = do
let vs = elems v
ds <- mapM recDerefPtrs vs
return $ Vector $ listArray (0, length vs 1) ds
recDerefPtrs (HashTable ht) = do
ks <- mapM recDerefPtrs $ map (\ (k, _) -> k) $ Data.Map.toList ht
vs <- mapM recDerefPtrs $ map (\ (_, v) -> v) $ Data.Map.toList ht
return $ HashTable $ Data.Map.fromList $ zip ks vs
#endif
recDerefPtrs (Pointer p env) = do
result <- getVar env p
recDerefPtrs result
recDerefPtrs v = return v
isObject :: LispVal -> Bool
isObject (List _) = True
isObject (DottedList _ _) = True
isObject (String _) = True
isObject (Vector _) = True
isObject (HashTable _) = True
isObject (Pointer _ _) = True
isObject _ = False
findPointerTo :: LispVal -> IOThrowsError LispVal
findPointerTo ptr@(Pointer p env) = do
result <- getVar env p
case result of
(Pointer _ _) -> findPointerTo result
_ -> return ptr
findPointerTo v = return v