Interface.hs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. module Gidl.Interface
  2. ( module Gidl.Interface.AST
  3. , InterfaceDescr
  4. , InterfaceRepr(..)
  5. , interfaceDescrToRepr
  6. , lookupInterface
  7. , insertInterface
  8. , interfaceParents
  9. , interfaceTypes
  10. , interfaceMethods
  11. ) where
  12. import Data.List (nub)
  13. import Data.Maybe (fromJust)
  14. import Gidl.Interface.AST
  15. import Gidl.Types
  16. type InterfaceDescr = Interface InterfaceName TypeName
  17. lookupInterface :: InterfaceName -> InterfaceEnv -> Maybe InterfaceDescr
  18. lookupInterface iname (InterfaceEnv ie) = lookup iname ie
  19. insertInterface :: InterfaceName -> InterfaceDescr -> InterfaceEnv -> InterfaceEnv
  20. insertInterface iname i e@(InterfaceEnv ie) = case lookupInterface iname e of
  21. Nothing -> InterfaceEnv ((iname,i):ie)
  22. Just _ -> error ("insertInterface invariant broken: interface " ++ iname ++ "already exists")
  23. interfaceParents :: Interface i t -> [i]
  24. interfaceParents (Interface parents _) = parents
  25. interfaceTypes :: InterfaceRepr -> [TypeRepr]
  26. interfaceTypes ir = nub (map (methodT . snd) ms)
  27. where
  28. ms = interfaceMethods ir
  29. methodT :: Method TypeRepr -> TypeRepr
  30. methodT (AttrMethod _ tr) = tr
  31. methodT (StreamMethod _ tr) = tr
  32. data InterfaceRepr = InterfaceRepr InterfaceName (Interface InterfaceRepr TypeRepr)
  33. deriving (Eq, Show)
  34. interfaceDescrToRepr :: InterfaceName -> InterfaceEnv -> TypeEnv -> InterfaceRepr
  35. interfaceDescrToRepr iname ie te = InterfaceRepr iname ir
  36. where
  37. ir = case fromJust $ lookupInterface iname ie of
  38. Interface is ms -> Interface (map recur is)
  39. [ (mn, methodDescrToRepr te md) | (mn, md) <- ms ]
  40. recur i = interfaceDescrToRepr i ie te
  41. methodDescrToRepr :: TypeEnv -> Method TypeName -> Method TypeRepr
  42. methodDescrToRepr te (AttrMethod p tn) = AttrMethod p (typeDescrToRepr tn te)
  43. methodDescrToRepr te (StreamMethod r tn) = StreamMethod r (typeDescrToRepr tn te)
  44. interfaceMethods :: InterfaceRepr -> [(MethodName, Method TypeRepr)]
  45. interfaceMethods ir = ms ++ concatMap interfaceMethods ps
  46. where
  47. (InterfaceRepr _ (Interface ps ms)) = ir