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