-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathExprType.hs
More file actions
228 lines (214 loc) · 6.74 KB
/
ExprType.hs
File metadata and controls
228 lines (214 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{-# LANGUAGE DeriveGeneric #-}
{-# OPTIONS_GHC -fno-warn-overlapping-patterns #-}
module Kit.Ast.ExprType where
import Data.Hashable
import GHC.Generics
import Kit.Ast.Definitions
import Kit.Ast.Identifier
import Kit.Ast.Metadata
import Kit.Ast.Operator
import Kit.Ast.TypePath
import Kit.Ast.UsingType
import Kit.Ast.Value
import Kit.Error
import Kit.Parser.Token
import Kit.Str
data MatchCase a = MatchCase {matchPattern :: a, matchBody :: a} deriving (Eq, Generic, Show)
instance (Hashable a) => Hashable (MatchCase a)
newMatchCase = MatchCase {matchPattern = undefined, matchBody = undefined}
matchCase x y = MatchCase {matchPattern = x, matchBody = y}
{-
All AST structures use the convention of two type parameters, a and b.
a = recursive expression data type (Expr, TypedExpr)
b = type specifier data type (TypeSpec, ConcreteType)
This allows reuse across passes of compilation:
- Initially, we use `ExprType (Expr) (TypeSpec)` for expressions with only
position info and optional annotations
- Typing converts this to `ExprType (TypedExpr) (ConcreteType)`, where each
expression has a type and all types are resolved to a single compile-time
type
-}
data ExprType a b
= Block [a]
| Using [UsingType a b] a
| Meta Metadata a
| Literal ValueLiteral b
| This
| Self
-- identifier, namespace
| Identifier (Identifier b)
-- expression, optional type annotation; blank to infer type
| TypeAnnotation a b
| PreUnop Operator a
| PostUnop Operator a
| Binop Operator a a
-- for (e1 in e2) e3
| For a a a
-- while (e1) e2 (do?)
| While a a Bool
-- if (e1) e2 [else e3]
| If a a (Maybe a)
| Continue
| Break
| Return (Maybe a)
| Match a [MatchCase a] (Maybe a)
| InlineCall a
| Field a (Identifier b)
| FieldWrite a (Identifier b) a
| StructInit b [(Str, a)]
| UnionInit b (Str, a)
| EnumInit b TypePath [(Str, a)]
| EnumField a Str Str
| TupleInit [a]
| TupleSlot a Int
| ArrayAccess a a
| ArrayWrite a a a
| Call a [a] [a]
| Cast a b
| Unsafe a
| BlockComment Str
-- e1 ... e2
| RangeLiteral a a
| ArrayLiteral [a]
-- var id[: type] [= default];
| LocalVarDeclaration (Identifier b) b Bool (Maybe a)
-- | Defer a
| Box (TraitImplementation a b) a
| BoxedValue a
| BoxedVtable (TraitDefinition a b) a
| StaticVtable (TraitImplementation a b)
| SizeOf b
| StaticMember TypePath [b] Str
| Implicit b
| Temp a
| Null
| Empty
| Undefined
| InlineCExpr Str b
| VarArg Str
| VarArgListCopy Str
| StaticExpr a
| Yield a
| Tokens Str
| Defined (Identifier b)
| TagExpr a
deriving (Eq, Generic, Show)
instance (Hashable a, Hashable b) => Hashable (ExprType a b)
exprDiscriminant :: (Show a, Show b) => ExprType a b -> Int
exprDiscriminant et = case et of
Block _ -> 1
Using _ _ -> 2
Meta _ _ -> 3
Literal _ _ -> 4
This -> 5
Self -> 6
Identifier _ -> 7
TypeAnnotation _ _ -> 8
PreUnop _ _ -> 9
PostUnop _ _ -> 10
Binop _ _ _ -> 11
For _ _ _ -> 12
While _ _ _ -> 13
If _ _ _ -> 14
Continue -> 15
Break -> 16
Return _ -> 17
StaticExpr _ -> 18
Match _ _ _ -> 19
InlineCall _ -> 20
Field _ _ -> 21
StructInit _ _ -> 22
EnumInit _ _ _ -> 23
EnumField _ _ _ -> 40
ArrayAccess _ _ -> 24
Call _ _ _ -> 25
Cast _ _ -> 26
Unsafe _ -> 27
BlockComment _ -> 28
RangeLiteral _ _ -> 29
ArrayLiteral _ -> 30
LocalVarDeclaration _ _ _ _ -> 31
-- Defer _ -> 32
Box _ _ -> 33
BoxedValue _ -> 34
BoxedVtable _ _ -> 35
SizeOf _ -> 36
TupleInit _ -> 37
TupleSlot _ _ -> 38
StaticMember _ _ _ -> 39
Implicit _ -> 40
Temp _ -> 41
Null -> 42
Empty -> 43
InlineCExpr _ _ -> 44
VarArg _ -> 45
StaticExpr _ -> 46
Yield _ -> 47
Tokens _ -> 48
Undefined -> 49
UnionInit _ _ -> 50
VarArgListCopy _ -> 51
StaticVtable _ -> 52
Defined _ -> 53
ArrayWrite _ _ _ -> 54
FieldWrite _ _ _ -> 55
TagExpr _ -> 56
x -> throwk
$ InternalError ("Expression has no discriminant: " ++ show x) Nothing
exprChildren :: ExprType a b -> [a]
exprChildren et = case et of
Block x -> x
Using _ x -> [x]
Meta _ x -> [x]
TypeAnnotation x _ -> [x]
PreUnop _ x -> [x]
PostUnop _ x -> [x]
Binop _ x y -> [x, y]
For x y z -> [x, y, z]
While x y _ -> [x, y]
If x y (Just z) -> [x, y, z]
If x y Nothing -> [x, y]
Match x _ _ -> [x]
InlineCall x -> [x]
Field x _ -> [x]
StructInit _ fields -> map snd fields
UnionInit _ field -> [snd field]
EnumInit _ _ x -> map snd x
EnumField x _ _ -> [x]
ArrayAccess x y -> [x, y]
Call x implicits args -> x : implicits ++ args
Cast x _ -> [x]
Unsafe x -> [x]
RangeLiteral x y -> [x, y]
ArrayLiteral x -> x
LocalVarDeclaration _ _ _ (Just x) -> [x]
-- Defer x -> [x]
Box _ x -> [x]
BoxedValue x -> [x]
BoxedVtable _ x -> [x]
TupleInit x -> x
TupleSlot x _ -> [x]
Temp x -> [x]
StaticExpr x -> [x]
Yield x -> [x]
ArrayWrite x y z -> [x, y, z]
FieldWrite x s y -> [x, y]
TagExpr x -> [x]
_ -> []
exprMapReduce :: (a -> c) -> (c -> d -> d) -> (a -> ExprType a b) -> d -> a -> d
exprMapReduce mapper reducer getter initialValue ex = foldr
(\a d -> exprMapReduce mapper reducer getter d a)
(reducer (mapper ex) (initialValue))
(exprChildren $ getter ex)
exprFilterMapReduce
:: (a -> Bool)
-> (a -> c)
-> (c -> d -> d)
-> (a -> ExprType a b)
-> d
-> a
-> d
exprFilterMapReduce f mapper reducer getter initialValue ex = foldr
(\a d -> exprFilterMapReduce f mapper reducer getter d a)
(reducer (mapper ex) (initialValue))
(if f ex then exprChildren $ getter ex else [])