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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
*/
#ifndef AST_EXPR_INCLUDED
#define AST_EXPR_INCLUDED
class NodeVisitor;
class ExprNode
{
public:
virtual ~ExprNode() = 0;
virtual void visit(NodeVisitor& nv) = 0;
};
struct ExprNode_Block:
public ExprNode
{
::std::vector< ::std::unique_ptr<ExprNode> > m_nodes;
ExprNode_Block(const ExprNode_Block& x) = delete;
ExprNode_Block(::std::vector< ::std::unique_ptr<ExprNode> >&& nodes):
m_nodes( move(nodes) )
{
}
virtual ~ExprNode_Block() override;
virtual void visit(NodeVisitor& nv) override;
};
struct ExprNode_Macro:
public ExprNode
{
::std::string m_name;
::TokenTree m_tokens;
ExprNode_Macro(::std::string name, ::TokenTree&& tokens):
m_name(name),
m_tokens( move(tokens) )
{}
virtual void visit(NodeVisitor& nv) override;
};
// Return a value
struct ExprNode_Return:
public ExprNode
{
unique_ptr<ExprNode> m_value;
ExprNode_Return(unique_ptr<ExprNode>&& value):
m_value( move(value) )
{
}
virtual void visit(NodeVisitor& nv) override;
};
struct ExprNode_LetBinding:
public ExprNode
{
Pattern m_pat;
unique_ptr<ExprNode> m_value;
ExprNode_LetBinding(Pattern pat, unique_ptr<ExprNode>&& value):
m_pat( move(pat) ),
m_value( move(value) )
{
}
virtual void visit(NodeVisitor& nv) override;
};
struct ExprNode_Assign:
public ExprNode
{
unique_ptr<ExprNode> m_slot;
unique_ptr<ExprNode> m_value;
ExprNode_Assign(unique_ptr<ExprNode>&& slot, unique_ptr<ExprNode>&& value):
m_slot( move(slot) ),
m_value( move(value) )
{
}
virtual void visit(NodeVisitor& nv) override;
};
struct ExprNode_CallPath:
public ExprNode
{
Path m_path;
::std::vector<unique_ptr<ExprNode>> m_args;
ExprNode_CallPath(Path&& path, ::std::vector<unique_ptr<ExprNode>>&& args):
m_path( move(path) ),
m_args( move(args) )
{
}
virtual void visit(NodeVisitor& nv) override;
};
// Call an object (Fn/FnMut/FnOnce)
struct ExprNode_CallObject:
public ExprNode
{
unique_ptr<ExprNode> m_val;
::std::vector<unique_ptr<ExprNode>> m_args;
ExprNode_CallObject(unique_ptr<ExprNode>&& val, ::std::vector< unique_ptr<ExprNode> >&& args):
m_val( move(val) ),
m_args( move(args) )
{
}
virtual void visit(NodeVisitor& nv) override;
};
struct ExprNode_Match:
public ExprNode
{
typedef ::std::vector< ::std::pair<Pattern,unique_ptr<ExprNode> > > arm_t;
unique_ptr<ExprNode> m_val;
arm_t m_arms;
ExprNode_Match(unique_ptr<ExprNode>&& val, arm_t&& arms):
m_val( ::std::move(val) ),
m_arms( ::std::move(arms) )
{
}
virtual void visit(NodeVisitor& nv) override;
};
struct ExprNode_If:
public ExprNode
{
unique_ptr<ExprNode> m_cond;
unique_ptr<ExprNode> m_true;
unique_ptr<ExprNode> m_false;
ExprNode_If(unique_ptr<ExprNode>&& cond, unique_ptr<ExprNode>&& true_code, unique_ptr<ExprNode>&& false_code):
m_cond( ::std::move(cond) ),
m_true( ::std::move(true_code) ),
m_false( ::std::move(false_code) )
{
}
virtual void visit(NodeVisitor& nv) override;
};
// Literal integer
struct ExprNode_Integer:
public ExprNode
{
enum eCoreType m_datatype;
uint64_t m_value;
ExprNode_Integer(uint64_t value, enum eCoreType datatype):
m_datatype(datatype),
m_value(value)
{
}
virtual void visit(NodeVisitor& nv) override;
};
// Literal structure
struct ExprNode_StructLiteral:
public ExprNode
{
typedef ::std::vector< ::std::pair< ::std::string, unique_ptr<ExprNode> > > t_values;
Path m_path;
unique_ptr<ExprNode> m_base_value;
t_values m_values;
ExprNode_StructLiteral(Path path, unique_ptr<ExprNode>&& base_value, t_values&& values ):
m_path( move(path) ),
m_base_value( move(base_value) ),
m_values( move(values) )
{}
virtual void visit(NodeVisitor& nv) override;
};
// Variable / Constant
struct ExprNode_NamedValue:
public ExprNode
{
Path m_path;
ExprNode_NamedValue(Path&& path):
m_path( ::std::move(path) )
{
}
virtual void visit(NodeVisitor& nv) override;
};
// Field dereference
struct ExprNode_Field:
public ExprNode
{
::std::unique_ptr<ExprNode> m_obj;
::std::string m_name;
ExprNode_Field(::std::unique_ptr<ExprNode>&& obj, ::std::string&& name):
m_obj( ::std::move(obj) ),
m_name( ::std::move(name) )
{
}
virtual void visit(NodeVisitor& nv) override;
};
// Type cast ('as')
struct ExprNode_Cast:
public ExprNode
{
unique_ptr<ExprNode> m_value;
TypeRef m_type;
ExprNode_Cast(unique_ptr<ExprNode>&& value, TypeRef&& dst_type):
m_value( move(value) ),
m_type( move(dst_type) )
{
}
virtual void visit(NodeVisitor& nv) override;
};
// Binary operation
struct ExprNode_BinOp:
public ExprNode
{
enum Type {
CMPEQU,
CMPNEQU,
BITAND,
BITOR,
BITXOR,
SHL,
SHR,
};
Type m_type;
::std::unique_ptr<ExprNode> m_left;
::std::unique_ptr<ExprNode> m_right;
ExprNode_BinOp(Type type, ::std::unique_ptr<ExprNode> left, ::std::unique_ptr<ExprNode> right):
m_type(type),
m_left( ::std::move(left) ),
m_right( ::std::move(right) )
{
}
virtual void visit(NodeVisitor& nv) override;
};
class NodeVisitor
{
public:
void visit(const unique_ptr<ExprNode>& cnode) {
if(cnode.get())
cnode->visit(*this);
}
virtual void visit(ExprNode_Block& node) {
for( auto& child : node.m_nodes )
visit(child);
}
virtual void visit(ExprNode_Return& node) {
visit(node.m_value);
}
virtual void visit(ExprNode_LetBinding& node) {
// TODO: Handle recurse into Let pattern
visit(node.m_value);
}
virtual void visit(ExprNode_Assign& node) {
visit(node.m_slot);
visit(node.m_value);
}
virtual void visit(ExprNode_CallPath& node) {
for( auto& arg : node.m_args )
visit(arg);
}
virtual void visit(ExprNode_CallObject& node) {
visit(node.m_val);
for( auto& arg : node.m_args )
visit(arg);
}
virtual void visit(ExprNode_Match& node) {
visit(node.m_val);
for( auto& arm : node.m_arms )
visit(arm.second);
}
virtual void visit(ExprNode_If& node) {
visit(node.m_cond);
visit(node.m_true);
visit(node.m_false);
}
virtual void visit(ExprNode_Integer& node) {
// LEAF
}
virtual void visit(ExprNode_StructLiteral& node) {
visit(node.m_base_value);
for( auto& val : node.m_values )
visit(val.second);
}
virtual void visit(ExprNode_NamedValue& node) {
// LEAF
}
virtual void visit(ExprNode_Field& node) {
visit(node.m_obj);
}
virtual void visit(ExprNode_Cast& node) {
visit(node.m_value);
}
virtual void visit(ExprNode_BinOp& node) {
visit(node.m_left);
visit(node.m_right);
}
};
#endif
|