1 /*=============================================================================
2 Phoenix V1.2.1
3 Copyright (c) 2001-2003 Joel de Guzman
4
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 ==============================================================================*/
9 #include <iostream>
10 #include <vector>
11 #include <algorithm>
12 #include <boost/detail/lightweight_test.hpp>
13
14 #define PHOENIX_LIMIT 15
15 #include <boost/spirit/include/phoenix1_primitives.hpp>
16 #include <boost/spirit/include/phoenix1_functions.hpp>
17 #include <boost/spirit/include/phoenix1_operators.hpp>
18 #include <boost/spirit/include/phoenix1_special_ops.hpp>
19 #include <boost/spirit/include/phoenix1_statements.hpp>
20
21 using namespace phoenix;
22 using namespace std;
23
24 ///////////////////////////////////////////////////////////////////////////////
25 struct poly_print_ {
26
27 template <typename ArgT>
28 struct result { typedef void type; };
29
30 template <typename ArgT>
operator ()poly_print_31 void operator()(ArgT v) const
32 { cout << v; }
33 };
34
35 function<poly_print_> poly_print;
36
37 ///////////////////////////////////////////////////////////////////////////////
38 int
main()39 main()
40 {
41 char c1 = '1';
42 int i1 = 1;
43 double d2_5 = 2.5;
44 string hello = "hello";
45 const char* world = " world";
46
47 ///////////////////////////////////////////////////////////////////////////////
48 //
49 // Block statements
50 //
51 ///////////////////////////////////////////////////////////////////////////////
52 (
53 poly_print(arg1),
54 poly_print(arg2),
55 poly_print(arg3),
56 poly_print(arg4),
57 poly_print(arg5),
58 poly_print("\n\n")
59 )
60 (hello, c1, world, i1, d2_5);
61
62 ///////////////////////////////////////////////////////////////////////////////
63 //
64 // If-else, while, do-while, for tatements
65 //
66 ///////////////////////////////////////////////////////////////////////////////
67
68 vector<int> v;
69 v.push_back(1);
70 v.push_back(2);
71 v.push_back(3);
72 v.push_back(4);
73 v.push_back(5);
74 v.push_back(6);
75 v.push_back(7);
76 v.push_back(8);
77 v.push_back(9);
78 v.push_back(10);
79
80 cout << dec;
81
82 //////////////////////////////////
83 for_each(v.begin(), v.end(),
84 if_(arg1 > 3 && arg1 <= 8)
85 [
86 cout << arg1 << ", "
87 ]
88 );
89
90 cout << endl;
91
92 //////////////////////////////////
93 for_each(v.begin(), v.end(),
94 if_(arg1 > 5)
95 [
96 cout << arg1 << " > 5\n"
97 ]
98 .else_
99 [
100 if_(arg1 == 5)
101 [
102 cout << arg1 << " == 5\n"
103 ]
104 .else_
105 [
106 cout << arg1 << " < 5\n"
107 ]
108 ]
109 );
110
111 cout << endl;
112
113 vector<int> t = v;
114 //////////////////////////////////
115 for_each(v.begin(), v.end(),
116 (
117 while_(arg1--)
118 [
119 cout << arg1 << ", "
120 ],
121 cout << val("\n")
122 )
123 );
124
125 v = t;
126 cout << endl;
127
128 //////////////////////////////////
129 for_each(v.begin(), v.end(),
130 (
131 do_
132 [
133 cout << arg1 << ", "
134 ]
135 .while_(arg1--),
136 cout << val("\n")
137 )
138 );
139
140 v = t;
141 cout << endl;
142
143 //////////////////////////////////
144 int iii;
145 for_each(v.begin(), v.end(),
146 (
147 for_(var(iii) = 0, var(iii) < arg1, ++var(iii))
148 [
149 cout << arg1 << ", "
150 ],
151 cout << val("\n")
152 )
153 );
154
155 v = t;
156 cout << endl;
157
158 ///////////////////////////////////////////////////////////////////////////////
159 //
160 // End asserts
161 //
162 ///////////////////////////////////////////////////////////////////////////////
163
164 return boost::report_errors();
165 }
166