Re: [proto] Proto Transform Questions

2011-01-18 Thread Nate Knight
Eric Niebler eric@... writes:
 
 This is a fun little problem. The answer is very simple, but requires
 some knowledge of proto's pass_through transform, possessed by
 proto::nary_expr (and friends):
 

Eric,

Thanks for the pointers.  I had tried this with nary_expr, but had some trouble 
distributing the subscript properly.  Also, its nice to know about 
_make_subscript and friends.



___
proto mailing list
proto@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/proto


[proto] Proto Transform Questions

2011-01-15 Thread Nate Knight
I've pasted some code below where I am trying to transform expressions of the 
form

(a op b op c)[i] 

to 

(a[i] op b[i] op c[i])

I managed to get this to work for the simple case

(a+b)[i]

but I'm curious about how to generalize this to include other operators 
(without explicitly handling them all).  Also, as written the transform doesn't 
recurse properly, and I'm having some trouble seeing how to correct this.

As I'm really just trying to get my mind around how proto works, any comments 
would be appreciated.

Thanks
Nate Knight



test2.cpp
Description: test2.cpp


___
proto mailing list
proto@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/proto


Re: [proto] Proto Transform Questions

2011-01-15 Thread Eric Niebler
On 1/15/2011 6:41 AM, Nate Knight wrote:
 On Jan 14, 2011, at 12:29 PM, Nate Knight wrote:
 
 I've pasted some code below where I am trying to transform expressions of 
 the form

 (a op b op c)[i] 

 to 

 (a[i] op b[i] op c[i])

 I managed to get this to work for the simple case

 (a+b)[i]

 but I'm curious about how to generalize this to include other operators 
 (without explicitly handling them all).  Also, as written the transform 
 doesn't recurse properly, and I'm having some trouble seeing how to correct 
 this.


This is a fun little problem. The answer is very simple, but requires
some knowledge of proto's pass_through transform, possessed by
proto::nary_expr (and friends):

// Take any expression and turn each node
// into a subscript expression, using the
// state as the RHS.
struct Distribute
  : or_
whenterminal_, _make_subscript(_, _state)
  , nary_expr_, varargDistribute 

{};

struct Vectorize
  : or_
terminal_
  , whensubscript_, _, Distribute(_left, _right)
  , nary_expr_, varargVectorize 

{};

int main()
{
terminalchar const *::type a = {a};
terminalchar const *::type b = {b};
terminalchar const *::type c = {c};
terminalint::type i = {42};

display_expr( (a+b+c)[i] );

display_expr( Vectorize()((a+b+c)[i]) );
}

HTH,

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com
___
proto mailing list
proto@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/proto