main.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2012.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #define BOOST_MPL_LIMIT_STRING_SIZE 64
  6. #define BOOST_METAPARSE_LIMIT_STRING_SIZE BOOST_MPL_LIMIT_STRING_SIZE
  7. #include <boost/metaparse/grammar.hpp>
  8. #include <boost/metaparse/string.hpp>
  9. #include <boost/metaparse/build_parser.hpp>
  10. #include <boost/metaparse/token.hpp>
  11. #include <boost/metaparse/entire_input.hpp>
  12. #include <boost/metaparse/int_.hpp>
  13. #include <boost/metaparse/transform.hpp>
  14. #include <boost/mpl/apply_wrap.hpp>
  15. #include <boost/mpl/front.hpp>
  16. #include <boost/mpl/back.hpp>
  17. #include <boost/mpl/if.hpp>
  18. #include <boost/mpl/bool.hpp>
  19. #include <boost/mpl/fold.hpp>
  20. using boost::mpl::apply_wrap1;
  21. using boost::mpl::front;
  22. using boost::mpl::back;
  23. using boost::mpl::if_;
  24. using boost::mpl::bool_;
  25. using boost::metaparse::build_parser;
  26. using boost::metaparse::token;
  27. using boost::metaparse::entire_input;
  28. using boost::metaparse::int_;
  29. using boost::metaparse::grammar;
  30. using boost::metaparse::transform;
  31. #if BOOST_METAPARSE_STD < 2011
  32. int main()
  33. {
  34. std::cout << "Please use a compiler that supports constexpr" << std::endl;
  35. }
  36. #else
  37. #ifdef _STR
  38. # error _STR already defined
  39. #endif
  40. #define _STR BOOST_METAPARSE_STRING
  41. template <class T, char C>
  42. struct is_c : bool_<T::type::value == C> {};
  43. struct build_plus_impl
  44. {
  45. template <class A, class B>
  46. class _plus
  47. {
  48. public:
  49. typedef _plus type;
  50. template <class T>
  51. T operator()(T t) const
  52. {
  53. return _left(t) + _right(t);
  54. }
  55. private:
  56. typename A::type _left;
  57. typename B::type _right;
  58. };
  59. template <class A, class B>
  60. class _minus
  61. {
  62. public:
  63. typedef _minus type;
  64. template <class T>
  65. T operator()(T t) const
  66. {
  67. return _left(t) - _right(t);
  68. }
  69. private:
  70. typename A::type _left;
  71. typename B::type _right;
  72. };
  73. template <class State, class C>
  74. struct apply :
  75. if_<
  76. typename is_c<front<C>, '+'>::type,
  77. _plus<State, typename back<C>::type>,
  78. _minus<State, typename back<C>::type>
  79. >
  80. {};
  81. };
  82. struct build_plus
  83. {
  84. typedef build_plus type;
  85. template <class Seq>
  86. struct apply :
  87. boost::mpl::fold<
  88. typename back<Seq>::type,
  89. typename front<Seq>::type,
  90. build_plus_impl
  91. >
  92. {};
  93. };
  94. struct build_mult_impl
  95. {
  96. template <class A, class B>
  97. class _mult
  98. {
  99. public:
  100. typedef _mult type;
  101. template <class T>
  102. T operator()(T t) const
  103. {
  104. return _left(t) * _right(t);
  105. }
  106. private:
  107. typename A::type _left;
  108. typename B::type _right;
  109. };
  110. template <class A, class B>
  111. class _div
  112. {
  113. public:
  114. typedef _div type;
  115. template <class T>
  116. T operator()(T t) const
  117. {
  118. return _left(t) / _right(t);
  119. }
  120. private:
  121. typename A::type _left;
  122. typename B::type _right;
  123. };
  124. template <class State, class C>
  125. struct apply :
  126. if_<
  127. typename is_c<front<C>, '*'>::type,
  128. _mult<State, typename back<C>::type>,
  129. _div<State, typename back<C>::type>
  130. >
  131. {};
  132. };
  133. struct build_mult
  134. {
  135. typedef build_mult type;
  136. template <class Seq>
  137. struct apply :
  138. boost::mpl::fold<
  139. typename back<Seq>::type,
  140. typename front<Seq>::type,
  141. build_mult_impl
  142. >
  143. {};
  144. };
  145. struct build_value
  146. {
  147. typedef build_value type;
  148. template <class V>
  149. struct apply
  150. {
  151. typedef apply type;
  152. template <class T>
  153. int operator()(T) const
  154. {
  155. return V::type::value;
  156. }
  157. };
  158. };
  159. struct build_arg
  160. {
  161. typedef build_arg type;
  162. template <class>
  163. struct apply
  164. {
  165. typedef apply type;
  166. template <class T>
  167. T operator()(T t) const
  168. {
  169. return t;
  170. }
  171. };
  172. };
  173. struct keep_front
  174. {
  175. typedef keep_front type;
  176. template <class Seq>
  177. struct apply : front<Seq> {};
  178. };
  179. typedef
  180. grammar<_STR("plus_exp")>
  181. ::import<_STR("int_token"), token<transform<int_, build_value>>>::type
  182. ::rule<_STR("ws ::= (' ' | '\n' | '\r' | '\t')*")>::type
  183. ::rule<_STR("plus_token ::= '+' ws"), keep_front>::type
  184. ::rule<_STR("minus_token ::= '-' ws"), keep_front>::type
  185. ::rule<_STR("mult_token ::= '*' ws"), keep_front>::type
  186. ::rule<_STR("div_token ::= '/' ws"), keep_front>::type
  187. ::rule<_STR("arg_token ::= '_' ws"), keep_front>::type
  188. ::rule<_STR("plus_exp ::= prod_exp ((plus_token | minus_token) prod_exp)*"), build_plus>::type
  189. ::rule<_STR("prod_exp ::= value_exp ((mult_token | div_token) value_exp)*"), build_mult>::type
  190. ::rule<_STR("value_exp ::= int_token | arg_exp")>::type
  191. ::rule<_STR("arg_exp ::= arg_token"), build_arg>::type
  192. g;
  193. typedef build_parser<entire_input<g>> function_parser;
  194. #ifdef LAMBDA
  195. #error LAMBDA already defined
  196. #endif
  197. #define LAMBDA(exp) apply_wrap1<function_parser, _STR(#exp)>::type
  198. LAMBDA(13) f1;
  199. LAMBDA(2 + 3) f2;
  200. LAMBDA(2 * 3) f3;
  201. LAMBDA(1+ 2*4-6/2) f4;
  202. LAMBDA(2 * _) f5;
  203. int main()
  204. {
  205. using std::cout;
  206. using std::endl;
  207. cout
  208. << f1(11) << endl
  209. << f2(11) << endl
  210. << f3(11) << endl
  211. << f4(11) << endl
  212. << f5(11) << endl
  213. << f5(1.1) << endl
  214. ;
  215. }
  216. #endif