on eb7feba help sign in
elk/src/AST/Expression/General.hs
115 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.Literal as Literal
12import qualified AST.Module.Name as ModuleName
13import qualified AST.Pattern as Pattern
14import qualified AST.Variable as Var
15import qualified Reporting.Annotation as A
19---- GENERAL AST ----
22{-| This is a fully general Abstract Syntax Tree (AST) for expressions. It has
23"type holes" that allow us to enrich the AST with additional information as we
24move through the compilation process. The type holes are used to represent:
26 ann: Annotations for arbitrary expressions. Allows you to add information
27 to the AST like position in source code or inferred types.
29 def: Definition style. The source syntax separates type annotations and
30 definitions, but after parsing we check that they are well formed and
31 collapse them.
33 var: Representation of variables. Starts as strings, but is later enriched
34 with information about what module a variable came from.
36-}
37type Expr annotation definition variable tipe =
38 A.Annotated annotation (Expr' annotation definition variable tipe)
41data Expr' ann def var typ
42 = Literal Literal.Literal
43 | Var var
44 | Range (Expr ann def var typ) (Expr ann def var typ)
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 String
58 | Sub ModuleName.Canonical String
59 | GLShader String String Literal.GLShaderTipe
63---- UTILITIES ----
66rawVar :: String -> Expr' ann def Var.Raw typ
67rawVar x =
68 Var (Var.Raw x)
71localVar :: String -> Expr' ann def Var.Canonical typ
72localVar x =
73 Var (Var.Canonical Var.Local x)
76tuple :: [Expr ann def var typ] -> Expr' ann def var typ
77tuple expressions =
78 Data ("_Tuple" ++ show (length expressions)) expressions
81saveEnvName :: String
82saveEnvName =
83 "_save_the_environment!!!"
86dummyLet :: [def] -> Expr ann def Var.Canonical typ
87dummyLet defs =
88 let
89 body =
90 A.A undefined (Var (Var.builtin saveEnvName))
91 in
92 A.A undefined (Let defs body)
95collectApps :: Expr ann def var typ -> [Expr ann def var typ]
96collectApps annExpr@(A.A _ expr) =
97 case expr of
98 App a b ->
99 collectApps a ++ [b]
101 _ ->
102 [annExpr]
105collectLambdas :: Expr ann def var typ -> ([Pattern.Pattern ann var], Expr ann def var typ)
106collectLambdas lexpr@(A.A _ expr) =
107 case expr of
108 Lambda pattern body ->
109 let
110 (ps, body') = collectLambdas body
111 in
112 (pattern : ps, body')
114 _ ->
115 ([], lexpr)