on ccc6bc5 help sign in
elk/src/AST/Declaration.hs
129 lines 1.9 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module AST.Declaration where
4import Data.Binary
6import qualified AST.Expression.Source as Source
7import qualified AST.Expression.Valid as Valid
8import qualified AST.Expression.Canonical as Canonical
9import qualified AST.Type as Type
10import qualified Reporting.Annotation as A
14-- DECLARATIONS
17data Decl def tipe expr
18 = Def def
19 | Union String [String] [(String, [tipe])]
20 | Alias String [String] tipe
21 | Fixity Assoc Int String
25-- DECLARATION PHASES
28type Source =
29 CommentOr (A.Located SourceOrDefine)
32data SourceOrDefine
33 = Source Source'
34 | Define EffectType [CommentOr (A.Located Source.Effect)]
37type Source' =
38 Decl Source.Def Type.Raw Source.Expr
41type Valid =
42 A.Commented Valid'
45type Valid' =
46 Decl Valid.Def Type.Raw Valid.Expr
49type Canonical =
50 A.Commented (Decl Canonical.Def Type.Canonical Canonical.Expr)
54-- COMMENTS
57data CommentOr a
58 = Comment (A.Located String)
59 | Whatever a
63-- EFFECTS
66data EffectType
67 = Cmd
68 | Sub
69 | ForeignCmd
70 | ForeignSub
71 deriving (Eq, Ord)
74effectTypeToString :: EffectType -> String
75effectTypeToString tipe =
76 case tipe of
77 Cmd ->
78 "commands"
80 Sub ->
81 "subscriptions"
83 ForeignCmd ->
84 "foreign commands"
86 ForeignSub ->
87 "foreign subscriptions"
91-- INFIX STUFF
94data Assoc = L | N | R
95 deriving (Eq)
98assocToString :: Assoc -> String
99assocToString assoc =
100 case assoc of
101 L ->
102 "left"
104 N ->
105 "non"
107 R ->
108 "right"
112-- BINARY CONVERSION
115instance Binary Assoc where
116 get =
117 do n <- getWord8
118 return $ case n of
119 0 -> L
120 1 -> N
121 2 -> R
122 _ -> error "Error reading valid associativity from serialized string"
124 put assoc =
125 putWord8 $
126 case assoc of
127 L -> 0
128 N -> 1
129 R -> 2