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
|
/*
* 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::Value& val)
{
TU_MATCH(Pattern::Value, (val), (e),
(Invalid,
os << "/*BAD PAT VAL*/";
),
(Integer,
switch(e.type)
{
case CORETYPE_BOOL:
os << (e.value ? "true" : "false");
break;
case CORETYPE_F32:
case CORETYPE_F64:
BUG(Span(), "Hit F32/f64 in printing pattern literal");
break;
default:
os << e.value;
break;
}
),
(Float,
switch(e.type)
{
case CORETYPE_BOOL:
os << (e.value ? "true" : "false");
break;
case CORETYPE_ANY:
case CORETYPE_F32:
case CORETYPE_F64:
os << e.value;
break;
default:
BUG(Span(), "Hit integer in printing pattern literal");
break;
}
),
(String,
os << "\"" << e << "\"";
),
(ByteString,
os << "b\"" << e.v << "\"";
),
(Named,
os << e;
)
)
return os;
}
::std::ostream& operator<<(::std::ostream& os, const Pattern::TuplePat& val)
{
if( val.has_wildcard )
{
os << val.start;
os << ".., ";
os << val.end;
}
else
{
os << val.start;
assert(val.end.size() == 0);
}
return os;
}
::std::ostream& operator<<(::std::ostream& os, const Pattern& pat)
{
os << "Pattern(";
if( pat.m_binding.is_valid() ) {
os << pat.m_binding.m_name << " @ ";
}
TU_MATCH(Pattern::Data, (pat.m_data), (ent),
(MaybeBind,
os << ent.name << "?";
),
(Macro,
os << *ent.inv;
),
(Any,
os << "_";
),
(Box,
os << "box " << *ent.sub;
),
(Ref,
os << "&" << (ent.mut ? "mut " : "") << *ent.sub;
),
(Value,
os << ent.start;
if( ! ent.end.is_Invalid() )
os << " ... " << ent.end;
),
(Tuple,
os << "(" << ent << ")";
),
(StructTuple,
os << ent.path << " (" << ent.tup_pat << ")";
),
(Struct,
os << ent.path << " {" << ent.sub_patterns << "}";
),
(Slice,
os << "[";
bool needs_comma = false;
if(ent.leading.size()) {
os << ent.leading;
needs_comma = true;
}
if(ent.extra_bind.size() > 0) {
if( needs_comma ) {
os << ", ";
}
if(ent.extra_bind != "_")
os << ent.extra_bind;
os << "..";
needs_comma = true;
}
if(ent.trailing.size()) {
if( needs_comma ) {
os << ", ";
}
os << ent.trailing;
}
os << "]";
)
)
os << ")";
return os;
}
void operator%(Serialiser& s, Pattern::Value::Tag c) {
}
void operator%(::Deserialiser& s, Pattern::Value::Tag& c) {
}
void operator%(::Serialiser& s, const Pattern::Value& v) {
}
void operator%(::Deserialiser& s, Pattern::Value& v) {
}
void operator%(Serialiser& s, Pattern::Data::Tag c) {
}
void operator%(::Deserialiser& s, Pattern::Data::Tag& c) {
}
Pattern::~Pattern()
{
}
AST::Pattern AST::Pattern::clone() const
{
AST::Pattern rv;
rv.m_span = m_span;
rv.m_binding = m_binding;
struct H {
static ::std::unique_ptr<Pattern> clone_sp(const ::std::unique_ptr<Pattern>& p) {
return ::std::make_unique<Pattern>( p->clone() );
}
static ::std::vector<Pattern> clone_list(const ::std::vector<Pattern>& list) {
::std::vector<Pattern> rv;
rv.reserve(list.size());
for(const auto& p : list)
rv.push_back( p.clone() );
return rv;
}
static TuplePat clone_tup(const TuplePat& p) {
return TuplePat {
H::clone_list(p.start),
p.has_wildcard,
H::clone_list(p.end)
};
}
static AST::Pattern::Value clone_val(const AST::Pattern::Value& v) {
TU_MATCH(::AST::Pattern::Value, (v), (e),
(Invalid, return Value(e);),
(Integer, return Value(e);),
(Float, return Value(e);),
(String, return Value(e);),
(ByteString, return Value(e);),
(Named, return Value::make_Named( AST::Path(e) );)
)
throw "";
}
};
TU_MATCH(Pattern::Data, (m_data), (e),
(Any,
rv.m_data = Data::make_Any(e);
),
(MaybeBind,
rv.m_data = Data::make_MaybeBind(e);
),
(Macro,
rv.m_data = Data::make_Macro({ ::std::make_unique<AST::MacroInvocation>( e.inv->clone() ) });
),
(Box,
rv.m_data = Data::make_Box({ H::clone_sp(e.sub) });
),
(Ref,
rv.m_data = Data::make_Ref({ e.mut, H::clone_sp(e.sub) });
),
(Value,
rv.m_data = Data::make_Value({ H::clone_val(e.start), H::clone_val(e.end) });
),
(Tuple,
rv.m_data = Data::make_Tuple({ H::clone_tup(e) });
),
(StructTuple,
rv.m_data = Data::make_StructTuple({ ::AST::Path(e.path), H::clone_tup(e.tup_pat) });
),
(Struct,
::std::vector< ::std::pair< ::std::string, Pattern> > sps;
for(const auto& sp : e.sub_patterns)
sps.push_back( ::std::make_pair(sp.first, sp.second.clone()) );
rv.m_data = Data::make_Struct({ ::AST::Path(e.path), mv$(sps) });
),
(Slice,
rv.m_data = Data::make_Slice({ H::clone_list(e.leading), e.extra_bind, H::clone_list(e.trailing) });
)
)
return rv;
}
#define _D(VAR, ...) case Pattern::Data::TAG_##VAR: { m_data = Pattern::Data::make_##VAR({}); auto& ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
SERIALISE_TYPE(Pattern::, "Pattern", {
},{
});
}
|