on 1d0ce8e help sign in
elk/compiler/SourceSyntax/Declaration.hs
48 lines 1.5 kB history raw
1{-# LANGUAGE DeriveDataTypeable #-}
2module SourceSyntax.Declaration where
4import Data.Data
5import qualified SourceSyntax.Expression as Expr
6import SourceSyntax.Type
7import SourceSyntax.PrettyPrint
8import Text.PrettyPrint as P
10data Declaration tipe var
11 = Definition (Expr.Def tipe var)
12 | Datatype String [String] [(String,[Type])]
13 | TypeAlias String [String] Type
14 | ImportEvent String (Expr.LExpr tipe var) String Type
15 | ExportEvent String String Type
16 | Fixity Assoc Int String
17 deriving (Eq, Show)
19data Assoc = L | N | R
20 deriving (Eq)
22instance Show Assoc where
23 show assoc =
24 case assoc of
25 L -> "left"
26 N -> "non"
27 R -> "right"
29instance Pretty (Declaration t v) where
30 pretty decl =
31 case decl of
32 Definition def -> pretty def
34 Datatype tipe tvars ctors ->
35 P.hang (P.text "data" <+> P.text tipe <+> P.hsep (map P.text tvars)) 4
36 (P.sep $ zipWith join ("=" : repeat "|") ctors)
37 where
38 join c ctor = P.text c <+> prettyCtor ctor
39 prettyCtor (name, tipes) =
40 P.hang (P.text name) 2 (P.sep (map prettyParens tipes))
42 TypeAlias name tvars tipe ->
43 let alias = P.text name <+> P.hsep (map P.text tvars) in
44 P.hang (P.text "type" <+> alias <+> P.equals) 4 (pretty tipe)
46 -- TODO: actually write out the other cases. They are currently unused, but
47 -- this is probably going to be a bug someday.
48 _ -> P.text (show decl)