on b97576d help sign in
elk/src/AST/Declaration.hs
151 lines 2.3 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module AST.Declaration where
4import Data.Binary
6import qualified AST.Expression.Canonical as Canonical
7import qualified AST.Expression.Source as Src
8import qualified AST.Type as Type
9import qualified Reporting.Annotation as A
13-- SOURCE DECLARATIONS
16type Source =
17 CommentOr (A.Located Raw)
20data CommentOr a
21 = Comment (A.Located String)
22 | Whatever a
25data Raw
26 = Def Src.RawDef
27 | Union (Union Type.Raw)
28 | Alias (Alias Type.Raw)
29 | Fixity Infix
30 | Port String Type.Raw
34-- STRUCTURED DECLARATIONS
37data Decls def tipe =
38 Decls
39 { _defs :: [A.Commented def]
40 , _unions :: [A.Commented (Union tipe)]
41 , _aliases :: [A.Commented (Alias tipe)]
42 , _infixes :: [Infix]
43 }
46type Valid =
47 Decls Src.ValidDef Type.Raw
50type Canonical =
51 Decls Canonical.Def Type.Canonical
54addDef :: A.Commented d -> Decls d t -> Decls d t
55addDef def decls =
56 decls { _defs = def : _defs decls }
59addUnion :: A.Commented (Union t) -> Decls d t -> Decls d t
60addUnion union decls =
61 decls { _unions = union : _unions decls }
64addAlias :: A.Commented (Alias t) -> Decls d t -> Decls d t
65addAlias alias decls =
66 decls { _aliases = alias : _aliases decls }
69addInfix :: Infix -> Decls d t -> Decls d t
70addInfix fixity decls =
71 decls { _infixes = fixity : _infixes decls }
75-- TYPE DECLARATIONS
78data Type body =
79 Type
80 { _name :: String
81 , _args :: [String]
82 , _body :: body
83 }
86type Union tipe =
87 Type [(String, [tipe])]
90type Alias tipe =
91 Type tipe
95-- INFIX STUFF
98data Infix =
99 Infix
100 { _op :: String
101 , _associativity :: Assoc
102 , _precedence :: Int
103 }
106data Assoc = L | N | R
107 deriving (Eq)
110assocToString :: Assoc -> String
111assocToString assoc =
112 case assoc of
113 L ->
114 "left"
116 N ->
117 "non"
119 R ->
120 "right"
124-- BINARY CONVERSION
127instance Binary Infix where
128 get =
129 Infix <$> get <*> get <*> get
131 put (Infix op assoc prec) =
132 do put op
133 put assoc
134 put prec
137instance Binary Assoc where
138 get =
139 do n <- getWord8
140 return $ case n of
141 0 -> L
142 1 -> N
143 2 -> R
144 _ -> error "Error reading valid associativity from serialized string"
146 put assoc =
147 putWord8 $
148 case assoc of
149 L -> 0
150 N -> 1
151 R -> 2