on master help sign in
Move AST.Patterns into the phased expression AST
1b6b47aEvan Czaplicki
src/AST/Expression/Canonical.hs+58−17
@@ -1,9 +1,15 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3module AST.Expression.Canonical
3module AST.Expression.Canonical
4− ( Expr
5− , Expr_(..)
4+ ( Expr, Expr_(..)
6 , Def(..)
5 , Def(..)
6+ -- patterns
7+ , Args(..), Arg(..)
8+ , Match(..)
9+ , Pattern, Pattern_(..)
10+ , Destructors
11+ , Destructor(..)
12+ -- decls
7 , Decl(..)
13 , Decl(..)
8 , Module(..)
14 , Module(..)
9 , Alias(..)
15 , Alias(..)
@@ -25,7 +31,6 @@ import Data.Text (Text)
25import qualified AST.Binop as Binop
31import qualified AST.Binop as Binop
26import qualified AST.Literal as Literal
32import qualified AST.Literal as Literal
27import qualified AST.Module.Name as ModuleName
33import qualified AST.Module.Name as ModuleName
28−import qualified AST.Pattern as Ptrn
29import qualified AST.Type as Type
34import qualified AST.Type as Type
30import qualified AST.Variable as Var
35import qualified AST.Variable as Var
31import qualified Elm.Name as N
36import qualified Elm.Name as N
@@ -39,7 +44,7 @@ import qualified Reporting.Region as R
39
44
40
45
41type Expr =
46type Expr =
42− A.Annotated R.Region Expr_
47+ A.Located Expr_
43
48
44
49
45data Expr_
50data Expr_
@@ -47,22 +52,25 @@ data Expr_
47 | VarTopLevel ModuleName.Canonical Name
52 | VarTopLevel ModuleName.Canonical Name
48 | VarKernel Name Name
53 | VarKernel Name Name
49 | VarForeign ModuleName.Canonical Name
54 | VarForeign ModuleName.Canonical Name
50− | VarOperator ModuleName.Canonical Name Name
55+ | VarOperator Name ModuleName.Canonical Name
51 | Literal Literal.Literal
56 | Literal Literal.Literal
52 | List [Expr]
57 | List [Expr]
58+ | Negate Expr
53 | Binop Name ModuleName.Canonical Name Expr Expr
59 | Binop Name ModuleName.Canonical Name Expr Expr
54− | Lambda [Ptrn.Canonical] Expr
60+ | Lambda Args Expr
55 | Call Expr [Expr]
61 | Call Expr [Expr]
56 | If [(Expr, Expr)] Expr
62 | If [(Expr, Expr)] Expr
63+ | Let Def Expr
57− | Let [Graph.SCC Def] Expr
64+ | LetRec [Def] Expr
65+ | LetDestruct Match Expr Expr
58− | Case Expr [(Ptrn.Canonical, Expr)]
66+ | Case Expr [(Match, Expr)]
59 | CtorAccess Expr Int
67 | CtorAccess Expr Int
60 | Accessor Name
68 | Accessor Name
61 | Access Expr Name
69 | Access Expr Name
62− | Update Expr [(Name, Expr)]
70+ | Update Expr (Map.Map Name Expr)
63− | Record [(Name, Expr)]
71+ | Record (Map.Map Name Expr)
64 | Unit
72 | Unit
65− | Tuple Expr Expr [Expr]
73+ | Tuple Expr Expr (Maybe Expr)
66 | GLShader Name Name Literal.Shader
74 | GLShader Name Name Literal.Shader
67
75
68
76
@@ -72,21 +80,54 @@ data Expr_
72
80
73data Def =
81data Def =
74 Def
82 Def
75− { _let_name :: Text
83+ { _def_name :: A.Located N.Name
76− , _let_args :: [A.Located Ptrn.Canonical]
84+ , _def_args :: Args
77− , _let_body :: Expr
85+ , _def_body :: Expr
78− , _let_type :: Maybe (A.Located Type.Canonical)
86+ , _def_type :: Maybe (A.Located Type.Canonical)
79 }
87 }
80
88
81
89
82
90
91+-- PATTERNS
92+
93+
94+data Args = Args [Arg] Destructors
95+data Arg = Arg Int Pattern
96+
97+data Match = Match Pattern Destructors
98+
99+
100+type Pattern = A.Located Pattern_
101+
102+data Pattern_
103+ = PAnything
104+ | PVar N.Name
105+ | PRecord [N.Name]
106+ | PAlias Pattern N.Name
107+ | PUnit
108+ | PTuple Pattern Pattern (Maybe Pattern)
109+ | PCtor ModuleName.Canonical N.Name [Pattern]
110+ | PList [Pattern]
111+ | PCons Pattern Pattern
112+ | PLiteral Literal.Literal
113+
114+
115+type Destructors = Map.Map N.Name (A.Located Destructor)
116+
117+data Destructor
118+ = DIndex Int Destructor
119+ | DField N.Name Destructor
120+ | DRoot Int
121+
122+
123+
83-- DECLARATIONS
124-- DECLARATIONS
84
125
85
126
86data Decl =
127data Decl =
87 Decl
128 Decl
88− { _top_name :: Text
129+ { _top_name :: N.Name
89− , _top_args :: [A.Located Ptrn.Canonical]
130+ , _top_args :: Args
90 , _top_body :: Expr
131 , _top_body :: Expr
91 , _top_type :: Maybe (A.Located Type.Canonical)
132 , _top_type :: Maybe (A.Located Type.Canonical)
92 , _top_deps :: [Var.Global]
133 , _top_deps :: [Var.Global]
src/AST/Expression/Source.hs+26−6
@@ -4,6 +4,7 @@ module AST.Expression.Source
4 ( Expr, Expr_(..)
4 ( Expr, Expr_(..)
5 , Decl, Decl_(..)
5 , Decl, Decl_(..)
6 , Def(..)
6 , Def(..)
7+ , Pattern, Pattern_(..)
7 , Module(..)
8 , Module(..)
8 , Import(..)
9 , Import(..)
9 , Effects(..)
10 , Effects(..)
@@ -21,7 +22,6 @@ import Data.Text (Text)
21
22
22import qualified AST.Binop as Binop
23import qualified AST.Binop as Binop
23import qualified AST.Literal as Literal
24import qualified AST.Literal as Literal
24−import qualified AST.Pattern as Ptrn
25import qualified AST.Type as Type
25import qualified AST.Type as Type
26import qualified Elm.Name as N
26import qualified Elm.Name as N
27import qualified Reporting.Annotation as A
27import qualified Reporting.Annotation as A
@@ -42,11 +42,11 @@ data Expr_
42 | Op N.Name
42 | Op N.Name
43 | Negate Expr
43 | Negate Expr
44 | Binops [(Expr, A.Located N.Name)] Expr
44 | Binops [(Expr, A.Located N.Name)] Expr
45− | Lambda [Ptrn.Raw] Expr
45+ | Lambda [Pattern] Expr
46 | Call Expr [Expr]
46 | Call Expr [Expr]
47 | If [(Expr, Expr)] Expr
47 | If [(Expr, Expr)] Expr
48 | Let [A.Located Def] Expr
48 | Let [A.Located Def] Expr
49− | Case Expr [(Ptrn.Raw, Expr)]
49+ | Case Expr [(Pattern, Expr)]
50 | Accessor N.Name
50 | Accessor N.Name
51 | Access Expr N.Name
51 | Access Expr N.Name
52 | Update (A.Located N.Name) [(A.Located N.Name, Expr)]
52 | Update (A.Located N.Name) [(A.Located N.Name, Expr)]
@@ -62,8 +62,28 @@ data Expr_
62
62
63data Def
63data Def
64 = Annotate N.Name Type.Raw
64 = Annotate N.Name Type.Raw
65− | Define N.Name [Ptrn.Raw] Expr
65+ | Define (A.Located N.Name) [Pattern] Expr
66− | Destruct Ptrn.Raw Expr
66+ | Destruct Pattern Expr
67+
68+
69+
70+-- PATTERN
71+
72+
73+type Pattern = A.Located Pattern_
74+
75+
76+data Pattern_
77+ = PAnything
78+ | PVar N.Name
79+ | PRecord [A.Located N.Name]
80+ | PAlias Pattern (A.Located N.Name)
81+ | PUnit
82+ | PTuple Pattern Pattern [Pattern]
83+ | PCtor R.Region (Maybe N.Name) N.Name [Pattern]
84+ | PList [Pattern]
85+ | PCons Pattern Pattern
86+ | PLiteral Literal.Literal
67
87
68
88
69
89
@@ -80,7 +100,7 @@ data Decl_
80 | Port (A.Located N.Name) Type.Raw
100 | Port (A.Located N.Name) Type.Raw
81 | Docs Text
101 | Docs Text
82 | Annotation (A.Located N.Name) Type.Raw
102 | Annotation (A.Located N.Name) Type.Raw
83− | Definition (A.Located N.Name) [Ptrn.Raw] Expr
103+ | Definition (A.Located N.Name) [Pattern] Expr
84
104
85
105
86
106
src/AST/Expression/Valid.hs+5−6
@@ -20,7 +20,6 @@ import Data.Text (Text)
20import qualified AST.Binop as Binop
20import qualified AST.Binop as Binop
21import qualified AST.Expression.Source as Src
21import qualified AST.Expression.Source as Src
22import qualified AST.Literal as Literal
22import qualified AST.Literal as Literal
23−import qualified AST.Pattern as Pattern
24import qualified AST.Type as Type
23import qualified AST.Type as Type
25import qualified Elm.Name as N
24import qualified Elm.Name as N
26import qualified Reporting.Annotation as A
25import qualified Reporting.Annotation as A
@@ -41,11 +40,11 @@ data Expr_
41 | Op N.Name
40 | Op N.Name
42 | Negate Expr
41 | Negate Expr
43 | Binops [(Expr, A.Located N.Name)] Expr
42 | Binops [(Expr, A.Located N.Name)] Expr
44− | Lambda [Pattern.Raw] Expr
43+ | Lambda [Src.Pattern] Expr
45 | Call Expr [Expr]
44 | Call Expr [Expr]
46 | If [(Expr, Expr)] Expr
45 | If [(Expr, Expr)] Expr
47 | Let [Def] Expr
46 | Let [Def] Expr
48− | Case Expr [(Pattern.Raw, Expr)]
47+ | Case Expr [(Src.Pattern, Expr)]
49 | Accessor N.Name
48 | Accessor N.Name
50 | Access Expr N.Name
49 | Access Expr N.Name
51 | Update (A.Located N.Name) [(A.Located N.Name, Expr)]
50 | Update (A.Located N.Name) [(A.Located N.Name, Expr)]
@@ -60,8 +59,8 @@ data Expr_
60
59
61
60
62data Def
61data Def
63− = Define R.Region N.Name [Pattern.Raw] Expr (Maybe Type.Raw)
62+ = Define R.Region (A.Located N.Name) [Src.Pattern] Expr (Maybe Type.Raw)
64− | Destruct R.Region Pattern.Raw Expr
63+ | Destruct R.Region Src.Pattern Expr
65
64
66
65
67
66
@@ -83,7 +82,7 @@ data Module =
83 }
82 }
84
83
85
84
86−data Decl = Decl (A.Located N.Name) [Pattern.Raw] Expr (Maybe Type.Raw)
85+data Decl = Decl (A.Located N.Name) [Src.Pattern] Expr (Maybe Type.Raw)
87data Union = Union (A.Located N.Name) [A.Located N.Name] [(A.Located N.Name, [Type.Raw])]
86data Union = Union (A.Located N.Name) [A.Located N.Name] [(A.Located N.Name, [Type.Raw])]
88data Alias = Alias (A.Located N.Name) [A.Located N.Name] Type.Raw
87data Alias = Alias (A.Located N.Name) [A.Located N.Name] Type.Raw
89data Binop = Binop (A.Located N.Name) Binop.Associativity Binop.Precedence N.Name
88data Binop = Binop (A.Located N.Name) Binop.Associativity Binop.Precedence N.Name
src/AST/Pattern.hsdeleted−54
@@ -1,54 +0,0 @@
1−{-# OPTIONS_GHC -Wall #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−module AST.Pattern
4− ( Raw
5− , Raw_(..)
6− , Canonical
7− , Canonical_(..)
8− )
9− where
10−
11−
12−import qualified AST.Literal as Literal
13−import qualified AST.Module.Name as ModuleName
14−import qualified Elm.Name as N
15−import qualified Reporting.Annotation as A
16−import qualified Reporting.Region as R
17−
18−
19−
20−-- RAW
21−
22−
23−type Raw = A.Located Raw_
24−
25−
26−data Raw_
27− = RCtor R.Region (Maybe N.Name) N.Name [Raw]
28− | RRecord [A.Located N.Name]
29− | RUnit
30− | RTuple Raw Raw [Raw]
31− | RList [Raw]
32− | RCons Raw Raw
33− | RAlias Raw (A.Located N.Name)
34− | RVar N.Name
35− | RAnything
36− | RLiteral Literal.Literal
37−
38−
39−
40−-- CANONICAL
41−
42−
43−type Canonical = A.Located Canonical_
44−
45−
46−data Canonical_
47− = Ctor ModuleName.Canonical N.Name [Canonical]
48− | Record [N.Name]
49− | Unit
50− | Tuple Canonical Canonical [Canonical]
51− | Alias Canonical N.Name
52− | Var N.Name
53− | Anything
54− | Literal Literal.Literal