1{-# OPTIONS_GHC -Wall #-}
2module AST.Declaration where
5import qualified AST.Expression.Source as Source
6import qualified AST.Expression.Valid as Valid
7import qualified AST.Expression.Canonical as Canonical
8import qualified AST.Type as T
9import qualified AST.Variable as Var
11import Text.PrettyPrint as P
16data Declaration' wire def var
18 | Datatype String [String] [(String, [T.Type var])]
19 | TypeAlias String [String] (T.Type var)
21 | Fixity Assoc Int String
31 Declaration' RawWire Source.Def Var.Raw
35 Declaration' ValidWire Valid.Def Var.Raw
39 Declaration' CanonicalWire Canonical.Def Var.Canonical
45 = InputAnnotation String T.RawType
46 | OutputAnnotation String T.RawType
47 | OutputDefinition String Source.Expr
48 | LoopbackAnnotation String T.RawType
49 | LoopbackDefinition String Source.Expr
52data Wire expr var input
53 = Input String (T.Type var)
54 | Output String expr (T.Type var)
59 ValidLoopback String (Maybe Valid.Expr) T.RawType
63 = Address String T.CanonicalType
64 | Promise String T.CanonicalType Canonical.Expr T.CanonicalType
68 Wire Valid.Expr Var.Raw ValidLoopback
72 Wire Canonical.Expr Var.Canonical CanonicalLoopback
77instance Binary Assoc where
84 _ -> error "Error reading valid associativity from serialized string"
96assocToString :: Assoc -> String
104instance (Pretty wire, Pretty def, Pretty var, Var.ToString var) =>
105 Pretty (Declaration' wire def var) where
108 Definition def -> pretty def
110 Datatype tipe tvars ctors ->
112 (P.text "type" <+> P.text tipe <+> P.hsep (map P.text tvars))
114 (P.sep $ zipWith (<+>) seperators (map prettyCtor ctors))
117 map P.text ("=" : repeat "|")
119 prettyCtor (name, tipes) =
120 P.hang (P.text name) 2 (P.sep (map T.prettyParens tipes))
122 TypeAlias name tvars tipe ->
124 (P.text "type" <+> P.text "alias" <+> name' <+> P.equals)
129 P.text name <+> P.hsep (map P.text tvars)
131 Wire wire -> pretty wire
133 Fixity assoc prec op ->
134 P.text "infix" <> assoc' <+> P.int prec <+> P.text op
143instance Pretty RawWire where
146 InputAnnotation name tipe ->
147 prettyWire "foreign input" name ":" tipe
149 OutputAnnotation name tipe ->
150 prettyWire "foreign output" name ":" tipe
152 OutputDefinition name expr ->
153 prettyWire "foreign output" name "=" expr
155 LoopbackAnnotation name tipe ->
156 prettyWire "input" name ":" tipe
158 LoopbackDefinition name expr ->
159 prettyWire "input" name "from" expr
162instance (Pretty expr, Pretty var, Var.ToString var, Pretty input) =>
163 Pretty (Wire expr var input) where
167 prettyWire "foreign input" name ":" tipe
169 Output name expr tipe ->
171 [ prettyWire "foreign output" name ":" tipe
172 , prettyWire "foreign output" name "=" expr
179instance Pretty ValidLoopback where
180 pretty (ValidLoopback name maybeExpr tipe) =
182 [ prettyWire "input" name ":" tipe
183 , maybe P.empty (prettyWire "input" name "<-") maybeExpr
187instance Pretty CanonicalLoopback where
191 prettyWire "input" name ":" tipe
193 Promise name _promiseType expr resultType ->
195 [ prettyWire "input" name ":" resultType
196 , prettyWire "input" name "<-" expr
200prettyWire :: (Pretty a) => String -> String -> String -> a -> Doc
201prettyWire keyword name op e =
202 P.text keyword <+> P.text name <+> P.text op <+> pretty e
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list