on 267f708 help sign in
elk/src/AST/Expression/General.hs
111 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 | ExplicitList [Expr ann def var typ]
46 | Binop var (Expr ann def var typ) (Expr ann def var typ)
47 | Lambda (Pattern.Pattern ann var) (Expr ann def var typ)
48 | App (Expr ann def var typ) (Expr ann def var typ)
49 | If [(Expr ann def var typ, Expr ann def var typ)] (Expr ann def var typ)
50 | Let [def] (Expr ann def var typ)
51 | Case (Expr ann def var typ) [(Pattern.Pattern ann var, Expr ann def var typ)]
52 | Data String [Expr ann def var typ]
53 | Access (Expr ann def var typ) String
54 | Update (Expr ann def var typ) [(String, Expr ann def var typ)]
55 | Record [(String, Expr ann def var typ)]
56 -- for type checking and code gen only
57 | Cmd ModuleName.Canonical
58 | Sub ModuleName.Canonical
59 | OutgoingPort String typ
60 | IncomingPort String typ
61 | Program (Main typ) (Expr ann def var typ)
62 | SaveEnv ModuleName.Canonical Effects.Canonical
63 | GLShader String String Literal.GLShaderTipe
66data Main typ
67 = VDom
68 | NoFlags
69 | Flags typ
73---- UTILITIES ----
76rawVar :: String -> Expr' ann def Var.Raw typ
77rawVar x =
78 Var (Var.Raw x)
81localVar :: String -> Expr' ann def Var.Canonical typ
82localVar x =
83 Var (Var.Canonical Var.Local x)
86tuple :: [Expr ann def var typ] -> Expr' ann def var typ
87tuple expressions =
88 Data ("_Tuple" ++ show (length expressions)) expressions
91collectApps :: Expr ann def var typ -> [Expr ann def var typ]
92collectApps annExpr@(A.A _ expr) =
93 case expr of
94 App a b ->
95 collectApps a ++ [b]
97 _ ->
98 [annExpr]
101collectLambdas :: Expr ann def var typ -> ([Pattern.Pattern ann var], Expr ann def var typ)
102collectLambdas lexpr@(A.A _ expr) =
103 case expr of
104 Lambda pattern body ->
105 let
106 (ps, body') = collectLambdas body
107 in
108 (pattern : ps, body')
110 _ ->
111 ([], lexpr)