Boost logo

Proto :

Subject: Re: [proto] : Proto transform with state
From: Eric Niebler (eric_at_[hidden])
Date: 2010-11-17 13:46:27


On 11/17/2010 1:47 AM, Manjunath Kudlur wrote:
> I am trying to create a transform where I replace occurrences of
> proto::terminal<term> with proto::terminal<newterm<mpl::int_<N> > >. I
> want N to increment for every proto::terminal<term> encountered. So
> far I have the following code :

<snip>

> I pass in mpl::int_<0>() as the initial state and call mpl::next when
> I match proto::terminal<term>. As the output would indicate, this is
> not working. Every time I think I understand proto transforms,
> something basic like this stumps me. I will be grateful for any hints
> on how to accomplish this.

Don't feel bad. This is EXTREMELY hard to do in Proto. :-( Most
algorithms only return one piece of information at each step. This task
requires returning two: the transformed expression and the new state.
Worse, after transforming a left child, the new state needs to be used
as input while transforming the right. Like I said, EXTREMELY hard.

Since your algorithm needs to return two pieces of information, it needs
to return a std::pair containing the new expression and the updated
state variable. And when transforming a non-terminal, you need to use
the fold algorithm to propagate the state from one invocation to the
next (again, bundling and unbundling into a pair as necessary). Fold can
build a fusion::vector of transformed children, which then needs to be
unpacked into an expression. (This step wouldn't be necessary if Proto
expressions were extensible Fusion sequences. <sigh>)

See the attached code. I wish I had a better answer. It sure would be
nice to generalize this for other times when new state needs to bubble
up and back down.

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com



Proto list run by eric at boostpro.com