summaryrefslogtreecommitdiff
path: root/src/ast/pattern.cpp
blob: 1b189fa9a695b4b417a1d00279f9b0e786e01e78 (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
/*
 * MRustC - Rust Compiler
 * - By John Hodge (Mutabah/thePowersGang)
 *
 * ast/pattern.cpp
 * - AST::Pattern support/implementation code
 */
#include "../common.hpp"
#include "ast.hpp"
#include "pattern.hpp"

namespace AST {

::std::ostream& operator<<(::std::ostream& os, const Pattern& pat)
{
    os << "Pattern(" << pat.m_binding << " @ ";
    TU_MATCH(Pattern::Data, (pat.m_data), (ent),
    (Any,
        os << "_";
        ),
    (MaybeBind,
        os << "?";
        ),
    (Ref,
        os << "&" << (ent.mut ? "mut " : "") << *ent.sub;
        ),
    (Value,
        os << *ent.start;
        if( ent.end.get() )
            os << " ... " << *ent.end;
        ),
    (Tuple,
        os << "(" << ent.sub_patterns << ")";
        ),
    (StructTuple,
        os << ent.path << " (" << ent.sub_patterns << ")";
        ),
    (Struct,
        os << ent.path << " {" << ent.sub_patterns << "}";
        )
    )
    os << ")";
    return os;
}
void operator%(Serialiser& s, Pattern::Data::Tag c) {
    s << Pattern::Data::tag_to_str(c);
}
void operator%(::Deserialiser& s, Pattern::Data::Tag& c) {
    ::std::string   n;
    s.item(n);
    c = Pattern::Data::tag_from_str(n);
}
#define _D(VAR, ...)  case Pattern::Data::TAG_##VAR: { m_data = Pattern::Data::make_null_##VAR(); auto& ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
SERIALISE_TYPE(Pattern::, "Pattern", {
    s.item(m_binding);
    s % m_data.tag();
    TU_MATCH(Pattern::Data, (m_data), (e),
    (Any,
        ),
    (MaybeBind,
        ),
    (Ref,
        s << e.mut;
        s << e.sub;
        ),
    (Value,
        s << e.start;
        s << e.end;
        ),
    (Tuple,
        s << e.sub_patterns;
        ),
    (StructTuple,
        s << e.path;
        s << e.sub_patterns;
        ),
    (Struct,
        s << e.path;
        s << e.sub_patterns;
        )
    )
},{
    s.item(m_binding);
    Pattern::Data::Tag  tag;
    s % tag;
    switch(tag)
    {
    _D(Any, )
    _D(MaybeBind,
        )
    _D(Ref,
        s.item( ent.mut );
        s.item( ent.sub );
        )
    _D(Value,
        s.item( ent.start );
        s.item( ent.end );
        )
    _D(Tuple,
        s.item( ent.sub_patterns );
        )
    _D(StructTuple,
        s.item( ent.path );
        s.item( ent.sub_patterns );
        )
    _D(Struct,
        s.item( ent.path );
        s.item( ent.sub_patterns );
        )
    }
});

}