@@ -3,6 +3,7 @@
3{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE UndecidableInstances #-}
4{-# LANGUAGE UndecidableInstances #-}
5{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE FlexibleInstances #-}
6module Language.Haskell.Generate.Monad
7module Language.Haskell.Generate.Monad
7 ( Generate(..), ExpG
8 ( Generate(..), ExpG
8 , runGenerate, newName
9 , runGenerate, newName
@@ -35,6 +36,8 @@ import Language.Haskell.Exts.SrcLoc
35import Language.Haskell.Exts.Syntax
36import Language.Haskell.Exts.Syntax
36import Language.Haskell.Generate.Expression
37import Language.Haskell.Generate.Expression
38--------------------------------------------------------------------------------
41--------------------------------------------------------------------------------
39-- Generate expressions
42-- Generate expressions
@@ -61,7 +64,13 @@ generateExp = prettyPrint . runExpression . fst . runGenerate
61caseE :: ExpG x -> [(Pat, ExpG t)] -> ExpG t
64caseE :: ExpG x -> [(Pat, ExpG t)] -> ExpG t
62caseE v alt = do
65caseE v alt = do
67+#if MIN_VERSION_haskell_src_exts(1,17,0)
68+ alt' <- mapM (\(p,a) -> fmap (\a' -> Alt noLoc p (UnGuardedRhs $ runExpression a') Nothing) a) alt
69+#elif MIN_VERSION_haskell_src_exts(1,16,0)
70+ alt' <- mapM (\(p,a) -> fmap (\a' -> Alt noLoc p (UnGuardedRhs $ runExpression a') (BDecls [])) a) alt
64 alt' <- mapM (\(p,a) -> fmap (\a' -> Alt noLoc p (UnGuardedAlt $ runExpression a') (BDecls [])) a) alt
72 alt' <- mapM (\(p,a) -> fmap (\a' -> Alt noLoc p (UnGuardedAlt $ runExpression a') (BDecls [])) a) alt
65 return $ Expression $ Case (runExpression v') alt'
74 return $ Expression $ Case (runExpression v') alt'
67-- | Import a function from a module. This function is polymorphic in the type of the resulting expression,
76-- | Import a function from a module. This function is polymorphic in the type of the resulting expression,
@@ -205,19 +214,31 @@ instance GenExp (FunRef t) where
206-- | Generate a ExportSpec for a given function item.
215-- | Generate a ExportSpec for a given function item.
207exportFun :: FunRef t -> ExportSpec
216exportFun :: FunRef t -> ExportSpec
217+#if MIN_VERSION_haskell_src_exts(1,16,0) && !MIN_VERSION_haskell_src_exts(1,17,0)
218+exportFun (FunRef name) = EVar NoNamespace (UnQual name)
208exportFun (FunRef name) = EVar (UnQual name)
220exportFun (FunRef name) = EVar (UnQual name)
210-- | Add a declaration to the module. Return a reference to it that can be used to either apply the function to some values or export it.
223-- | Add a declaration to the module. Return a reference to it that can be used to either apply the function to some values or export it.
211addDecl :: Name -> ExpG t -> ModuleM (FunRef t)
224addDecl :: Name -> ExpG t -> ModuleM (FunRef t)
212addDecl name e = ModuleM $ do
225addDecl name e = ModuleM $ do
213 let (body, mods) = runGenerate e
226 let (body, mods) = runGenerate e
227+#if MIN_VERSION_haskell_src_exts(1,17,0)
228+ tell (mods, [FunBind [Match noLoc name [] Nothing (UnGuardedRhs $ runExpression body) Nothing]])
214 tell (mods, [FunBind [Match noLoc name [] Nothing (UnGuardedRhs $ runExpression body) $ BDecls []]])
230 tell (mods, [FunBind [Match noLoc name [] Nothing (UnGuardedRhs $ runExpression body) $ BDecls []]])
215 return $ FunRef name
232 return $ FunRef name
217-- | Extract the Module from a module generator.
234-- | Extract the Module from a module generator.
218runModuleM :: ModuleG -> String -> Module
235runModuleM :: ModuleG -> String -> Module
219runModuleM (ModuleM act) name =
236runModuleM (ModuleM act) name =
237+#if MIN_VERSION_haskell_src_exts(1,16,0)
238+ Module noLoc (ModuleName name) [] Nothing export (map (\md -> ImportDecl noLoc md True False False Nothing Nothing Nothing) $ S.toList imps) decls
220 Module noLoc (ModuleName name) [] Nothing export (map (\md -> ImportDecl noLoc md True False Nothing Nothing Nothing) $ S.toList imps) decls
240 Module noLoc (ModuleName name) [] Nothing export (map (\md -> ImportDecl noLoc md True False Nothing Nothing Nothing) $ S.toList imps) decls
221 where (export, (imps, decls)) = runWriter act
242 where (export, (imps, decls)) = runWriter act
223-- | Generate the source code for a module.
244-- | Generate the source code for a module.