Boost logo

Boost Users :

Subject: [Boost-users] [lambda][phoenix] print expression tree
From: alfC (alfredo.correa_at_[hidden])
Date: 2010-06-03 02:33:12


As you can see from other posts. It's been quite painful to me to
debug programs with expression templates. In order to debug some code
I implemented some functions to print the expression trees, this would
be a nice addition to Boost.Lambda and also to Boost.Phoenix (although
Boost.Phoenix will probably have it through Boost.Proto which
apparently already can print its expression trees).

Here it is a *minimal* code for printing some Boost.Lambda expression
(involving only [binary +,-,/,*],bind,constants), (let me know if I am
reinventing the wheel)

namespace boost{
namespace lambda{
using namespace boost::tuples;

template<int N> //print placeholder
std::string to_string(const
lambda_functor<boost::lambda::placeholder<N> >&){
        return "_"+boost::lexical_cast<std::string>(N);
}

//template<class T>
//std::string to_string(const T& t){
// return boost::lexical_cast<std::string>(t);
//}

//binary operators
#define BOOST_LAMBDA_TO_STRING_GEN(action_name, symbol_string)\
template<class LExp1, class LExp2> \
std::string to_string(const \
        lambda_functor<lambda_functor_base<arithmetic_action<action_name>,
tuple< \
                LExp1, \
                LExp2 \
> > >& action_name##_expr){ \
        return to_string(get<0>(action_name##_expr.args)) + #symbol_string
+to_string(get<1>(action_name##_expr.args)); \
}

BOOST_LAMBDA_TO_STRING_GEN(plus_action, +);
BOOST_LAMBDA_TO_STRING_GEN(divide_action, /);
BOOST_LAMBDA_TO_STRING_GEN(minus_action, -);
BOOST_LAMBDA_TO_STRING_GEN(multiply_action, *);

//bind
template<class FunctionType, class LExpr>
std::string to_string(const
        lambda_functor<lambda_functor_base<action<2, function_action<2> >,
                tuple<
                        FunctionType, //double (* const)(double),
                        LExpr
>
> >& bind_expr){
        return std::string("bind(")
+boost::units::detail::demangle(typeid(FunctionType).name()) +",
"+to_string(get<1>(bind_expr.args)) + ")";
}
}}

Cheers,
Alfredo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net