Skip to content

Commit

Permalink
More Monte Monad.
Browse files Browse the repository at this point in the history
  • Loading branch information
MostAwesomeDude committed Feb 19, 2016
1 parent 30953cf commit e450a15
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
13 changes: 0 additions & 13 deletions Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,6 @@ import Masque.Vats
-- ej@(EjectorObj u) <- newEjector
-- catchEjector u $ action ej
--
-- getNames :: Monte (S.Set String)
-- getNames = do
-- env <- view id
-- envs <- use envStack
-- return $ S.unions (map (M.keysSet . _unEnv) (env : NE.toList envs))
--
-- getBinding :: String -> Monte Binding
-- getBinding name = do
-- names <- getNames
-- binding <- preuse $ envStack . traverse . unEnv . ix name
-- let availableNames = S.unions [S.fromList builtins, names]
-- maybe (left $ BadName name availableNames) return binding
--
-- bindToObj :: Binding -> Monte Obj
-- bindToObj (DefBind o) = return o
-- bindToObj (VarBind ref _) = liftIO $ readIORef ref
Expand Down
21 changes: 21 additions & 0 deletions Masque/Monte.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Masque.Monte where
import Control.Lens
import Control.Monad.Trans.Either
import Control.Monad.Trans.State
import Data.Foldable (toList)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Map as M
import qualified Data.Set as S
Expand Down Expand Up @@ -76,3 +77,23 @@ unwrapInt _ = left WrongType
unwrapStr :: Obj -> Monte String
unwrapStr (StrObj s) = return s
unwrapStr _ = left WrongType

namesInScope :: Monte (S.Set String)
namesInScope = do
envs <- use envStack
return $ S.unions (map (M.keysSet . _unEnv) (toList envs))

-- | Lookup a name in the current scope.
maybeLookupName :: String -> Monte (Maybe Binding)
maybeLookupName name = preuse $ envStack . traverse . unEnv . ix name

-- | Lookup a name in the current scope. If not found, transition to an
-- erroring state with a helpful error message.
lookupName :: String -> Monte Binding
lookupName name = do
maybeBinding <- maybeLookupName name
case maybeBinding of
Just binding -> return binding
Nothing -> do
names <- namesInScope
left $ BadName name names
5 changes: 5 additions & 0 deletions Masque/Objects.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{-# LANGUAGE TemplateHaskell #-}

module Masque.Objects where

import Control.Lens
import Data.Foldable (toList)
import Data.IORef
import Data.List
Expand Down Expand Up @@ -48,3 +51,5 @@ instance Show Binding where

newtype Env = Env { _unEnv :: M.Map String Binding }
deriving (Show)

makeLenses ''Env

0 comments on commit e450a15

Please sign in to comment.