Detect and remove __anon_exprs in repl
[kaleidoscope-hs.git] / Utils.hs
1 {-|
2 Shoving away gross stuff into this one module.
3 -}
4 module Utils where
5
6 import Control.Monad.Trans.State
7 import Data.ByteString.Short (ShortByteString)
8 import Data.Functor.Identity
9 import Data.List
10 import LLVM.AST
11 import LLVM.IRBuilder.Module
12 import LLVM.IRBuilder.Internal.SnocList
13
14 moduleSoFar :: MonadModuleBuilder m => ShortByteString -> m Module
15 moduleSoFar nm = do
16   s <- liftModuleState get
17   let ds = getSnocList (builderDefs s)
18   return $ defaultModule { moduleName = nm, moduleDefinitions = ds }
19
20 removeDef :: MonadModuleBuilder m => Definition -> m ()
21 removeDef def = liftModuleState (modify update)
22   where
23     update (ModuleBuilderState defs typeDefs) =
24       let newDefs = SnocList (delete def (getSnocList defs))
25       in ModuleBuilderState newDefs typeDefs
26
27 mostRecentDef :: Monad m => ModuleBuilderT m Definition
28 mostRecentDef = last . getSnocList . builderDefs <$> liftModuleState get
29
30 hoist :: Monad m => ModuleBuilder a -> ModuleBuilderT m a
31 hoist m = ModuleBuilderT $ StateT $
32   return . runIdentity . runStateT (unModuleBuilderT m)