on 17618de help sign in
elk/src/AST/Expression/General.hs
112 lines 3.4 kB history raw
1{-# OPTIONS_GHC -Wall #-}
3{-| The Abstract Syntax Tree (AST) for expressions comes in a couple formats.
4The first is the fully general version and is labeled with a prime (Expr').
5The others are specialized versions of the AST that represent specific phases
6of the compilation process. I expect there to be more phases as we begin to
7enrich the AST with more information.
8-}
9module AST.Expression.General where
11import qualified AST.Effects as Effects
12import qualified AST.Literal as Literal
13import qualified AST.Module.Name as ModuleName
14import qualified AST.Pattern as Pattern
15import qualified AST.Variable as Var
16import qualified Reporting.Annotation as A
20---- GENERAL AST ----
23{-| This is a fully general Abstract Syntax Tree (AST) for expressions. It has
24"type holes" that allow us to enrich the AST with additional information as we
25move through the compilation process. The type holes are used to represent:
27 ann: Annotations for arbitrary expressions. Allows you to add information
28 to the AST like position in source code or inferred types.
30 def: Definition style. The source syntax separates type annotations and
31 definitions, but after parsing we check that they are well formed and
32 collapse them.
34 var: Representation of variables. Starts as strings, but is later enriched
35 with information about what module a variable came from.
37-}
38type Expr annotation definition variable tipe =
39 A.Annotated annotation (Expr' annotation definition variable tipe)
42data Expr' ann def var typ
43 = Literal Literal.Literal
44 | Var var
45 | Range (Expr ann def var typ) (Expr ann def var typ)
46 | ExplicitList [Expr ann def var typ]
47 | Binop var (Expr ann def var typ) (Expr ann def var typ)
48 | Lambda (Pattern.Pattern ann var) (Expr ann def var typ)
49 | App (Expr ann def var typ) (Expr ann def var typ)
50 | If [(Expr ann def var typ, Expr ann def var typ)] (Expr ann def var typ)
51 | Let [def] (Expr ann def var typ)
52 | Case (Expr ann def var typ) [(Pattern.Pattern ann var, Expr ann def var typ)]
53 | Data String [Expr ann def var typ]
54 | Access (Expr ann def var typ) String
55 | Update (Expr ann def var typ) [(String, Expr ann def var typ)]
56 | Record [(String, Expr ann def var typ)]
57 -- for type checking and code gen only
58 | Cmd ModuleName.Canonical
59 | Sub ModuleName.Canonical
60 | OutgoingPort String typ
61 | IncomingPort String typ
62 | Program (Main typ) (Expr ann def var typ)
63 | SaveEnv ModuleName.Canonical Effects.Canonical
64 | GLShader String String Literal.GLShaderTipe
67data Main typ
68 = VDom
69 | NoFlags
70 | Flags typ
74---- UTILITIES ----
77rawVar :: String -> Expr' ann def Var.Raw typ
78rawVar x =
79 Var (Var.Raw x)
82localVar :: String -> Expr' ann def Var.Canonical typ
83localVar x =
84 Var (Var.Canonical Var.Local x)
87tuple :: [Expr ann def var typ] -> Expr' ann def var typ
88tuple expressions =
89 Data ("_Tuple" ++ show (length expressions)) expressions
92collectApps :: Expr ann def var typ -> [Expr ann def var typ]
93collectApps annExpr@(A.A _ expr) =
94 case expr of
95 App a b ->
96 collectApps a ++ [b]
98 _ ->
99 [annExpr]
102collectLambdas :: Expr ann def var typ -> ([Pattern.Pattern ann var], Expr ann def var typ)
103collectLambdas lexpr@(A.A _ expr) =
104 case expr of
105 Lambda pattern body ->
106 let
107 (ps, body') = collectLambdas body
108 in
109 (pattern : ps, body')
111 _ ->
112 ([], lexpr)