on 60aaaf4 help sign in
elk/compiler/SourceSyntax/Declaration.hs
47 lines 1.5 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
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)
20instance Show Assoc where
21 show assoc =
22 case assoc of
23 L -> "left"
24 N -> "non"
25 R -> "right"
27instance Pretty (Declaration t v) where
28 pretty decl =
29 case decl of
30 Definition def -> pretty def
32 Datatype tipe tvars ctors ->
33 P.hang (P.text "data" <+> P.text tipe <+> P.hsep (map P.text tvars)) 4
34 (P.sep $ zipWith join ("=" : repeat "|") ctors)
35 where
36 join c ctor = P.text c <+> prettyCtor ctor
37 prettyCtor (name, tipes) =
38 P.hang (P.text name) 2 (P.sep (map prettyParens tipes))
40 TypeAlias name tvars tipe ->
41 let alias = P.text name <+> P.hsep (map P.text tvars) in
42 P.hang (P.text "type" <+> alias <+> P.equals) 4 (pretty tipe)
44 -- TODO: Actually write out the contained data in these cases.
45 ImportEvent _ _ _ _ -> P.text (show decl)
46 ExportEvent _ _ _ -> P.text (show decl)
47 Fixity _ _ _ -> P.text (show decl)