on master help sign in
Merge pull request #486 from JoeyEremondi/dev
3de2023Evan Czaplicki
compiler/SourceSyntax/Module.hs+29
@@ -4,12 +4,16 @@ module SourceSyntax.Module where
4import Data.Binary
4import Data.Binary
5import qualified Data.Map as Map
5import qualified Data.Map as Map
6import Control.Applicative ((<$>), (<*>))
6import Control.Applicative ((<$>), (<*>))
7+import Text.PrettyPrint as P
7
8
8import SourceSyntax.Expression (LExpr)
9import SourceSyntax.Expression (LExpr)
9import SourceSyntax.Declaration
10import SourceSyntax.Declaration
10import SourceSyntax.Type
11import SourceSyntax.Type
12+import Data.List (intercalate)
11
13
12import qualified Elm.Internal.Version as Version
14import qualified Elm.Internal.Version as Version
15+import SourceSyntax.PrettyPrint
16+
13
17
14data Module def =
18data Module def =
15 Module [String] Exports Imports [def]
19 Module [String] Exports Imports [def]
@@ -21,6 +25,31 @@ type Imports = [(String, ImportMethod)]
21data ImportMethod = As String | Importing [String] | Hiding [String]
25data ImportMethod = As String | Importing [String] | Hiding [String]
22 deriving (Eq, Ord, Show)
26 deriving (Eq, Ord, Show)
23
27
28+instance (Pretty def ) => Pretty (Module def) where
29+ pretty (Module modNames exportList importList decs) =
30+ let
31+ exportPret = case exportList of
32+ [] -> P.text " "
33+ _ -> P.parens $ commaCat $ map P.text exportList
34+
35+ decPret = P.sep $ map pretty decs
36+ modName = P.text $ intercalate "." modNames
37+ modPret = (P.text "module" <+> modName <+> exportPret <+> P.text "where")
38+
39+
40+ importPret = P.vcat $ map prettyImport importList
41+
42+ prettyImport (name, method) =
43+ case method of
44+ As s -> if name == s
45+ then P.text $ "import " ++ name
46+ else P.text $ "import " ++ name ++ " as " ++ s
47+ Importing strs -> (P.text $ "import " ++ name ++ " ") <+> (commaCat $ map P.text strs)
48+ Hiding [] -> (P.text $ "import open " ++ name ++ " ")
49+ Hiding strs -> (P.text $ "import open " ++ name ++ " ") <+> (commaCat $ map P.text strs)
50+
51+ in P.sep [modPret, importPret, decPret]
52+
24instance Binary ImportMethod where
53instance Binary ImportMethod where
25 put method =
54 put method =
26 let put' n info = putWord8 n >> put info in
55 let put' n info = putWord8 n >> put info in