Interface.hs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. module Gidl.Backend.Haskell.Interface where
  2. import Data.Monoid
  3. import Data.List (intercalate, nub)
  4. import Data.Char (toUpper)
  5. import Gidl.Types
  6. import Gidl.Interface
  7. import Gidl.Backend.Haskell.Types
  8. import Ivory.Artifact
  9. import Text.PrettyPrint.Mainland
  10. interfaceModule :: [String] -> InterfaceRepr -> Artifact
  11. interfaceModule modulepath ir@(InterfaceRepr iname i) =
  12. artifactPath (intercalate "/" modulepath) $
  13. artifactText ((ifModuleName ir) ++ ".hs") $
  14. prettyLazyText 80 $
  15. stack
  16. [ text "module"
  17. <+> im (ifModuleName ir)
  18. <+> text "where"
  19. , empty
  20. , stack [ text "import" <+> im (ifModuleName iir)
  21. | iir <- interfaceParents i
  22. ]
  23. , stack $ map (importDecl tm)
  24. $ nub
  25. $ map importType
  26. $ interfaceTypes ir
  27. ]
  28. where
  29. im mname = mconcat $ punctuate dot
  30. $ map text (modulepath ++ [mname])
  31. tm mname = mconcat $ punctuate dot
  32. $ map text (typepath modulepath ++ ["Types", mname])
  33. where typepath = reverse . drop 1 . reverse
  34. ifModuleName :: InterfaceRepr -> String
  35. ifModuleName (InterfaceRepr iname _) = aux iname
  36. where
  37. aux :: String -> String
  38. aux = first_cap . u_to_camel
  39. first_cap (s:ss) = (toUpper s) : ss
  40. first_cap [] = []
  41. u_to_camel ('_':'i':[]) = []
  42. u_to_camel ('_':[]) = []
  43. u_to_camel ('_':a:as) = (toUpper a) : u_to_camel as
  44. u_to_camel (a:as) = a : u_to_camel as
  45. u_to_camel [] = []