on 32c78ef help sign in
elk/compiler/SourceSyntax/Declaration.hs
57 lines 1.9 kB history raw
1module SourceSyntax.Declaration where
3import qualified SourceSyntax.Expression as Expr
4import SourceSyntax.Type
5import SourceSyntax.PrettyPrint
6import Text.PrettyPrint as P
8data Declaration tipe var
9 = Definition (Expr.Def tipe var)
10 | Datatype String [String] [(String,[Type])]
11 | TypeAlias String [String] Type [Deriveable]
12 | ImportEvent String (Expr.LExpr tipe var) String Type
13 | ExportEvent String String Type
14 | Fixity Assoc Int String
15 deriving (Eq, Show)
17data Assoc = L | N | R
18 deriving (Eq)
20data Deriveable = Json | Binary | RecordConstructor
21 deriving (Eq, Show)
23instance Show Assoc where
24 show assoc =
25 case assoc of
26 L -> "left"
27 N -> "non"
28 R -> "right"
30instance Pretty (Declaration t v) where
31 pretty decl =
32 case decl of
33 Definition def -> pretty def
35 Datatype tipe tvars ctors ->
36 P.hang (P.text "data" <+> P.text tipe <+> P.hsep (map P.text tvars)) 4
37 (P.sep $ zipWith join ("=" : repeat "|") ctors)
38 where
39 join c ctor = P.text c <+> prettyCtor ctor
40 prettyCtor (name, tipes) =
41 P.hang (P.text name) 2 (P.sep (map prettyParens tipes))
43 TypeAlias name tvars tipe deriveables ->
44 P.hang (P.text "type" <+> alias <+> P.equals) 4 (pretty tipe) <+> deriving'
45 where
46 alias = P.text name <+> P.hsep (map P.text tvars)
47 deriving' =
48 case deriveables of
49 [] -> P.empty
50 [d] -> P.text "deriving" <+> P.text (show d)
51 ds -> P.text "deriving" <+>
52 P.parens (P.hsep $ P.punctuate P.comma $ map (P.text . show) ds)
54 -- TODO: Actually write out the contained data in these cases.
55 ImportEvent _ _ _ _ -> P.text (show decl)
56 ExportEvent _ _ _ -> P.text (show decl)
57 Fixity _ _ _ -> P.text (show decl)