Generate the module inside the 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 LLVM.AST
10 import LLVM.IRBuilder.Module
11 import LLVM.IRBuilder.Internal.SnocList
12
13 moduleSoFar :: MonadModuleBuilder m => ShortByteString -> m Module
14 moduleSoFar nm = do
15   s <- liftModuleState get
16   let ds = getSnocList (builderDefs s)
17   return $ defaultModule { moduleName = nm, moduleDefinitions = ds }
18
19 mostRecentDef :: Monad m => ModuleBuilderT m Definition
20 mostRecentDef = last . getSnocList . builderDefs <$> liftModuleState get
21
22 hoist :: Monad m => ModuleBuilder a -> ModuleBuilderT m a
23 hoist m = ModuleBuilderT $ StateT $
24   return . runIdentity . runStateT (unModuleBuilderT m)