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