Convert Boost C ++ Phoenix Expression Tree

In Boost Phoenix's article “Transforming an Expression Tree” here , a set of custom class specializations invert_actionsare used to invert binary arithmetic expressions. For example, a+bit becomes a-b; a*bbecomes a/b; and vice versa for both.

This implies a recursive traversal of the expression tree — however, this traversal stops when an expression is encountered in which the statement is not explicitly processed. For example, it _1+_2-_3will become _1-_2+_3, but _1+_1&_2will remain as it is (the handler for &) is absent. let(_a = 1, _b = 2) [ _a+_b ]will also remain unchanged.

I thought this was done in accordance with this article, but looking at the tests listed at the end, I see what if_(_1 * _4)[_2 - _3]is expected; with the code provided ( here ), I find that it is not.

How then can I define a general Boost Phoenix expression tree that applies to the whole set of explicitly listed (n-ary) operators; leaving the rest unchanged?

Some code may be helpful. I would like the following C ++ 11 (auto) code to output 0, not 2; without explicit processing &or any other operator / operator.

#include <iostream>
#include <boost/phoenix.hpp>
#include <boost/proto/proto.hpp>

using namespace boost;
using namespace proto;
using namespace phoenix;
using namespace arg_names;

struct invrt {
  template <typename Rule> struct when : proto::_ {};
};

template <>
struct invrt::when<rule::plus>
  : proto::call<
    proto::functional::make_expr<proto::tag::minus>(
        evaluator(_left, _context), evaluator(_right, _context)
    )
  >
{};

int main(int argc, char *argv[])
{
  auto f = phoenix::eval( _1+_1&_2 , make_context(make_env(), invrt()) );
  std::cout << f(1,2) << std::endl; // Alas 2 instead of 0
  return 0;
}
+5
source share
1 answer

Here's how you do it with direct proto:

#include <iostream>
#include <boost/phoenix.hpp>
#include <boost/proto/proto.hpp>
namespace proto = boost::proto;
using namespace boost::phoenix;
using namespace arg_names;

struct invrt:
  proto::or_<
    proto::when<
      // Turn plus nodes into minus
      proto::plus<proto::_, proto::_>,
      proto::functional::make_expr<proto::tag::minus>(
        invrt(proto::_left), invrt(proto::_right)
      )
    >,
    proto::otherwise<
      // This recurses on children, transforming them with invrt
      proto::nary_expr<proto::_, proto::vararg<invrt> >
    >
  >
{};

int main(int argc, char *argv[])
{
  auto f = invrt()(_1+_1&_2);
  proto::display_expr(f);
  std::cout << f(1,2) << std::endl;
  return 0;
}

. pheonix::eval , , . , -, , .

==== EDIT ====

Phoenix. , . :

#include <iostream>
#include <boost/phoenix.hpp>
#include <boost/proto/proto.hpp>

using namespace boost;
using namespace proto;
using namespace phoenix;
using namespace arg_names;

struct invrt {
  template <typename Rule>
  struct when :
    // NOTE!!! recursively transform children and reassemble
    nary_expr<_, vararg<proto::when<_, evaluator(_, _context)> > >
  {};
};

template <>
struct invrt::when<rule::plus> :
  proto::call<
    proto::functional::make_expr<proto::tag::minus>(
      evaluator(_left, _context), evaluator(_right, _context)
    )
  >
{};

int main()
{
  auto f = phoenix::eval( _1+_1&_2 , make_context(make_env(), invrt()) );
  display_expr(f);
  std::cout << f(1,2) << std::endl; // Prints 0. Huzzah!
}

, , Proto, .

+2

All Articles