on df41ae8 help sign in
elk/src/AST/Expression/General.hs
103 lines 3.3 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 Fx
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 String
59 | Sub ModuleName.Canonical String
60 | SaveEnv ModuleName.Canonical Fx.Effects
61 | GLShader String String Literal.GLShaderTipe
65---- UTILITIES ----
68rawVar :: String -> Expr' ann def Var.Raw typ
69rawVar x =
70 Var (Var.Raw x)
73localVar :: String -> Expr' ann def Var.Canonical typ
74localVar x =
75 Var (Var.Canonical Var.Local x)
78tuple :: [Expr ann def var typ] -> Expr' ann def var typ
79tuple expressions =
80 Data ("_Tuple" ++ show (length expressions)) expressions
83collectApps :: Expr ann def var typ -> [Expr ann def var typ]
84collectApps annExpr@(A.A _ expr) =
85 case expr of
86 App a b ->
87 collectApps a ++ [b]
89 _ ->
90 [annExpr]
93collectLambdas :: Expr ann def var typ -> ([Pattern.Pattern ann var], Expr ann def var typ)
94collectLambdas lexpr@(A.A _ expr) =
95 case expr of
96 Lambda pattern body ->
97 let
98 (ps, body') = collectLambdas body
99 in
100 (pattern : ps, body')
102 _ ->
103 ([], lexpr)