summaryrefslogtreecommitdiff
path: root/src/ast/expr.hpp
blob: bbb82bdee1b7a4c48541391c07770d568f938281 (plain)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 */
#ifndef AST_EXPR_INCLUDED
#define AST_EXPR_INCLUDED

#include <ostream>
#include <memory>   // unique_ptr
#include <vector>

#include "../parse/tokentree.hpp"
#include "../types.hpp"
#include "pattern.hpp"

namespace AST {

using ::std::unique_ptr;

class NodeVisitor;

class ExprNode:
    public Serialisable
{
    TypeRef m_res_type;
public:
    virtual ~ExprNode() = 0;
    
    virtual void visit(NodeVisitor& nv) = 0;
    //virtual void visit(NodeVisitor& nv) const = 0;
    virtual void print(::std::ostream& os) const = 0;
    
    TypeRef& get_res_type() { return m_res_type; }
    
    friend ::std::ostream& operator<<(::std::ostream& os, const ExprNode& node);
    static ::std::unique_ptr<ExprNode> from_deserialiser(Deserialiser& d);
};

#define NODE_METHODS()  \
	virtual void visit(NodeVisitor& nv) override;\
	virtual void print(::std::ostream& os) const override; \
	SERIALISABLE_PROTOTYPES();/* \
	virtual void visit(NodeVisitor& nv) const override;*/

struct ExprNode_Block:
    public ExprNode
{
    ::std::vector< ::std::unique_ptr<ExprNode> >    m_nodes;

    ExprNode_Block() {}
    ExprNode_Block(::std::vector< ::std::unique_ptr<ExprNode> >&& nodes):
        m_nodes( move(nodes) )
    {
    }
    virtual ~ExprNode_Block() override;
    
    NODE_METHODS();
};

struct ExprNode_Macro:
    public ExprNode
{
    ::std::string   m_name;
    ::TokenTree m_tokens;
    
    ExprNode_Macro() {}
    ExprNode_Macro(::std::string name, ::TokenTree&& tokens):
        m_name(name),
        m_tokens( move(tokens) )
    {}
    
    NODE_METHODS();
};

// Return a value
struct ExprNode_Return:
    public ExprNode
{
    unique_ptr<ExprNode>    m_value;

    ExprNode_Return() {}
    ExprNode_Return(unique_ptr<ExprNode>&& value):
        m_value( move(value) )
    {
    }
    
    NODE_METHODS();
};
struct ExprNode_LetBinding:
    public ExprNode
{
    Pattern m_pat;
    TypeRef m_type;
    unique_ptr<ExprNode>    m_value;

    ExprNode_LetBinding() {}
    ExprNode_LetBinding(Pattern pat, TypeRef type, unique_ptr<ExprNode>&& value):
        m_pat( move(pat) ),
        m_type( move(type) ),
        m_value( move(value) )
    {
    }
    
    NODE_METHODS();
};
struct ExprNode_Assign:
    public ExprNode
{
    unique_ptr<ExprNode>    m_slot;
    unique_ptr<ExprNode>    m_value;

    ExprNode_Assign() {}
    ExprNode_Assign(unique_ptr<ExprNode>&& slot, unique_ptr<ExprNode>&& value):
        m_slot( move(slot) ),
        m_value( move(value) )
    {
    }
    
    NODE_METHODS();
};
struct ExprNode_CallPath:
    public ExprNode
{
    Path    m_path;
    ::std::vector<unique_ptr<ExprNode>> m_args;

    ExprNode_CallPath() {}
    ExprNode_CallPath(Path&& path, ::std::vector<unique_ptr<ExprNode>>&& args):
        m_path( move(path) ),
        m_args( move(args) )
    {
    }
    
    NODE_METHODS();
};
struct ExprNode_CallMethod:
    public ExprNode
{
    unique_ptr<ExprNode>    m_val;
    PathNode    m_method;
    ::std::vector<unique_ptr<ExprNode>> m_args;

    ExprNode_CallMethod() {}
    ExprNode_CallMethod(unique_ptr<ExprNode>&& obj, PathNode&& method, ::std::vector<unique_ptr<ExprNode>>&& args):
        m_val( move(obj) ),
        m_method( move(method) ),
        m_args( move(args) )
    {
    }
    
    NODE_METHODS();
};
// 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() {}
    ExprNode_CallObject(unique_ptr<ExprNode>&& val, ::std::vector< unique_ptr<ExprNode> >&& args):
        m_val( move(val) ),
        m_args( move(args) )
    {
    }
    NODE_METHODS();
};

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() {}
    ExprNode_Match(unique_ptr<ExprNode>&& val, arm_t&& arms):
        m_val( ::std::move(val) ),
        m_arms( ::std::move(arms) )
    {
    }
    NODE_METHODS();
};

struct ExprNode_If:
    public ExprNode
{
    unique_ptr<ExprNode>    m_cond;
    unique_ptr<ExprNode>    m_true;
    unique_ptr<ExprNode>    m_false;

    ExprNode_If() {}
    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) )
    {
    }
    NODE_METHODS();
};
// Literal integer
struct ExprNode_Integer:
    public ExprNode
{
    enum eCoreType  m_datatype;
    uint64_t    m_value;

    ExprNode_Integer() {}
    ExprNode_Integer(uint64_t value, enum eCoreType datatype):
        m_datatype(datatype),
        m_value(value)
    {
    }
    
    NODE_METHODS();
};
// 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() {}
    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) )
    {}
    
    NODE_METHODS();
};
// Tuple
struct ExprNode_Tuple:
    public ExprNode
{
    ::std::vector< unique_ptr<ExprNode> >   m_values;
    
    ExprNode_Tuple() {}
    ExprNode_Tuple(::std::vector< unique_ptr<ExprNode> > vals):
        m_values( ::std::move(vals) )
    {}
    
    NODE_METHODS();
};
// Variable / Constant
struct ExprNode_NamedValue:
    public ExprNode
{
    Path    m_path;

    ExprNode_NamedValue() {}
    ExprNode_NamedValue(Path&& path):
        m_path( ::std::move(path) )
    {
    }
    NODE_METHODS();
};
// Field dereference
struct ExprNode_Field:
    public ExprNode
{
    ::std::unique_ptr<ExprNode> m_obj;
    ::std::string   m_name;

    ExprNode_Field() {}
    ExprNode_Field(::std::unique_ptr<ExprNode>&& obj, ::std::string&& name):
        m_obj( ::std::move(obj) ),
        m_name( ::std::move(name) )
    {
    }
    NODE_METHODS();
};

// Pointer dereference
struct ExprNode_Deref:
    public ExprNode
{
    ::std::unique_ptr<ExprNode>    m_value;
    
    ExprNode_Deref() {}
    ExprNode_Deref(::std::unique_ptr<ExprNode> value):
        m_value( ::std::move(value) )
    {
    }
    
    NODE_METHODS();
};

// Type cast ('as')
struct ExprNode_Cast:
    public ExprNode
{
    unique_ptr<ExprNode>    m_value;
    TypeRef m_type;

    ExprNode_Cast() {}
    ExprNode_Cast(unique_ptr<ExprNode>&& value, TypeRef&& dst_type):
        m_value( move(value) ),
        m_type( move(dst_type) )
    {
    }
    NODE_METHODS();
};

// 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() {}
    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) )
    {
    }
    
    NODE_METHODS();
};

class NodeVisitor
{
public:
    inline void visit(const unique_ptr<ExprNode>& cnode) {
        if(cnode.get())
            cnode->visit(*this);
    }
	virtual bool is_const() const { return false; }
  
    #define NT(nt) \
		virtual void visit(nt& node) = 0/*; \
		virtual void visit(const nt& node) = 0*/
	NT(ExprNode_Block);  
    NT(ExprNode_Macro);
    NT(ExprNode_Return);
    NT(ExprNode_LetBinding);
    NT(ExprNode_Assign);
    NT(ExprNode_CallPath);
    NT(ExprNode_CallMethod);
    NT(ExprNode_CallObject);
    NT(ExprNode_Match);
    NT(ExprNode_If);
    
    NT(ExprNode_Integer);
    NT(ExprNode_StructLiteral);
    NT(ExprNode_Tuple);
    NT(ExprNode_NamedValue);
    
    NT(ExprNode_Field);
    NT(ExprNode_Deref);
    NT(ExprNode_Cast);
    NT(ExprNode_BinOp);
	#undef NT
};
class NodeVisitorDef:
	public NodeVisitor
{
public:
    inline void visit(const unique_ptr<ExprNode>& cnode) {
        if(cnode.get())
            cnode->visit(*this);
    }
    #define NT(nt) \
		virtual void visit(nt& node) override;/* \
		virtual void visit(const nt& node) override*/
	NT(ExprNode_Block);  
    NT(ExprNode_Macro);
    NT(ExprNode_Return);
    NT(ExprNode_LetBinding);
    NT(ExprNode_Assign);
    NT(ExprNode_CallPath);
    NT(ExprNode_CallMethod);
    NT(ExprNode_CallObject);
    NT(ExprNode_Match);
    NT(ExprNode_If);
    
    NT(ExprNode_Integer);
    NT(ExprNode_StructLiteral);
    NT(ExprNode_Tuple);
    NT(ExprNode_NamedValue);
    
    NT(ExprNode_Field);
    NT(ExprNode_Deref);
    NT(ExprNode_Cast);
    NT(ExprNode_BinOp);
	#undef NT
};

class Expr:
    public Serialisable
{
    ::std::shared_ptr<ExprNode> m_node;
public:
    Expr(unique_ptr<ExprNode> node):
        m_node(node.release())
    {
    }
    Expr(ExprNode* node):
        m_node(node)
    {
    }
    Expr():
        m_node(nullptr)
    {
    }

    bool is_valid() const { return m_node.get() != nullptr; }
    ExprNode& node() { assert(m_node.get()); return *m_node; }
    const ExprNode& node() const { assert(m_node.get()); return *m_node; }
    ::std::shared_ptr<ExprNode> take_node() { assert(m_node.get()); return ::std::move(m_node); }
    void visit_nodes(NodeVisitor& v);
    void visit_nodes(NodeVisitor& v) const;

    friend ::std::ostream& operator<<(::std::ostream& os, const Expr& pat);
    
    SERIALISABLE_PROTOTYPES();
};

}

#endif