sl@0
|
1 |
/*=============================================================================
|
sl@0
|
2 |
Phoenix V1.2.1
|
sl@0
|
3 |
Copyright (c) 2001-2002 Joel de Guzman
|
sl@0
|
4 |
|
sl@0
|
5 |
Use, modification and distribution is subject to the Boost Software
|
sl@0
|
6 |
License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
sl@0
|
7 |
http://www.boost.org/LICENSE_1_0.txt)
|
sl@0
|
8 |
==============================================================================*/
|
sl@0
|
9 |
#ifndef PHOENIX_STATEMENTS_HPP
|
sl@0
|
10 |
#define PHOENIX_STATEMENTS_HPP
|
sl@0
|
11 |
|
sl@0
|
12 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
13 |
#include <boost/spirit/phoenix/composite.hpp>
|
sl@0
|
14 |
|
sl@0
|
15 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
16 |
namespace phoenix {
|
sl@0
|
17 |
|
sl@0
|
18 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
19 |
//
|
sl@0
|
20 |
// sequential_composite
|
sl@0
|
21 |
//
|
sl@0
|
22 |
// Two or more actors separated by the comma generates a
|
sl@0
|
23 |
// sequential_composite which is a composite actor. Example:
|
sl@0
|
24 |
//
|
sl@0
|
25 |
// actor,
|
sl@0
|
26 |
// actor,
|
sl@0
|
27 |
// actor
|
sl@0
|
28 |
//
|
sl@0
|
29 |
// The actors are evaluated sequentially. The result type of this
|
sl@0
|
30 |
// is void. Note that the last actor should not have a trailing
|
sl@0
|
31 |
// comma.
|
sl@0
|
32 |
//
|
sl@0
|
33 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
34 |
template <typename A0, typename A1>
|
sl@0
|
35 |
struct sequential_composite {
|
sl@0
|
36 |
|
sl@0
|
37 |
typedef sequential_composite<A0, A1> self_t;
|
sl@0
|
38 |
|
sl@0
|
39 |
template <typename TupleT>
|
sl@0
|
40 |
struct result { typedef void type; };
|
sl@0
|
41 |
|
sl@0
|
42 |
sequential_composite(A0 const& _0, A1 const& _1)
|
sl@0
|
43 |
: a0(_0), a1(_1) {}
|
sl@0
|
44 |
|
sl@0
|
45 |
template <typename TupleT>
|
sl@0
|
46 |
void
|
sl@0
|
47 |
eval(TupleT const& args) const
|
sl@0
|
48 |
{
|
sl@0
|
49 |
a0.eval(args);
|
sl@0
|
50 |
a1.eval(args);
|
sl@0
|
51 |
}
|
sl@0
|
52 |
|
sl@0
|
53 |
A0 a0; A1 a1; // actors
|
sl@0
|
54 |
};
|
sl@0
|
55 |
|
sl@0
|
56 |
//////////////////////////////////
|
sl@0
|
57 |
template <typename BaseT0, typename BaseT1>
|
sl@0
|
58 |
inline actor<sequential_composite<actor<BaseT0>, actor<BaseT1> > >
|
sl@0
|
59 |
operator,(actor<BaseT0> const& _0, actor<BaseT1> const& _1)
|
sl@0
|
60 |
{
|
sl@0
|
61 |
return sequential_composite<actor<BaseT0>, actor<BaseT1> >(_0, _1);
|
sl@0
|
62 |
}
|
sl@0
|
63 |
|
sl@0
|
64 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
65 |
//
|
sl@0
|
66 |
// if_then_else_composite
|
sl@0
|
67 |
//
|
sl@0
|
68 |
// This composite has two (2) forms:
|
sl@0
|
69 |
//
|
sl@0
|
70 |
// if_(condition)
|
sl@0
|
71 |
// [
|
sl@0
|
72 |
// statement
|
sl@0
|
73 |
// ]
|
sl@0
|
74 |
//
|
sl@0
|
75 |
// and
|
sl@0
|
76 |
//
|
sl@0
|
77 |
// if_(condition)
|
sl@0
|
78 |
// [
|
sl@0
|
79 |
// true_statement
|
sl@0
|
80 |
// ]
|
sl@0
|
81 |
// .else_
|
sl@0
|
82 |
// [
|
sl@0
|
83 |
// false_statement
|
sl@0
|
84 |
// ]
|
sl@0
|
85 |
//
|
sl@0
|
86 |
// where condition is an actor that evaluates to bool. If condition
|
sl@0
|
87 |
// is true, the true_statement (again an actor) is executed
|
sl@0
|
88 |
// otherwise, the false_statement (another actor) is executed. The
|
sl@0
|
89 |
// result type of this is void. Note the trailing underscore after
|
sl@0
|
90 |
// if_ and the the leading dot and the trailing underscore before
|
sl@0
|
91 |
// and after .else_.
|
sl@0
|
92 |
//
|
sl@0
|
93 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
94 |
template <typename CondT, typename ThenT, typename ElseT>
|
sl@0
|
95 |
struct if_then_else_composite {
|
sl@0
|
96 |
|
sl@0
|
97 |
typedef if_then_else_composite<CondT, ThenT, ElseT> self_t;
|
sl@0
|
98 |
|
sl@0
|
99 |
template <typename TupleT>
|
sl@0
|
100 |
struct result {
|
sl@0
|
101 |
|
sl@0
|
102 |
typedef void type;
|
sl@0
|
103 |
};
|
sl@0
|
104 |
|
sl@0
|
105 |
if_then_else_composite(
|
sl@0
|
106 |
CondT const& cond_,
|
sl@0
|
107 |
ThenT const& then_,
|
sl@0
|
108 |
ElseT const& else__)
|
sl@0
|
109 |
: cond(cond_), then(then_), else_(else__) {}
|
sl@0
|
110 |
|
sl@0
|
111 |
template <typename TupleT>
|
sl@0
|
112 |
void eval(TupleT const& args) const
|
sl@0
|
113 |
{
|
sl@0
|
114 |
if (cond.eval(args))
|
sl@0
|
115 |
then.eval(args);
|
sl@0
|
116 |
else
|
sl@0
|
117 |
else_.eval(args);
|
sl@0
|
118 |
}
|
sl@0
|
119 |
|
sl@0
|
120 |
CondT cond; ThenT then; ElseT else_; // actors
|
sl@0
|
121 |
};
|
sl@0
|
122 |
|
sl@0
|
123 |
//////////////////////////////////
|
sl@0
|
124 |
template <typename CondT, typename ThenT>
|
sl@0
|
125 |
struct else_gen {
|
sl@0
|
126 |
|
sl@0
|
127 |
else_gen(CondT const& cond_, ThenT const& then_)
|
sl@0
|
128 |
: cond(cond_), then(then_) {}
|
sl@0
|
129 |
|
sl@0
|
130 |
template <typename ElseT>
|
sl@0
|
131 |
actor<if_then_else_composite<CondT, ThenT,
|
sl@0
|
132 |
typename as_actor<ElseT>::type> >
|
sl@0
|
133 |
operator[](ElseT const& else_)
|
sl@0
|
134 |
{
|
sl@0
|
135 |
typedef if_then_else_composite<CondT, ThenT,
|
sl@0
|
136 |
typename as_actor<ElseT>::type>
|
sl@0
|
137 |
result;
|
sl@0
|
138 |
|
sl@0
|
139 |
return result(cond, then, as_actor<ElseT>::convert(else_));
|
sl@0
|
140 |
}
|
sl@0
|
141 |
|
sl@0
|
142 |
CondT cond; ThenT then;
|
sl@0
|
143 |
};
|
sl@0
|
144 |
|
sl@0
|
145 |
//////////////////////////////////
|
sl@0
|
146 |
template <typename CondT, typename ThenT>
|
sl@0
|
147 |
struct if_then_composite {
|
sl@0
|
148 |
|
sl@0
|
149 |
typedef if_then_composite<CondT, ThenT> self_t;
|
sl@0
|
150 |
|
sl@0
|
151 |
template <typename TupleT>
|
sl@0
|
152 |
struct result { typedef void type; };
|
sl@0
|
153 |
|
sl@0
|
154 |
if_then_composite(CondT const& cond_, ThenT const& then_)
|
sl@0
|
155 |
: cond(cond_), then(then_), else_(cond, then) {}
|
sl@0
|
156 |
|
sl@0
|
157 |
template <typename TupleT>
|
sl@0
|
158 |
void eval(TupleT const& args) const
|
sl@0
|
159 |
{
|
sl@0
|
160 |
if (cond.eval(args))
|
sl@0
|
161 |
then.eval(args);
|
sl@0
|
162 |
}
|
sl@0
|
163 |
|
sl@0
|
164 |
CondT cond; ThenT then; // actors
|
sl@0
|
165 |
else_gen<CondT, ThenT> else_;
|
sl@0
|
166 |
};
|
sl@0
|
167 |
|
sl@0
|
168 |
//////////////////////////////////
|
sl@0
|
169 |
template <typename CondT>
|
sl@0
|
170 |
struct if_gen {
|
sl@0
|
171 |
|
sl@0
|
172 |
if_gen(CondT const& cond_)
|
sl@0
|
173 |
: cond(cond_) {}
|
sl@0
|
174 |
|
sl@0
|
175 |
template <typename ThenT>
|
sl@0
|
176 |
actor<if_then_composite<
|
sl@0
|
177 |
typename as_actor<CondT>::type,
|
sl@0
|
178 |
typename as_actor<ThenT>::type> >
|
sl@0
|
179 |
operator[](ThenT const& then) const
|
sl@0
|
180 |
{
|
sl@0
|
181 |
typedef if_then_composite<
|
sl@0
|
182 |
typename as_actor<CondT>::type,
|
sl@0
|
183 |
typename as_actor<ThenT>::type>
|
sl@0
|
184 |
result;
|
sl@0
|
185 |
|
sl@0
|
186 |
return result(
|
sl@0
|
187 |
as_actor<CondT>::convert(cond),
|
sl@0
|
188 |
as_actor<ThenT>::convert(then));
|
sl@0
|
189 |
}
|
sl@0
|
190 |
|
sl@0
|
191 |
CondT cond;
|
sl@0
|
192 |
};
|
sl@0
|
193 |
|
sl@0
|
194 |
//////////////////////////////////
|
sl@0
|
195 |
template <typename CondT>
|
sl@0
|
196 |
inline if_gen<CondT>
|
sl@0
|
197 |
if_(CondT const& cond)
|
sl@0
|
198 |
{
|
sl@0
|
199 |
return if_gen<CondT>(cond);
|
sl@0
|
200 |
}
|
sl@0
|
201 |
|
sl@0
|
202 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
203 |
//
|
sl@0
|
204 |
// while_composite
|
sl@0
|
205 |
//
|
sl@0
|
206 |
// This composite has the form:
|
sl@0
|
207 |
//
|
sl@0
|
208 |
// while_(condition)
|
sl@0
|
209 |
// [
|
sl@0
|
210 |
// statement
|
sl@0
|
211 |
// ]
|
sl@0
|
212 |
//
|
sl@0
|
213 |
// While the condition (an actor) evaluates to true, statement
|
sl@0
|
214 |
// (another actor) is executed. The result type of this is void.
|
sl@0
|
215 |
// Note the trailing underscore after while_.
|
sl@0
|
216 |
//
|
sl@0
|
217 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
218 |
template <typename CondT, typename DoT>
|
sl@0
|
219 |
struct while_composite {
|
sl@0
|
220 |
|
sl@0
|
221 |
typedef while_composite<CondT, DoT> self_t;
|
sl@0
|
222 |
|
sl@0
|
223 |
template <typename TupleT>
|
sl@0
|
224 |
struct result { typedef void type; };
|
sl@0
|
225 |
|
sl@0
|
226 |
while_composite(CondT const& cond_, DoT const& do__)
|
sl@0
|
227 |
: cond(cond_), do_(do__) {}
|
sl@0
|
228 |
|
sl@0
|
229 |
template <typename TupleT>
|
sl@0
|
230 |
void eval(TupleT const& args) const
|
sl@0
|
231 |
{
|
sl@0
|
232 |
while (cond.eval(args))
|
sl@0
|
233 |
do_.eval(args);
|
sl@0
|
234 |
}
|
sl@0
|
235 |
|
sl@0
|
236 |
CondT cond;
|
sl@0
|
237 |
DoT do_;
|
sl@0
|
238 |
};
|
sl@0
|
239 |
|
sl@0
|
240 |
//////////////////////////////////
|
sl@0
|
241 |
template <typename CondT>
|
sl@0
|
242 |
struct while_gen {
|
sl@0
|
243 |
|
sl@0
|
244 |
while_gen(CondT const& cond_)
|
sl@0
|
245 |
: cond(cond_) {}
|
sl@0
|
246 |
|
sl@0
|
247 |
template <typename DoT>
|
sl@0
|
248 |
actor<while_composite<
|
sl@0
|
249 |
typename as_actor<CondT>::type,
|
sl@0
|
250 |
typename as_actor<DoT>::type> >
|
sl@0
|
251 |
operator[](DoT const& do_) const
|
sl@0
|
252 |
{
|
sl@0
|
253 |
typedef while_composite<
|
sl@0
|
254 |
typename as_actor<CondT>::type,
|
sl@0
|
255 |
typename as_actor<DoT>::type>
|
sl@0
|
256 |
result;
|
sl@0
|
257 |
|
sl@0
|
258 |
return result(
|
sl@0
|
259 |
as_actor<CondT>::convert(cond),
|
sl@0
|
260 |
as_actor<DoT>::convert(do_));
|
sl@0
|
261 |
}
|
sl@0
|
262 |
|
sl@0
|
263 |
CondT cond;
|
sl@0
|
264 |
};
|
sl@0
|
265 |
|
sl@0
|
266 |
//////////////////////////////////
|
sl@0
|
267 |
template <typename CondT>
|
sl@0
|
268 |
inline while_gen<CondT>
|
sl@0
|
269 |
while_(CondT const& cond)
|
sl@0
|
270 |
{
|
sl@0
|
271 |
return while_gen<CondT>(cond);
|
sl@0
|
272 |
}
|
sl@0
|
273 |
|
sl@0
|
274 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
275 |
//
|
sl@0
|
276 |
// do_composite
|
sl@0
|
277 |
//
|
sl@0
|
278 |
// This composite has the form:
|
sl@0
|
279 |
//
|
sl@0
|
280 |
// do_
|
sl@0
|
281 |
// [
|
sl@0
|
282 |
// statement
|
sl@0
|
283 |
// ]
|
sl@0
|
284 |
// .while_(condition)
|
sl@0
|
285 |
//
|
sl@0
|
286 |
// While the condition (an actor) evaluates to true, statement
|
sl@0
|
287 |
// (another actor) is executed. The statement is executed at least
|
sl@0
|
288 |
// once. The result type of this is void. Note the trailing
|
sl@0
|
289 |
// underscore after do_ and the the leading dot and the trailing
|
sl@0
|
290 |
// underscore before and after .while_.
|
sl@0
|
291 |
//
|
sl@0
|
292 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
293 |
template <typename DoT, typename CondT>
|
sl@0
|
294 |
struct do_composite {
|
sl@0
|
295 |
|
sl@0
|
296 |
typedef do_composite<DoT, CondT> self_t;
|
sl@0
|
297 |
|
sl@0
|
298 |
template <typename TupleT>
|
sl@0
|
299 |
struct result { typedef void type; };
|
sl@0
|
300 |
|
sl@0
|
301 |
do_composite(DoT const& do__, CondT const& cond_)
|
sl@0
|
302 |
: do_(do__), cond(cond_) {}
|
sl@0
|
303 |
|
sl@0
|
304 |
template <typename TupleT>
|
sl@0
|
305 |
void eval(TupleT const& args) const
|
sl@0
|
306 |
{
|
sl@0
|
307 |
do
|
sl@0
|
308 |
do_.eval(args);
|
sl@0
|
309 |
while (cond.eval(args));
|
sl@0
|
310 |
}
|
sl@0
|
311 |
|
sl@0
|
312 |
DoT do_;
|
sl@0
|
313 |
CondT cond;
|
sl@0
|
314 |
};
|
sl@0
|
315 |
|
sl@0
|
316 |
////////////////////////////////////
|
sl@0
|
317 |
template <typename DoT>
|
sl@0
|
318 |
struct do_gen2 {
|
sl@0
|
319 |
|
sl@0
|
320 |
do_gen2(DoT const& do__)
|
sl@0
|
321 |
: do_(do__) {}
|
sl@0
|
322 |
|
sl@0
|
323 |
template <typename CondT>
|
sl@0
|
324 |
actor<do_composite<
|
sl@0
|
325 |
typename as_actor<DoT>::type,
|
sl@0
|
326 |
typename as_actor<CondT>::type> >
|
sl@0
|
327 |
while_(CondT const& cond) const
|
sl@0
|
328 |
{
|
sl@0
|
329 |
typedef do_composite<
|
sl@0
|
330 |
typename as_actor<DoT>::type,
|
sl@0
|
331 |
typename as_actor<CondT>::type>
|
sl@0
|
332 |
result;
|
sl@0
|
333 |
|
sl@0
|
334 |
return result(
|
sl@0
|
335 |
as_actor<DoT>::convert(do_),
|
sl@0
|
336 |
as_actor<CondT>::convert(cond));
|
sl@0
|
337 |
}
|
sl@0
|
338 |
|
sl@0
|
339 |
DoT do_;
|
sl@0
|
340 |
};
|
sl@0
|
341 |
|
sl@0
|
342 |
////////////////////////////////////
|
sl@0
|
343 |
struct do_gen {
|
sl@0
|
344 |
|
sl@0
|
345 |
template <typename DoT>
|
sl@0
|
346 |
do_gen2<DoT>
|
sl@0
|
347 |
operator[](DoT const& do_) const
|
sl@0
|
348 |
{
|
sl@0
|
349 |
return do_gen2<DoT>(do_);
|
sl@0
|
350 |
}
|
sl@0
|
351 |
};
|
sl@0
|
352 |
|
sl@0
|
353 |
do_gen const do_ = do_gen();
|
sl@0
|
354 |
|
sl@0
|
355 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
356 |
//
|
sl@0
|
357 |
// for_composite
|
sl@0
|
358 |
//
|
sl@0
|
359 |
// This statement has the form:
|
sl@0
|
360 |
//
|
sl@0
|
361 |
// for_(init, condition, step)
|
sl@0
|
362 |
// [
|
sl@0
|
363 |
// statement
|
sl@0
|
364 |
// ]
|
sl@0
|
365 |
//
|
sl@0
|
366 |
// Where init, condition, step and statement are all actors. init
|
sl@0
|
367 |
// is executed once before entering the for-loop. The for-loop
|
sl@0
|
368 |
// exits once condition evaluates to false. At each loop iteration,
|
sl@0
|
369 |
// step and statement is called. The result of this statement is
|
sl@0
|
370 |
// void. Note the trailing underscore after for_.
|
sl@0
|
371 |
//
|
sl@0
|
372 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
373 |
template <typename InitT, typename CondT, typename StepT, typename DoT>
|
sl@0
|
374 |
struct for_composite {
|
sl@0
|
375 |
|
sl@0
|
376 |
typedef composite<InitT, CondT, StepT, DoT> self_t;
|
sl@0
|
377 |
|
sl@0
|
378 |
template <typename TupleT>
|
sl@0
|
379 |
struct result { typedef void type; };
|
sl@0
|
380 |
|
sl@0
|
381 |
for_composite(
|
sl@0
|
382 |
InitT const& init_,
|
sl@0
|
383 |
CondT const& cond_,
|
sl@0
|
384 |
StepT const& step_,
|
sl@0
|
385 |
DoT const& do__)
|
sl@0
|
386 |
: init(init_), cond(cond_), step(step_), do_(do__) {}
|
sl@0
|
387 |
|
sl@0
|
388 |
template <typename TupleT>
|
sl@0
|
389 |
void
|
sl@0
|
390 |
eval(TupleT const& args) const
|
sl@0
|
391 |
{
|
sl@0
|
392 |
for (init.eval(args); cond.eval(args); step.eval(args))
|
sl@0
|
393 |
do_.eval(args);
|
sl@0
|
394 |
}
|
sl@0
|
395 |
|
sl@0
|
396 |
InitT init; CondT cond; StepT step; DoT do_; // actors
|
sl@0
|
397 |
};
|
sl@0
|
398 |
|
sl@0
|
399 |
//////////////////////////////////
|
sl@0
|
400 |
template <typename InitT, typename CondT, typename StepT>
|
sl@0
|
401 |
struct for_gen {
|
sl@0
|
402 |
|
sl@0
|
403 |
for_gen(
|
sl@0
|
404 |
InitT const& init_,
|
sl@0
|
405 |
CondT const& cond_,
|
sl@0
|
406 |
StepT const& step_)
|
sl@0
|
407 |
: init(init_), cond(cond_), step(step_) {}
|
sl@0
|
408 |
|
sl@0
|
409 |
template <typename DoT>
|
sl@0
|
410 |
actor<for_composite<
|
sl@0
|
411 |
typename as_actor<InitT>::type,
|
sl@0
|
412 |
typename as_actor<CondT>::type,
|
sl@0
|
413 |
typename as_actor<StepT>::type,
|
sl@0
|
414 |
typename as_actor<DoT>::type> >
|
sl@0
|
415 |
operator[](DoT const& do_) const
|
sl@0
|
416 |
{
|
sl@0
|
417 |
typedef for_composite<
|
sl@0
|
418 |
typename as_actor<InitT>::type,
|
sl@0
|
419 |
typename as_actor<CondT>::type,
|
sl@0
|
420 |
typename as_actor<StepT>::type,
|
sl@0
|
421 |
typename as_actor<DoT>::type>
|
sl@0
|
422 |
result;
|
sl@0
|
423 |
|
sl@0
|
424 |
return result(
|
sl@0
|
425 |
as_actor<InitT>::convert(init),
|
sl@0
|
426 |
as_actor<CondT>::convert(cond),
|
sl@0
|
427 |
as_actor<StepT>::convert(step),
|
sl@0
|
428 |
as_actor<DoT>::convert(do_));
|
sl@0
|
429 |
}
|
sl@0
|
430 |
|
sl@0
|
431 |
InitT init; CondT cond; StepT step;
|
sl@0
|
432 |
};
|
sl@0
|
433 |
|
sl@0
|
434 |
//////////////////////////////////
|
sl@0
|
435 |
template <typename InitT, typename CondT, typename StepT>
|
sl@0
|
436 |
inline for_gen<InitT, CondT, StepT>
|
sl@0
|
437 |
for_(InitT const& init, CondT const& cond, StepT const& step)
|
sl@0
|
438 |
{
|
sl@0
|
439 |
return for_gen<InitT, CondT, StepT>(init, cond, step);
|
sl@0
|
440 |
}
|
sl@0
|
441 |
|
sl@0
|
442 |
} // namespace phoenix
|
sl@0
|
443 |
|
sl@0
|
444 |
#endif
|