Interface.hs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. module Gidl.Backend.Tower.Interface where
  2. import Data.Monoid
  3. import Data.List (intercalate, nub)
  4. import Gidl.Types
  5. import Gidl.Interface
  6. import Gidl.Schema
  7. import Gidl.Backend.Ivory.Types
  8. import Gidl.Backend.Ivory.Schema (ifModuleName)
  9. import Ivory.Artifact
  10. import Text.PrettyPrint.Mainland
  11. interfaceModule :: [String] -> Interface -> Artifact
  12. interfaceModule modulepath ir =
  13. artifactPath (intercalate "/" modulepath) $
  14. artifactText (ifModuleName ir ++ ".hs") $
  15. prettyLazyText 80 $
  16. stack
  17. [ text "{-# LANGUAGE DataKinds #-}"
  18. , text "{-# LANGUAGE RankNTypes #-}"
  19. , text "{-# LANGUAGE ScopedTypeVariables #-}"
  20. , text "{-# OPTIONS_GHC -fno-warn-unused-imports #-}"
  21. , empty
  22. , text "module"
  23. <+> im (ifModuleName ir)
  24. <+> text "where"
  25. , empty
  26. , stack $ typeimports ++ extraimports
  27. , empty
  28. ]
  29. where
  30. rootpath = reverse . drop 2 . reverse
  31. modAt path = mconcat (punctuate dot (map text path))
  32. im mname = modAt (modulepath ++ [mname])
  33. tm mname = modAt (rootpath modulepath ++ ["Ivory","Types", mname])
  34. typeimports = map (importDecl tm)
  35. $ nub
  36. $ map importType
  37. $ interfaceTypes ir
  38. extraimports =
  39. [ text "import" <+> modAt (rootpath modulepath ++ ["Ivory", "Types"])
  40. , text "import" <+> im (ifModuleName ir) <> dot <> text "Producer"
  41. , text "import" <+> im (ifModuleName ir) <> dot <> text "Consumer"
  42. , text "import Ivory.Language"
  43. , text "import Ivory.Stdlib"
  44. , text "import Ivory.Tower"
  45. , text "import Ivory.Serialize"
  46. ]