on 9111afa help sign in
elk/src/AST/Expression/Source.hs
95 lines 1.8 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module AST.Expression.Source
3 ( RawExpr, RawExpr', RawDef, RawDef'(..)
4 , ValidExpr, ValidExpr', ValidDef(..)
5 , Expr, Expr'(..)
6 , zero, collectLambdas
7 )
8 where
11import Data.Text (Text)
13import qualified AST.Literal as Literal
14import qualified AST.Pattern as Ptrn
15import qualified AST.Type as Type
16import qualified Reporting.Annotation as A
17import qualified Reporting.Region as R
21-- EXPRESSIONS
24type Expr def =
25 A.Located (Expr' def)
28data Expr' def
29 = Literal Literal.Literal
30 | Var (Maybe Text) Text
31 | List [Expr def]
32 | Op Text
33 | Binop [(Expr def, A.Located Text)] (Expr def)
34 | Lambda Ptrn.Raw (Expr def)
35 | App (Expr def) (Expr def)
36 | If [(Expr def, Expr def)] (Expr def)
37 | Let [def] (Expr def)
38 | Case (Expr def) [(Ptrn.Raw, Expr def)]
39 | Accessor Text
40 | Access (Expr def) Text
41 | Update (A.Located Text) [(A.Located Text, Expr def)]
42 | Record [(A.Located Text, Expr def)]
43 | Unit
44 | Tuple (Expr def) (Expr def) [Expr def]
45 | GLShader Text Text Literal.Shader
48type RawExpr = Expr RawDef
50type RawExpr' = Expr' RawDef
52type ValidExpr = Expr ValidDef
54type ValidExpr' = Expr' ValidDef
58-- DEFINITIONS
61type RawDef =
62 A.Located RawDef'
65data RawDef'
66 = Annotation Text Type.Raw
67 | Definition Text [Ptrn.Raw] RawExpr
68 | Destructure Ptrn.Raw RawExpr
71data ValidDef
72 = VDefinition R.Region Text [Ptrn.Raw] ValidExpr (Maybe Type.Raw)
73 | VDestructure R.Region Ptrn.Raw ValidExpr
77-- HELPERS
80zero :: Expr' def
81zero =
82 Literal (Literal.IntNum 0)
85collectLambdas :: Expr def -> ([Ptrn.Raw], Expr def)
86collectLambdas lexpr@(A.A _ expr) =
87 case expr of
88 Lambda pattern body ->
89 let
90 (ps, body') = collectLambdas body
91 in
92 (pattern : ps, body')
94 _ ->
95 ([], lexpr)