Transforming a Boost C++ Phoenix Expression Tree

那年仲夏 提交于 2019-12-05 09:38:09

This is how you do it with straight 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;
}

Phoenix has layered a bunch of stuff on top of Proto. I don't know the semantics of pheonix::eval or why what you tried didn't work. Perhaps someone knowledgeable of Phoenix will chime in.

==== EDIT ====

I figured out the problem with the Phoenix example. It's not recursing for the non-plus case. Your code should be as follows:

#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!
}

Whether you consider that simpler or more complicated than the straight Proto solution is for you to decide.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!