on 0ff8a91 help sign in
elk/src/AST/Declaration.hs
152 lines 2.4 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-- SOURCE DECLARATIONS
17type Source =
18 CommentOr (A.Located Raw)
21data CommentOr a
22 = Comment (A.Located String)
23 | Whatever a
26data Raw
27 = Def Source.Def
28 | Union (Union Type.Raw)
29 | Alias (Alias Type.Raw)
30 | Fixity Infix
31 | Foreign String Type.Raw
35-- STRUCTURED DECLARATIONS
38data Decls def tipe =
39 Decls
40 { _defs :: [A.Commented def]
41 , _unions :: [A.Commented (Union tipe)]
42 , _aliases :: [A.Commented (Alias tipe)]
43 , _infixes :: [Infix]
44 }
47type Valid =
48 Decls Valid.Def Type.Raw
51type Canonical =
52 Decls Canonical.Def Type.Canonical
55addDef :: A.Commented d -> Decls d t -> Decls d t
56addDef def decls =
57 decls { _defs = def : _defs decls }
60addUnion :: A.Commented (Union t) -> Decls d t -> Decls d t
61addUnion union decls =
62 decls { _unions = union : _unions decls }
65addAlias :: A.Commented (Alias t) -> Decls d t -> Decls d t
66addAlias alias decls =
67 decls { _aliases = alias : _aliases decls }
70addInfix :: Infix -> Decls d t -> Decls d t
71addInfix fixity decls =
72 decls { _infixes = fixity : _infixes decls }
76-- TYPE DECLARATIONS
79data Type body =
80 Type
81 { _name :: String
82 , _args :: [String]
83 , _body :: body
84 }
87type Union tipe =
88 Type [(String, [tipe])]
91type Alias tipe =
92 Type tipe
96-- INFIX STUFF
99data Infix =
100 Infix
101 { _op :: String
102 , _associativity :: Assoc
103 , _precedence :: Int
104 }
107data Assoc = L | N | R
108 deriving (Eq)
111assocToString :: Assoc -> String
112assocToString assoc =
113 case assoc of
114 L ->
115 "left"
117 N ->
118 "non"
120 R ->
121 "right"
125-- BINARY CONVERSION
128instance Binary Infix where
129 get =
130 Infix <$> get <*> get <*> get
132 put (Infix op assoc prec) =
133 do put op
134 put assoc
135 put prec
138instance Binary Assoc where
139 get =
140 do n <- getWord8
141 return $ case n of
142 0 -> L
143 1 -> N
144 2 -> R
145 _ -> error "Error reading valid associativity from serialized string"
147 put assoc =
148 putWord8 $
149 case assoc of
150 L -> 0
151 N -> 1
152 R -> 2