/
make_expr.cpp
397 lines (347 loc) · 16.5 KB
/
make_expr.cpp
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
////////////////////////////////////////////////////////////////////////////////////////////////////
// make_expr.cpp
//
// Copyright 2012 Eric Niebler. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/proto/v5/proto.hpp>
#include <boost/utility/addressof.hpp>
#include <boost/fusion/tuple.hpp>
#include "./unit_test.hpp"
namespace mpl = boost::mpl;
namespace proto = boost::proto;
namespace fusion = boost::fusion;
using proto::_;
template<typename E>
struct ewrap;
struct mydomain
: proto::domain<mydomain>
{
using make_expr = proto::make_custom_expr<ewrap>;
};
template<typename E>
struct ewrap
: proto::expr<E, mydomain>
{
BOOST_PROTO_REGULAR_TRIVIAL_CLASS(ewrap);
using proto::expr<E, mydomain>::expr;
};
void test_make_expr()
{
int i = 42;
proto::expr<proto::terminal(int)> t1 = proto::make_expr(proto::terminal(), 1);
proto::expr<proto::terminal(int &)> t2 = proto::make_expr(proto::terminal(), i);
proto::expr<proto::unary_plus(proto::terminal(int))> p1 = proto::make_expr(proto::unary_plus(), 1);
proto::expr<proto::unary_plus(proto::terminal(int &))> p2 = proto::make_expr(proto::unary_plus(), i);
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p2)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p2)), &i);
BOOST_PROTO_IGNORE_UNUSED(t1, t2, p1, p2);
using p3_type = ewrap<proto::unary_plus(ewrap<proto::terminal(int &)>)>;
p3_type p3 = proto::make_expr<mydomain>(proto::unary_plus(), i);
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p3)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p3)), &i);
ewrap<proto::plus(p3_type &, ewrap<proto::terminal(int)>)> p4 = proto::make_expr(proto::plus(), p3, 0);
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(proto::left(p4))), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(proto::left(p4))), &i);
}
void test_make_expr_2()
{
int i = 42;
proto::expr<proto::terminal(int)> t1 = proto::make_expr<proto::terminal>(1);
proto::expr<proto::terminal(int &)> t2 = proto::make_expr<proto::terminal>(i);
proto::expr<proto::unary_plus(proto::terminal(int))> p1 = proto::make_expr<proto::unary_plus>(1);
proto::expr<proto::unary_plus(proto::terminal(int &))> p2 = proto::make_expr<proto::unary_plus>(i);
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p2)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p2)), &i);
BOOST_PROTO_IGNORE_UNUSED(t1, t2, p1, p2);
using p3_type = ewrap<proto::unary_plus(ewrap<proto::terminal(int &)>)>;
p3_type p3 = proto::make_expr<proto::unary_plus, mydomain>(i);
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p3)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p3)), &i);
ewrap<proto::plus(p3_type &, ewrap<proto::terminal(int)>)> p4 = proto::make_expr<proto::plus>(p3, 0);
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(proto::left(p4))), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(proto::left(p4))), &i);
}
void test_make_expr_functional()
{
int i = 42;
proto::expr<proto::terminal(int)> t1 = proto::functional::make_expr<>()(proto::terminal(), 1);
proto::expr<proto::terminal(int &)> t2 = proto::functional::make_expr<>()(proto::terminal(), i);
proto::expr<proto::unary_plus(proto::terminal(int))> p1 = proto::functional::make_expr<>()(proto::unary_plus(), 1);
proto::expr<proto::unary_plus(proto::terminal(int &))> p2 = proto::functional::make_expr<>()(proto::unary_plus(), i);
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p2)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p2)), &i);
BOOST_PROTO_IGNORE_UNUSED(t1, t2, p1, p2);
using p3_type = ewrap<proto::unary_plus(ewrap<proto::terminal(int &)>)>;
p3_type p3 = proto::functional::make_expr<mydomain>()(proto::unary_plus(), i);
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p3)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p3)), &i);
ewrap<proto::plus(p3_type &, ewrap<proto::terminal(int)>)> p4 = proto::functional::make_expr<>()(proto::plus(), p3, 0);
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(proto::left(p4))), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(proto::left(p4))), &i);
}
void test_make_expr_functional_2()
{
int i = 42;
proto::expr<proto::terminal(int)> t1 = proto::functional::make_expr<proto::terminal>()(1);
proto::expr<proto::terminal(int &)> t2 = proto::functional::make_expr<proto::terminal>()(i);
proto::expr<proto::unary_plus(proto::terminal(int))> p1 = proto::functional::make_expr<proto::unary_plus>()(1);
proto::expr<proto::unary_plus(proto::terminal(int &))> p2 = proto::functional::make_expr<proto::unary_plus>()(i);
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p2)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p2)), &i);
BOOST_PROTO_IGNORE_UNUSED(t1, t2, p1, p2);
using p3_type = ewrap<proto::unary_plus(ewrap<proto::terminal(int &)>)>;
p3_type p3 = proto::functional::make_expr<proto::unary_plus, mydomain>()(i);
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p3)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p3)), &i);
ewrap<proto::plus(p3_type &, ewrap<proto::terminal(int)>)> p4 = proto::functional::make_expr<proto::plus>()(p3, 0);
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(proto::left(p4))), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(proto::left(p4))), &i);
}
void test_unpack_expr()
{
int i = 42;
proto::expr<proto::terminal(int)> t1 = proto::unpack_expr(proto::terminal(), fusion::make_tuple(1));
proto::expr<proto::terminal(int &)> t2 = proto::unpack_expr(proto::terminal(), fusion::make_tuple(boost::ref(i)));
proto::expr<proto::unary_plus(proto::terminal(int))> p1 = proto::unpack_expr(proto::unary_plus(), fusion::make_tuple(1));
proto::expr<proto::unary_plus(proto::terminal(int &))> p2 = proto::unpack_expr(proto::unary_plus(), fusion::make_tuple(boost::ref(i)));
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p2)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p2)), &i);
BOOST_PROTO_IGNORE_UNUSED(t1, t2, p1, p2);
using p3_type = ewrap<proto::unary_plus(ewrap<proto::terminal(int &)>)>;
p3_type p3 = proto::unpack_expr<mydomain>(proto::unary_plus(), fusion::make_tuple(boost::ref(i)));
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p3)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p3)), &i);
ewrap<proto::plus(p3_type &, ewrap<proto::terminal(int)>)> p4 = proto::unpack_expr(proto::plus(), fusion::make_tuple(boost::ref(p3), 0));
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(proto::left(p4))), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(proto::left(p4))), &i);
}
void test_unpack_expr_2()
{
int i = 42;
proto::expr<proto::terminal(int)> t1 = proto::unpack_expr<proto::terminal>(fusion::make_tuple(1));
proto::expr<proto::terminal(int &)> t2 = proto::unpack_expr<proto::terminal>(fusion::make_tuple(boost::ref(i)));
proto::expr<proto::unary_plus(proto::terminal(int))> p1 = proto::unpack_expr<proto::unary_plus>(fusion::make_tuple(1));
proto::expr<proto::unary_plus(proto::terminal(int &))> p2 = proto::unpack_expr<proto::unary_plus>(fusion::make_tuple(boost::ref(i)));
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p2)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p2)), &i);
BOOST_PROTO_IGNORE_UNUSED(t1, t2, p1, p2);
using p3_type = ewrap<proto::unary_plus(ewrap<proto::terminal(int &)>)>;
p3_type p3 = proto::unpack_expr<proto::unary_plus, mydomain>(fusion::make_tuple(boost::ref(i)));
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p3)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p3)), &i);
ewrap<proto::plus(p3_type &, ewrap<proto::terminal(int)>)> p4 = proto::unpack_expr<proto::plus>(fusion::make_tuple(boost::ref(p3), 0));
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(proto::left(p4))), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(proto::left(p4))), &i);
}
void test_unpack_expr_functional()
{
int i = 42;
proto::expr<proto::terminal(int)> t1 = proto::functional::unpack_expr<>()(proto::terminal(), fusion::make_tuple(1));
proto::expr<proto::terminal(int &)> t2 = proto::functional::unpack_expr<>()(proto::terminal(), fusion::make_tuple(boost::ref(i)));
proto::expr<proto::unary_plus(proto::terminal(int))> p1 = proto::functional::unpack_expr<>()(proto::unary_plus(), fusion::make_tuple(1));
proto::expr<proto::unary_plus(proto::terminal(int &))> p2 = proto::functional::unpack_expr<>()(proto::unary_plus(), fusion::make_tuple(boost::ref(i)));
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p2)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p2)), &i);
BOOST_PROTO_IGNORE_UNUSED(t1, t2, p1, p2);
using p3_type = ewrap<proto::unary_plus(ewrap<proto::terminal(int &)>)>;
p3_type p3 = proto::functional::unpack_expr<mydomain>()(proto::unary_plus(), fusion::make_tuple(boost::ref(i)));
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p3)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p3)), &i);
ewrap<proto::plus(p3_type &, ewrap<proto::terminal(int)>)> p4 = proto::functional::unpack_expr<>()(proto::plus(), fusion::make_tuple(boost::ref(p3), 0));
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(proto::left(p4))), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(proto::left(p4))), &i);
}
void test_unpack_expr_functional_2()
{
int i = 42;
proto::expr<proto::terminal(int)> t1 = proto::functional::unpack_expr<proto::terminal>()(fusion::make_tuple(1));
proto::expr<proto::terminal(int &)> t2 = proto::functional::unpack_expr<proto::terminal>()(fusion::make_tuple(boost::ref(i)));
proto::expr<proto::unary_plus(proto::terminal(int))> p1 = proto::functional::unpack_expr<proto::unary_plus>()(fusion::make_tuple(1));
proto::expr<proto::unary_plus(proto::terminal(int &))> p2 = proto::functional::unpack_expr<proto::unary_plus>()(fusion::make_tuple(boost::ref(i)));
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p2)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p2)), &i);
BOOST_PROTO_IGNORE_UNUSED(t1, t2, p1, p2);
using p3_type = ewrap<proto::unary_plus(ewrap<proto::terminal(int &)>)>;
p3_type p3 = proto::functional::unpack_expr<proto::unary_plus, mydomain>()(fusion::make_tuple(boost::ref(i)));
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(p3)), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(p3)), &i);
ewrap<proto::plus(p3_type &, ewrap<proto::terminal(int)>)> p4 = proto::functional::unpack_expr<proto::plus>()(fusion::make_tuple(boost::ref(p3), 0));
BOOST_CHECK_EQUAL(proto::value(proto::child<0>(proto::left(p4))), 42);
BOOST_CHECK_EQUAL(&proto::value(proto::child<0>(proto::left(p4))), &i);
}
// Turn all terminals held by reference into ones held by value
struct ByVal
: proto::def<
proto::match(
proto::case_(
proto::terminal(proto::_)
, proto::terminal(proto::utility::by_val(proto::_value))
)
, proto::case_(
proto::_(ByVal...)
, proto::pass
)
)
>
{};
// Turn all terminals held by value into ones held by reference (not safe in general)
struct ByRef
: proto::def<
proto::match(
proto::case_(
proto::terminal(proto::_)
, proto::terminal(proto::utility::by_ref(proto::_value))
)
, proto::case_(
proto::_(ByRef...)
, proto::pass
)
)
>
{};
// turn all proto::plus nodes to minus nodes:
struct Minus
: proto::def<
proto::match(
proto::case_(
proto::terminal(proto::_)
, proto::pass
)
, proto::case_(
proto::plus(Minus, Minus)
, proto::minus(Minus(proto::_left), Minus(proto::_right))
)
)
>
{};
struct Square
: proto::def<
proto::match(
proto::case_(
proto::terminal(proto::_)
, proto::multiplies(proto::_, proto::_)
)
, proto::case_(
proto::plus(Square, Square)
, proto::pass
)
)
>
{};
void test_make_expr_transform()
{
proto::expr<
proto::plus(
proto::terminal(int)
, proto::terminal(int)
)
> t1 = ByVal()(proto::as_expr(1) + 1);
proto::expr<
proto::plus(
proto::terminal(int &)
, proto::terminal(int &)
)
> t2 = ByRef()(proto::as_expr(1) + 1);
proto::expr<
proto::minus(
proto::terminal(int)
, proto::terminal(int)
)
> t3 = Minus()(proto::as_expr(1) + 1);
proto::expr<
proto::plus(
proto::multiplies(proto::terminal(int), proto::terminal(int))
, proto::multiplies(proto::terminal(int), proto::terminal(int))
)
> t4 = Square()(proto::as_expr(1) + 1);
BOOST_PROTO_IGNORE_UNUSED(t1, t2, t3, t4);
}
template<typename ExprDesc>
struct by_ref_expr;
struct by_ref_domain
: proto::domain<by_ref_domain>
{
using make_expr = proto::make_custom_expr<by_ref_expr>;
using store_child = proto::utility::by_ref;
};
template<typename ExprDesc>
struct by_ref_expr
: proto::basic_expr<ExprDesc, by_ref_domain>
, proto::expr_function<by_ref_expr<ExprDesc>>
{
BOOST_PROTO_REGULAR_TRIVIAL_CLASS(by_ref_expr);
using proto::basic_expr<ExprDesc, by_ref_domain>::basic_expr;
};
struct length_impl {};
struct dot_impl {};
constexpr by_ref_expr<proto::terminal(length_impl)> length{};
constexpr by_ref_expr<proto::terminal(dot_impl)> dot{};
// convert length(a) < length(b) to dot(a,a) < dot(b,b)
struct Convert
: proto::def<
proto::match(
proto::case_(
proto::less(
proto::function(proto::terminal(length_impl), proto::_)
, proto::function(proto::terminal(length_impl), proto::_)
)
, proto::less(
proto::function(
proto::terminal(dot_impl())
, proto::_child<1>(proto::_child<0>)
, proto::_child<1>(proto::_child<0>)
)
, proto::function(
proto::terminal(dot_impl())
, proto::_child<1>(proto::_child<1>)
, proto::_child<1>(proto::_child<1>)
)
)
)
)
>
{};
void test_make_expr_transform_2()
{
auto expr = proto::deep_copy(length(1) < length(2));
// Test that intermediate nodes are stored by value, even though the
// store_child policy is by_ref
by_ref_expr<proto::less(
by_ref_expr<proto::function(
proto::expr<proto::terminal(dot_impl)> // BUGBUG would be nice if this were by_ref_expr
, by_ref_expr<proto::terminal(int)> &
, by_ref_expr<proto::terminal(int)> &
)>
, by_ref_expr<proto::function(
proto::expr<proto::terminal(dot_impl)> // BUGBUG would be nice if this were by_ref_expr
, by_ref_expr<proto::terminal(int)> &
, by_ref_expr<proto::terminal(int)> &
)>
)> res = Convert()(expr);
void const *addr1 = boost::addressof(proto::child<1>(proto::child<0>(expr)));
void const *addr2 = boost::addressof(proto::child<1>(proto::child<0>(res)));
BOOST_CHECK_EQUAL(addr1, addr2);
BOOST_CHECK_EQUAL(1, proto::value(proto::child<1>(proto::child<0>(expr))));
BOOST_CHECK_EQUAL(1, proto::value(proto::child<1>(proto::child<0>(res))));
}
using namespace boost::unit_test;
////////////////////////////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite *test = BOOST_TEST_SUITE("test proto::make_expr, proto::unpack_expr and friends");
test->add(BOOST_TEST_CASE(&test_make_expr));
test->add(BOOST_TEST_CASE(&test_make_expr_2));
test->add(BOOST_TEST_CASE(&test_make_expr_functional));
test->add(BOOST_TEST_CASE(&test_make_expr_functional_2));
test->add(BOOST_TEST_CASE(&test_unpack_expr));
test->add(BOOST_TEST_CASE(&test_unpack_expr_2));
test->add(BOOST_TEST_CASE(&test_unpack_expr_functional));
test->add(BOOST_TEST_CASE(&test_unpack_expr_functional_2));
test->add(BOOST_TEST_CASE(&test_unpack_expr_functional));
test->add(BOOST_TEST_CASE(&test_make_expr_transform));
test->add(BOOST_TEST_CASE(&test_make_expr_transform_2));
return test;
}