@@ -1,5 +1,12 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2module SourceSyntax.Expression where
2module SourceSyntax.Expression where
3+{-| The Abstract Syntax Tree (AST) for expressions comes in a couple formats.
4+The first is the fully general version and is labeled with a prime (Expr').
5+The others are specialized versions of the AST that represent specific phases
6+of the compilation process. I expect there to be more phases as we begin to
7+enrich the AST with more information.
4import SourceSyntax.PrettyPrint
11import SourceSyntax.PrettyPrint
5import Text.PrettyPrint as P
12import Text.PrettyPrint as P
@@ -9,12 +16,18 @@ import qualified SourceSyntax.Pattern as Pattern
9import qualified SourceSyntax.Type as SrcType
16import qualified SourceSyntax.Type as SrcType
10import qualified SourceSyntax.Literal as Literal
17import qualified SourceSyntax.Literal as Literal
12−{-| This is a located expression. -}
21+{-| This is a located expression, meaning it is tagged with info about where it
22+came from in the source code. Expr' is defined in terms of LExpr' so that the
23+location information does not need to be an extra field on every constructor.
13type LExpr' def = Location.Located (Expr' def)
25type LExpr' def = Location.Located (Expr' def)
15−{-| This is a fully general expression. The compilation process will enrich
27+{-| This is a fully general Abstract Syntax Tree (AST) for expressions. It has
16−this data structure with additional information after the initial parse.
28+"type holes" that allow us to enrich the AST with additional information as we
29+move through the compilation process. The type holes let us show these
17−The type holes let us reflect these structural changes in the types.
30+structural changes in the types. The only type hole right now is:
19 def: Parsing allows two kinds of definitions (type annotations or definitions),
32 def: Parsing allows two kinds of definitions (type annotations or definitions),
20 but later checks will see that they are well formed and combine them.
33 but later checks will see that they are well formed and combine them.
@@ -42,25 +55,34 @@ data Expr' def
42 | PortIn String SrcType.Type
55 | PortIn String SrcType.Type
43 | PortOut String SrcType.Type (LExpr' def)
56 | PortOut String SrcType.Type (LExpr' def)
59+---- SPECIALIZED ASTs ----
61+{-| Expressions created by the parser. These use a split representation of type
62+annotations and definitions, which is how they appear in source code and how
45type ParseExpr = Expr' ParseDef
65type ParseExpr = Expr' ParseDef
46type LParseExpr = LExpr' ParseDef
66type LParseExpr = LExpr' ParseDef
47−type LExpr = LExpr' Def
50−{-| Style of definitions as they are parsed. Users specify them
51−separately, and other checks will make sure they can be combined.
53data ParseDef
68data ParseDef
54 = Def Pattern.Pattern LParseExpr
69 = Def Pattern.Pattern LParseExpr
55 | TypeAnnotation String SrcType.Type
70 | TypeAnnotation String SrcType.Type
56 deriving (Show)
71 deriving (Show)
58−{-| After checking that type annotations and definitions are all
73+{-| "Normal" expressions. When the compiler checks that type annotations and
59−valid, they can be combined.
74+ports are all paired with definitions in the appropriate order, it collapses
75+them into a Def that is easier to work with in later phases of compilation.
77+type LExpr = LExpr' Def
61−data Def = Definition Pattern.Pattern (LExpr' Def) (Maybe SrcType.Type)
80+data Def = Definition Pattern.Pattern LExpr (Maybe SrcType.Type)
62 deriving (Show)
81 deriving (Show)
64tuple :: [LExpr' def] -> Expr' def
86tuple :: [LExpr' def] -> Expr' def
65tuple es = Data ("_Tuple" ++ show (length es)) es
87tuple es = Data ("_Tuple" ++ show (length es)) es