statements_tests.cpp 3.8 KB

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