on 2297bbf help sign in
elk/src/AST/Declaration.hs
96 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
14-- DECLARATIONS
16data Declaration' port def tipe expr
17 = Definition def
18 | Datatype String [String] [(String, [tipe])]
19 | TypeAlias String [String] tipe
20 | Port port
21 | Fixity Assoc Int String
24-- INFIX STUFF
26data Assoc = L | N | R
27 deriving (Eq)
30assocToString :: Assoc -> String
31assocToString assoc =
32 case assoc of
33 L -> "left"
34 N -> "non"
35 R -> "right"
38-- DECLARATION PHASES
40type SourceDecl' =
41 Declaration' SourcePort Source.Def Type.Raw Source.Expr
44data SourceDecl
45 = Comment String
46 | Decl (A.Located SourceDecl')
49type ValidDecl =
50 A.Commented (Declaration' ValidPort Valid.Def Type.Raw Valid.Expr)
53type CanonicalDecl =
54 A.Commented (Declaration' CanonicalPort Canonical.Def Type.Canonical Canonical.Expr)
57-- PORTS
59data SourcePort
60 = PortAnnotation String Type.Raw
61 | PortDefinition String Source.Expr
64data ValidPort
65 = In String Type.Raw
66 | Out String Valid.Expr Type.Raw
69newtype CanonicalPort
70 = CanonicalPort (General.PortImpl Canonical.Expr Type.Canonical)
73validPortName :: ValidPort -> String
74validPortName port =
75 case port of
76 In name _ -> name
77 Out name _ _ -> name
80-- BINARY CONVERSION
82instance Binary Assoc where
83 get =
84 do n <- getWord8
85 return $ case n of
86 0 -> L
87 1 -> N
88 2 -> R
89 _ -> error "Error reading valid associativity from serialized string"
91 put assoc =
92 putWord8 $
93 case assoc of
94 L -> 0
95 N -> 1
96 R -> 2