Source

xilocaml / Common.hs

Full commit
module Common where
import Control.Monad.State
import qualified Data.Map as Map
import AbsXilocaml

type Ident = String


localDeclIdent :: LocDecl -> Ident
localDeclIdent (LocalVarDecl (OIdent (_, ident)) _) = ident
localDeclIdent (LocalVarDeclExplType (OIdent (_, ident)) _ _) = ident
localDeclIdent (LocalFunDecl (OIdent (_, ident)) _ _) = ident
localDeclIdent (LocalFunDeclExplType (OIdent (_, ident)) _ _ _) = ident


comparator :: (Eq a, Ord a) => OCmp -> a -> a -> Bool
comparator op = case op of
	OEq -> (==)
	ONeq -> (/=)
	OLt -> (<)
	OLeq -> (<=)
	OGt -> (>)
	OGeq -> (>=)


startMapEnv :: Ord k => Map.Map k v
startMapEnv = Map.empty

assignVar :: Ord k => k -> v -> State (Map.Map k v) v
assignVar k v = do
	s <- get
	put $ Map.insert k v s
	return v

lookupVar :: Ord k => k -> State (Map.Map k v) (Maybe v)
lookupVar k = do
	s <- get
	return $ Map.lookup k s