transforming.qbk 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. [/==============================================================================
  2. Copyright (C) 2001-2010 Joel de Guzman
  3. Copyright (C) 2001-2005 Dan Marsden
  4. Copyright (C) 2001-2010 Thomas Heller
  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. [section Transforming the Expression Tree]
  9. This example will show how to write __phoenix_actions__ that transform the
  10. Phoenix AST.
  11. [:
  12. "/Lisp macros transform the program structure itself, with the full language
  13. available to express such transformations./"
  14. [@http://en.wikipedia.org/wiki/Lisp_macro#Lisp_macros Wikipedia]
  15. ]
  16. What we want to do is to invert some arithmetic operators, i.e. plus will be
  17. transformed to minus, minus to plus, multiplication to division and division to
  18. multiplication.
  19. Let's start with defining our default action:
  20. [def __proto_nary_expr__ [@http://www.boost.org/doc/libs/release/doc/html/boost/proto/nary_expr.html `proto::nary_expr`]]
  21. [def __proto_vararg__ [@http://www.boost.org/doc/libs/release/doc/html/boost/proto/vararg.html `proto::vararg`]]
  22. [def __proto_when__ [@http://www.boost.org/doc/libs/release/doc/html/boost/proto/when.html `proto::when`]]
  23. [def __proto_underscore__ [@http://www.boost.org/doc/libs/release/doc/html/boost/proto/_.html `proto::_`]]
  24. [def __proto_make_expr__ [@http://www.boost.org/doc/libs/release/doc/html/boost/proto/functional/make_expr.html `proto::functional::make_expr`]]
  25. struct invert_actions
  26. {
  27. template <typename Rule>
  28. struct when
  29. : __proto_nary_expr__
  30. __proto_underscore__
  31. , __proto_vararg__
  32. __proto_when__<__proto_underscore__, phoenix::evaluator(__proto_underscore__, phoenix::_context)
  33. >
  34. >
  35. {};
  36. };
  37. Wow, this looks complicated! Granted you need to know a little bit about __proto__
  38. (For a good introduction read through the
  39. [@http://cpp-next.com/archive/2010/08/expressive-c-introduction/ Expressive C++] series).
  40. By default, we don't want to do anything, well, not exactly nothing, but just
  41. continue transformation into its arguments.
  42. So, it is done by the following:
  43. * For each arguments are passed to evaluator (with the current context, that contains our invert_actions)
  44. * Create new expression using current expression tag, what is done by __proto_underscore__, with the result of evaluated arguments
  45. So, after the basics are set up, we can start by writing the transformations we
  46. want to have on our tree:
  47. // Transform plus to minus
  48. template <>
  49. struct invert_actions::when<phoenix::rule::plus>
  50. : __proto_call__<
  51. __proto_make_expr__<proto::tag::minus>(
  52. phoenix::evaluator(proto::_left, phoenix::_context)
  53. , phoenix::evaluator(proto::_right, phoenix::_context)
  54. )
  55. >
  56. {};
  57. What is done is the following:
  58. * The left expression is passed to evaluator (with the current context, that contains our invert_actions)
  59. * The right expression is passed to evaluator (with the current context, that contains our invert_actions)
  60. * The result of these two __proto_transforms__ are passed to __proto_make_expr__ which returns the freshly created expression
  61. After you know what is going on, maybe the rest doesn't look so scary anymore:
  62. // Transform minus to plus
  63. template <>
  64. struct invert_actions::when<phoenix::rule::minus>
  65. : __proto_call__<
  66. __proto_make_expr__<proto::tag::plus>(
  67. phoenix::evaluator(proto::_left, phoenix::_context)
  68. , phoenix::evaluator(proto::_right, phoenix::_context)
  69. )
  70. >
  71. {};
  72. // Transform multiplies to divides
  73. template <>
  74. struct invert_actions::when<phoenix::rule::multiplies>
  75. : __proto_call__<
  76. __proto_make_expr__<proto::tag::divides>(
  77. phoenix::evaluator(proto::_left, phoenix::_context)
  78. , phoenix::evaluator(proto::_right, phoenix::_context)
  79. )
  80. >
  81. {};
  82. // Transform divides to multiplies
  83. template <>
  84. struct invert_actions::when<phoenix::rule::divides>
  85. : __proto_call__<
  86. __proto_make_expr__<proto::tag::multiplies>(
  87. phoenix::evaluator(proto::_left, phoenix::_context)
  88. , phoenix::evaluator(proto::_right, phoenix::_context)
  89. )
  90. >
  91. {};
  92. That's it! Now that we have our actions defined, we want to evaluate some of our expressions with them:
  93. template <typename Expr>
  94. // Calculate the result type: our transformed AST
  95. typename boost::result_of<
  96. phoenix::evaluator(
  97. Expr const&
  98. , phoenix::result_of::context<int, invert_actions>::type
  99. )
  100. >::type
  101. invert(Expr const & expr)
  102. {
  103. return
  104. // Evaluate it with our actions
  105. phoenix::eval(
  106. expr
  107. , phoenix::context(
  108. int()
  109. , invert_actions()
  110. )
  111. );
  112. }
  113. Run some tests to see if it is working:
  114. invert(_1); // --> _1
  115. invert(_1 + _2); // --> _1 - _2
  116. invert(_1 + _2 - _3); // --> _1 - _2 + _3
  117. invert(_1 * _2); // --> _1 / _2
  118. invert(_1 * _2 / _3); // --> _1 / _2 * _3
  119. invert(_1 * _2 + _3); // --> _1 / _2 - _3
  120. invert(_1 * _2 - _3); // --> _1 / _2 + _2
  121. invert(if_(_1 * _4)[_2 - _3]); // --> if_(_1 / _4)[_2 + _3]
  122. _1 * invert(_2 - _3)); // --> _1 * _2 + _3
  123. __note__ The complete example can be found here: [@../../example/invert.cpp example/invert.cpp]
  124. /Pretty simple .../
  125. [endsect]