misc cleanup

This commit is contained in:
darkf 2014-02-12 00:50:09 -08:00
parent fe280dca78
commit 13b1671662
1 changed files with 4 additions and 13 deletions

View File

@ -14,8 +14,7 @@ import Control.Monad.Trans.State (StateT, runStateT, evalStateT, get, put)
import System.IO (Handle, hPutStr, hGetLine, hClose, hIsEOF, hSetBuffering, import System.IO (Handle, hPutStr, hGetLine, hClose, hIsEOF, hSetBuffering,
hSetBinaryMode, openBinaryFile, IOMode(..), BufferMode(NoBuffering), stdout, stdin) hSetBinaryMode, openBinaryFile, IOMode(..), BufferMode(NoBuffering), stdout, stdin)
import System.Directory (doesFileExist) import System.Directory (doesFileExist)
import System.FilePath (takeDirectory) import System.FilePath (FilePath, splitExtension, takeBaseName, takeDirectory, (</>))
import System.FilePath (FilePath, splitExtension, takeBaseName, (</>))
import System.Environment (getExecutablePath) import System.Environment (getExecutablePath)
import AST import AST
import Parser (parseProgram) import Parser (parseProgram)
@ -58,10 +57,7 @@ unitv = TupleV []
-- look up a binding from the bottom up -- look up a binding from the bottom up
lookup :: Env -> String -> Maybe Value lookup :: Env -> String -> Maybe Value
lookup [] _ = Nothing lookup [] _ = Nothing
lookup (env:xs) name = lookup (env:xs) name = maybe (lookup xs name) Just (M.lookup name env)
case M.lookup name env of
Nothing -> lookup xs name
Just x -> Just x
-- bind in the local environment -- bind in the local environment
bind :: Env -> String -> Value -> Env bind :: Env -> String -> Value -> Env
@ -286,10 +282,7 @@ eval (Cons a b) = do
ListV v' -> return $ ListV $ a':v' ListV v' -> return $ ListV $ a':v'
_ -> error "cons: RHS must be a list" _ -> error "cons: RHS must be a list"
eval (ListConst v) = eval (ListConst v) = mapM eval v >>= return . ListV
mapM eval v >>= \xs ->
return $ ListV xs
eval (TupleConst v) = mapM eval v >>= return . TupleV eval (TupleConst v) = mapM eval v >>= return . TupleV
eval (IfExpr c t e) = eval c >>= \cond -> eval (IfExpr c t e) = eval c >>= \cond ->
@ -299,9 +292,7 @@ eval (IfExpr c t e) = eval c >>= \cond ->
_ -> error "if: condition must be a boolean" _ -> error "if: condition must be a boolean"
eval (Var var) = get >>= \(_,env) -> eval (Var var) = get >>= \(_,env) ->
case lookup env var of maybe (error $ "unbound variable " ++ var) return (lookup env var)
Just v -> return v
Nothing -> error $ "unbound variable " ++ var
eval (Defun name fn) = do eval (Defun name fn) = do
(s,env) <- get (s,env) <- get