@@ -1,4 +1,14 @@
1−module AST.Module where
1+module AST.Module
3+ , Types, Aliases, ADTs
4+ , AdtInfo, CanonicalAdt
5+ , SourceModule, ValidModule, CanonicalModule
6+ , Module(..), CanonicalBody(..)
7+ , HeaderAndImports(..)
8+ , Name, nameToString, nameIsNative
9+ , Interface(..), toInterface
3import Data.Binary
13import Data.Binary
4import qualified Data.List as List
14import qualified Data.List as List
@@ -65,10 +75,14 @@ data HeaderAndImports = HeaderAndImports
65 , _imports :: [(Name, ImportMethod)]
75 , _imports :: [(Name, ImportMethod)]
68type Name = [String] -- must be non-empty
79type Name = [String] -- must be non-empty
70nameToString :: Name -> String
82nameToString :: Name -> String
71−nameToString = List.intercalate "."
84+ List.intercalate "."
73nameIsNative :: Name -> Bool
87nameIsNative :: Name -> Bool
74nameIsNative name =
88nameIsNative name =
@@ -121,27 +135,19 @@ instance Binary Interface where
122-- IMPORT METHOD
136-- IMPORT METHOD
124−data ImportMethod
138+data ImportMethod = ImportMethod
125− = As !String
139+ { alias :: Maybe String
126− | Open !(Var.Listing Var.Value)
140+ , exposedVars :: !(Var.Listing Var.Value)
129−open = Open (Var.openListing)
131−importing :: [Var.Value] -> ImportMethod
132−importing xs = Open (Var.Listing xs False)
134instance Binary ImportMethod where
144instance Binary ImportMethod where
135− put method =
145+ put (ImportMethod alias exposedVars) =
137− As alias -> putWord8 0 >> put alias
146+ do put alias
138− Open listing -> putWord8 1 >> put listing
147+ put exposedVars
140− get = do tag <- getWord8
149+ get =
141− case tag of
150+ ImportMethod <$> get <*> get
144− _ -> error "Error reading valid ImportMethod type from serialized string"
147-- PRETTY PRINTING
153-- PRETTY PRINTING
@@ -150,19 +156,22 @@ instance (Pretty exs, Pretty body) => Pretty (Module exs body) where
150 pretty (Module names _ exs ims body) =
156 pretty (Module names _ exs ims body) =
151 P.vcat [modul, P.text "", prettyImports, P.text "", pretty body]
157 P.vcat [modul, P.text "", prettyImports, P.text "", pretty body]
153− modul = P.text "module" <+> name <+> pretty exs <+> P.text "where"
160+ P.text "module" <+> name <+> pretty exs <+> P.text "where"
154− name = P.text (List.intercalate "." names)
161+
163+ P.text (nameToString names)
156 prettyImports =
165 prettyImports =
157− P.vcat $ map prettyMethod ims
166+ P.vcat $ map prettyMethod ims
160prettyMethod :: (Name, ImportMethod) -> Doc
169prettyMethod :: (Name, ImportMethod) -> Doc
161−prettyMethod import' =
170+prettyMethod (name, ImportMethod maybeAlias exposedVars) =
162− case import' of
172+ case maybeAlias of
164− | name == alias -> P.empty
173+ Nothing -> P.empty
166− (_, As alias) -> P.text "as" <+> P.text alias
175+ P.text "as" <+> P.text alias
168− (_, Open listing) -> pretty listing
177+ prettyAlias <+> pretty exposedVars