-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tdfirth
committed
Jul 9, 2017
1 parent
4fe8f0d
commit e60d797
Showing
3 changed files
with
49 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,49 @@ | ||
{-# LANGUAGE MultiParamTypeClasses, TypeFamilies, FlexibleInstances #-} | ||
module Lambda.Environment where | ||
|
||
import Lambda.Types | ||
|
||
import qualified Data.Map.Strict as Map | ||
import Data.IORef | ||
import Control.Monad.Except | ||
import System.IO | ||
import Lambda.Types hiding (Env, isBound, getVar, setVar, defineVar, bindVars) | ||
|
||
type Environment = IORef (Map.Map String (IORef LispVal)) | ||
|
||
nullEnv :: IO Environment | ||
nullEnv = newIORef Map.empty | ||
|
||
isBound :: Environment -> String -> IO Bool | ||
isBound e s = readIORef e >>= return . Map.member s | ||
|
||
getVar :: Environment -> String -> IOThrowsError LispVal | ||
getVar e s = do | ||
env <- liftIO $ readIORef e | ||
case Map.lookup s env of | ||
Just v -> liftIO $ readIORef v | ||
Nothing -> throwError $ UnboundVar "Trying to get an unbound variable..." s | ||
|
||
updateVar :: Environment -> String -> LispVal -> IOThrowsError LispVal | ||
updateVar e s l = do | ||
env <- liftIO $ readIORef e | ||
case Map.lookup s env of | ||
Just v -> liftIO $ writeIORef v l | ||
Nothing -> throwError $ UnboundVar "Trying to update an unbound variable.." s | ||
return l | ||
|
||
defineVar :: Environment -> String -> LispVal -> IOThrowsError LispVal | ||
defineVar e s l = do | ||
defined <- liftIO $ isBound e s | ||
if defined then updateVar e s l | ||
else liftIO $ do | ||
newVar <- newIORef l | ||
modifyIORef' e $ Map.insert s newVar | ||
return l | ||
|
||
foo :: Int | ||
foo = 1 | ||
bindVars :: Environment -> [(String, LispVal)] -> IO Environment | ||
bindVars e ls = do | ||
updates <- mapM mkRef ls >>= return . Map.fromList | ||
modifyIORef' e (Map.union updates) | ||
return e | ||
where | ||
mkRef :: (String, LispVal) -> IO (String, IORef LispVal) | ||
mkRef (s, l) = newIORef l >>= return . (,) s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters