on 769faec help sign in
elk/src/AST/Expression/General.hs
124 lines 3.7 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.Pattern as Pattern
13import qualified AST.Type as Type
14import qualified AST.Variable as Var
15import qualified Reporting.Annotation as A
18---- GENERAL AST ----
20{-| This is a fully general Abstract Syntax Tree (AST) for expressions. It has
21"type holes" that allow us to enrich the AST with additional information as we
22move through the compilation process. The type holes are used to represent:
24 ann: Annotations for arbitrary expressions. Allows you to add information
25 to the AST like position in source code or inferred types.
27 def: Definition style. The source syntax separates type annotations and
28 definitions, but after parsing we check that they are well formed and
29 collapse them.
31 var: Representation of variables. Starts as strings, but is later enriched
32 with information about what module a variable came from.
34-}
35type Expr annotation definition variable tipe =
36 A.Annotated annotation (Expr' annotation definition variable tipe)
39data Expr' ann def var typ
40 = Literal Literal.Literal
41 | Var var
42 | Range (Expr ann def var typ) (Expr ann def var typ)
43 | ExplicitList [Expr ann def var typ]
44 | Binop var (Expr ann def var typ) (Expr ann def var typ)
45 | Lambda (Pattern.Pattern ann var) (Expr ann def var typ)
46 | App (Expr ann def var typ) (Expr ann def var typ)
47 | If [(Expr ann def var typ, Expr ann def var typ)] (Expr ann def var typ)
48 | Let [def] (Expr ann def var typ)
49 | Case (Expr ann def var typ) [(Pattern.Pattern ann var, Expr ann def var typ)]
50 | Data String [Expr ann def var typ]
51 | Access (Expr ann def var typ) String
52 | Update (Expr ann def var typ) [(String, Expr ann def var typ)]
53 | Record [(String, Expr ann def var typ)]
54 -- for type checking and code gen only
55 | Port (PortImpl (Expr ann def var typ) typ)
56 | GLShader String String Literal.GLShaderTipe
59-- PORTS
61data PortImpl expr tipe
62 = In String (Type.Port tipe)
63 | Out String expr (Type.Port tipe)
64 | Task String expr (Type.Port tipe)
65 deriving (Eq)
68portName :: PortImpl expr tipe -> String
69portName impl =
70 case impl of
71 In name _ -> name
72 Out name _ _ -> name
73 Task name _ _ -> name
76---- UTILITIES ----
78rawVar :: String -> Expr' ann def Var.Raw typ
79rawVar x =
80 Var (Var.Raw x)
83localVar :: String -> Expr' ann def Var.Canonical typ
84localVar x =
85 Var (Var.Canonical Var.Local x)
88tuple :: [Expr ann def var typ] -> Expr' ann def var typ
89tuple expressions =
90 Data ("_Tuple" ++ show (length expressions)) expressions
93saveEnvName :: String
94saveEnvName =
95 "_save_the_environment!!!"
98dummyLet :: [def] -> Expr ann def Var.Canonical typ
99dummyLet defs =
100 let body =
101 A.A undefined (Var (Var.builtin saveEnvName))
102 in
103 A.A undefined (Let defs body)
106collectApps :: Expr ann def var typ -> [Expr ann def var typ]
107collectApps annExpr@(A.A _ expr) =
108 case expr of
109 App a b -> collectApps a ++ [b]
110 _ -> [annExpr]
113collectLambdas
114 :: Expr ann def var typ
115 -> ([Pattern.Pattern ann var], Expr ann def var typ)
116collectLambdas lexpr@(A.A _ expr) =
117 case expr of
118 Lambda pattern body ->
119 let
120 (ps, body') = collectLambdas body
121 in
122 (pattern : ps, body')
124 _ -> ([], lexpr)