-- State Monad for the Yhc Core Erlang Backend. module Yhc.Core.BackEnd.Erlang.EGMonad ( EG (..), EGM, getCnt, storeCVar, checkCVar, currFName, module Control.Monad.State) where import Data.Maybe import Yhc.Core.Extra import qualified Data.Map as M import qualified Data.Set as S import Control.Monad import Control.Monad.State data EG = EG { stateCnt :: Int -- counter to generate unique names ,currFun :: CoreFunc -- current function being compiled ,caseVars :: S.Set CoreVarName -- set of case variable names ,funcMap :: M.Map CoreFuncName CoreFuncName -- map of function names (Yhc -> Erlang) ,strctMap :: M.Map CoreFuncName [Bool] -- strictness map ,expFunc :: [CoreFuncName] -- list of exported functions ,coreRef :: Core -- reference to core ,coreAnno :: CoreAnnotations -- annotations for this core ,emodName :: String -- Erlang module name } getCnt :: EGM Int getCnt = do c <- gets stateCnt st <- get put st {stateCnt = c + 1} return c type EGM a = State EG a currFName :: EGM CoreFuncName currFName = gets currFun >>= return . coreFuncName storeCVar :: CoreVarName -> EGM () storeCVar cvn = do cfn <- currFName cvset <- gets caseVars let fvn = cfn ++ "::" ++ cvn st <- get put st {caseVars = fvn `S.insert` cvset} checkCVar :: CoreVarName -> EGM Bool checkCVar cvn = do cfn <- currFName cvset <- gets caseVars let fvn = cfn ++ "::" ++ cvn return $ S.member fvn cvset