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.