summaryrefslogtreecommitdiff
path: root/src/ast/pattern.hpp
blob: a9a944fb8a567e471f0943e9d0922f6b067c9664 (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

#ifndef _AST__PATTERN_HPP_INCLUDED_
#define _AST__PATTERN_HPP_INCLUDED_

#include <vector>
#include <memory>
#include <string>
#include <tagged_enum.hpp>

namespace AST {

using ::std::unique_ptr;
using ::std::move;

class ExprNode;

class Pattern:
    public Serialisable
{
public:
    enum BindType {
        MAYBE_BIND,
        ANY,
        REF,
        VALUE,
        TUPLE,
        TUPLE_STRUCT,
    };
private:
    BindType    m_class;
    ::std::string   m_binding;
    Path    m_path;
    unique_ptr<ExprNode>    m_node;
    unique_ptr<ExprNode>    m_node2;    // ONLY used for range values
    ::std::vector<Pattern>  m_sub_patterns;
    
    TAGGED_ENUM(Data, Any,
        (Any,       () ),
        (Ref,       (bool mut; unique_ptr<ExprNode> sub;) ),
        (Value,     (unique_ptr<ExprNode> start; unique_ptr<ExprNode> end;) ),
        (Tuple,     (::std::vector<Pattern> sub_patterns;) ),
        (StructTuple, (Path path; ::std::vector<Pattern> sub_patterns;) ),
        (Struct,    (Path path; ::std::vector< ::std::pair< ::std::string,Pattern> > sub_patterns;) )
        );
    
public:
    Pattern(Pattern&& o) noexcept:
        m_class(o.m_class),
        m_binding( move(o.m_binding) ),
        m_path( move(o.m_path) ),
        m_node( move(o.m_node) ),
        m_sub_patterns( move(o.m_sub_patterns) )
    { }
    
    Pattern(const Pattern& o):
        m_class(o.m_class),
        m_binding(o.m_binding),
        m_path(o.m_path),
        m_node(nullptr),
        m_sub_patterns(o.m_sub_patterns)
    {
        if( o.m_node.get() ) {
            DEBUG("Cloning " << o);
            throw ::std::runtime_error(FMT("Cloning pattern with node : " << o));
        }
    }
    
    Pattern& operator=(Pattern o)
    {
        m_class = o.m_class,
        m_binding = move(o.m_binding);
        m_path = move(o.m_path);
        m_node = move(o.m_node);
        m_sub_patterns = move(o.m_sub_patterns);
        return *this;
    }
    
    Pattern():
        m_class(ANY)
    {}

    // Wildcard = '..', distinct from '_'
    // TODO: Store wildcard as a different pattern type
    struct TagWildcard {};
    Pattern(TagWildcard):
        m_class(ANY)
    {}

    struct TagBind {};
    Pattern(TagBind, ::std::string name):
        m_class(ANY),
        m_binding(name)
    {}

    struct TagMaybeBind {};
    Pattern(TagMaybeBind, ::std::string name):
        m_class(MAYBE_BIND),
        m_binding(name)
    {}

    struct TagValue {};
    Pattern(TagValue, unique_ptr<ExprNode> node, unique_ptr<ExprNode> node2 = 0):
        m_class(VALUE),
        m_node( ::std::move(node) ),
        m_node2( ::std::move(node2) )
    {}
    
    
    struct TagReference {};
    Pattern(TagReference, Pattern sub_pattern):
        m_class(REF),
        m_sub_patterns()
    {
        m_sub_patterns.push_back( ::std::move(sub_pattern) );
    }

    struct TagTuple {};
    Pattern(TagTuple, ::std::vector<Pattern> sub_patterns):
        m_class(TUPLE),
        m_sub_patterns( ::std::move(sub_patterns) )
    {}

    struct TagEnumVariant {};
    Pattern(TagEnumVariant, Path path, ::std::vector<Pattern> sub_patterns):
        m_class(TUPLE_STRUCT),
        m_path( ::std::move(path) ),
        m_sub_patterns( ::std::move(sub_patterns) )
    {}
    
    // Mutators
    void set_bind(::std::string name, bool is_ref, bool is_mut) {
        m_binding = name;
    }
    
    ::std::unique_ptr<ExprNode> take_node() { assert(m_class == VALUE); m_class = ANY; return ::std::move(m_node); }
    
    // Accessors
    const ::std::string& binding() const { return m_binding; }
    BindType type() const { return m_class; }
    ExprNode& node() { return *m_node; }
    const ExprNode& node() const { return *m_node; }
    Path& path() { return m_path; }
    const Path& path() const { return m_path; }
    ::std::vector<Pattern>& sub_patterns() { return m_sub_patterns; }
    const ::std::vector<Pattern>& sub_patterns() const { return m_sub_patterns; }

    friend ::std::ostream& operator<<(::std::ostream& os, const Pattern& pat);

    SERIALISABLE_PROTOTYPES();
};

};

#endif