[proto] : Proto transform with state
I am trying to create a transform where I replace occurrences of
proto::terminal<term> with proto::terminal
{};
int main()
{
mpl::plus
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
>. 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
On Wed, Nov 17, 2010 at 1:46 PM, Eric Niebler
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.
Forgive me if I'm telling you something you already know, but there's probably already an abstraction for this out there; it sounds like classic attribute-grammar stuff. -- Dave Abrahams BoostPro Computing http://www.boostpro.com
On 11/18/2010 12:31 PM, Dave Abrahams wrote:
On Wed, Nov 17, 2010 at 1:46 PM, Eric Niebler
wrote: 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.
Forgive me if I'm telling you something you already know, but there's probably already an abstraction for this out there; it sounds like classic attribute-grammar stuff.
If you have more insights to share, I'm all ears. I don't know enough about attribute-grammar to see the applicability. -- Eric Niebler BoostPro Computing http://www.boostpro.com
participants (3)
-
Dave Abrahams
-
Eric Niebler
-
Manjunath Kudlur