summaryrefslogtreecommitdiff
path: root/src/hir/pattern.cpp
blob: 64b132a31ef26be15d2e74bf59ac1345bd3a8b1d (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

#include "pattern.hpp"

namespace HIR {
    ::std::ostream& operator<<(::std::ostream& os, const Pattern::Value& x) {
        TU_MATCH(Pattern::Value, (x), (e),
        (Integer,
            // TODO: Print with type
            os << e.value;
            ),
        (String,
            os << "\"" << e << "\"";
            ),
        (Named,
            os << e;
            )
        )
        return os;
    }
    ::std::ostream& operator<<(::std::ostream& os, const Pattern& x) {
        if( x.m_binding.is_valid() ) {
            if( x.m_binding.m_mutable )
                os << "mut ";
            switch(x.m_binding.m_type)
            {
            case PatternBinding::Type::Move:    break;
            case PatternBinding::Type::Ref:     os << "ref ";   break;
            case PatternBinding::Type::MutRef:  os << "ref mut ";   break;
            }
            os << x.m_binding.m_name << " @ ";
        }
        TU_MATCH(Pattern::Data, (x.m_data), (e),
        (Any,
            os << "_";
            ),
        (Box,
            os << "box " << *e.sub;
            ),
        (Ref,
            switch(e.type)
            {
            case BorrowType::Shared:    os << "&";  break;
            case BorrowType::Unique:    os << "&mut ";   break;
            case BorrowType::Owned:     os << "&move ";   break;
            }
            os << *e.sub;
            ),
        (Tuple,
            os << "(";
            for(const auto& s : e.sub_patterns)
                os << s << ", ";
            os << ")";
            ),
        (StructTuple,
            os << e.path;
            os << "(";
            for(const auto& s : e.sub_patterns)
                os << s << ", ";
            os << ")";
            ),
        (StructTupleWildcard,
            os << e.path;
            ),
        (Struct,
            os << e.path;
            os << "{ ";
            for(const auto& ns : e.sub_patterns)
                os << ns.first << ": " << ns.second << ", ";
            os << "}";
            ),
        
        (Value,
            os << e.val;
            ),
        (Range,
            os << e.start << " ... " << e.end;
            ),
        
        (EnumTuple,
            os << e.path;
            os << "(";
            for(const auto& s : e.sub_patterns)
                os << s << ", ";
            os << ")";
            ),
        (EnumTupleWildcard,
            os << e.path;
            ),
        (EnumStruct,
            os << e.path;
            os << "{ ";
            for(const auto& ns : e.sub_patterns)
                os << ns.first << ": " << ns.second << ", ";
            os << "}";
            ),
        (Slice,
            os << "[";
            for(const auto& s : e.sub_patterns)
                os << s << ", ";
            os << "]";
            ),
        (SplitSlice,
            os << "[";
            for(const auto& s : e.leading)
                os << s << ", ";
            if( e.extra_bind.is_valid() ) {
                os << e.extra_bind.m_name << " @ ";
            }
            os << ".. ";
            for(const auto& s : e.trailing)
                os << s << ", ";
            os << "]";
            )
        )
        return os;
    }
}