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