on 4e73834 help sign in
elk/src/AST/Declaration.hs
106 lines 1.9 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module AST.Declaration where
4import Data.Binary
6import qualified AST.Expression.General as General
7import qualified AST.Expression.Source as Source
8import qualified AST.Expression.Valid as Valid
9import qualified AST.Expression.Canonical as Canonical
10import qualified AST.Type as Type
11import qualified Reporting.Annotation as A
15-- DECLARATIONS
18data Declaration' port def tipe expr
19 = Definition def
20 | Datatype String [String] [(String, [tipe])]
21 | TypeAlias String [String] tipe
22 | Port port
23 | Fixity Assoc Int String
27-- INFIX STUFF
30data Assoc = L | N | R
31 deriving (Eq)
34assocToString :: Assoc -> String
35assocToString assoc =
36 case assoc of
37 L -> "left"
38 N -> "non"
39 R -> "right"
43-- DECLARATION PHASES
46type SourceDecl' =
47 Declaration' SourcePort Source.Def Type.Raw Source.Expr
50data SourceDecl
51 = Comment String
52 | Decl (A.Located SourceDecl')
55type ValidDecl =
56 A.Commented (Declaration' ValidPort Valid.Def Type.Raw Valid.Expr)
59type CanonicalDecl =
60 A.Commented (Declaration' CanonicalPort Canonical.Def Type.Canonical Canonical.Expr)
64-- PORTS
67data SourcePort
68 = PortAnnotation String Type.Raw
69 | PortDefinition String Source.Expr
72data ValidPort
73 = In String Type.Raw
74 | Out String Valid.Expr Type.Raw
77newtype CanonicalPort
78 = CanonicalPort (General.PortImpl Canonical.Expr Type.Canonical)
81validPortName :: ValidPort -> String
82validPortName port =
83 case port of
84 In name _ -> name
85 Out name _ _ -> name
89-- BINARY CONVERSION
92instance Binary Assoc where
93 get =
94 do n <- getWord8
95 return $ case n of
96 0 -> L
97 1 -> N
98 2 -> R
99 _ -> error "Error reading valid associativity from serialized string"
101 put assoc =
102 putWord8 $
103 case assoc of
104 L -> 0
105 N -> 1
106 R -> 2