Re: [proto] Manipulating an expression tree

2011-04-06 Thread Bart Janssens
On Wed, Apr 6, 2011 at 10:29 PM, Karsten Ahnert
karsten.ahn...@ambrosys.de wrote:
 Is there a direct way to transform an expression tree into another one?
 For example, is it possible that every proto::plus  node is
 transformed to it left child? I tried to solve this problem via protos
 build-in transforms without success. It seems that they are suited for
 evaluation of an existing tree, but I might be wrong.

Hi Karsten,

I'm pretty sure they can do both. For your example, I think something
along the lines of this might work (untested):

struct LeftPlus :
  boost::proto::or_
  
boost::proto::terminalboost::proto::_,
boost::proto::when

  boost::proto::plusboost::proto::_, boost::proto::_,
  LeftPlus(boost::proto::_left)
,
boost::proto::nary_expr boost::proto::_, boost::proto::varargLeftPlus 
  
{};

This should recurse through expressions and replace sequences of
pluses with the left-most terminal. You may need some other criteria
to end the recursion depending on your use case.

Disclaimer: I'm relatively new to proto myself, so the experts might
have better solutions!

Cheers,

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


Re: [proto] Manipulating an expression tree

2011-04-06 Thread Karsten Ahnert
Great! It works perfectly, alltough I don't understand the code
completely yet.

Another question is: can a node have a state. In my algorithm it would
be nice, if every proto::multiplies  node stores some intermediate
values which are used during later evaluations of the tree.

Thanks,

Karsten




On 04/06/2011 10:53 PM, Bart Janssens wrote:
 On Wed, Apr 6, 2011 at 10:29 PM, Karsten Ahnert
 karsten.ahn...@ambrosys.de wrote:
 Is there a direct way to transform an expression tree into another one?
 For example, is it possible that every proto::plus  node is
 transformed to it left child? I tried to solve this problem via protos
 build-in transforms without success. It seems that they are suited for
 evaluation of an existing tree, but I might be wrong.
 
 Hi Karsten,
 
 I'm pretty sure they can do both. For your example, I think something
 along the lines of this might work (untested):
 
 struct LeftPlus :
   boost::proto::or_
   
 boost::proto::terminalboost::proto::_,
 boost::proto::when
 
   boost::proto::plusboost::proto::_, boost::proto::_,
   LeftPlus(boost::proto::_left)
 ,
 boost::proto::nary_expr boost::proto::_, boost::proto::varargLeftPlus 
   
 {};
 
 This should recurse through expressions and replace sequences of
 pluses with the left-most terminal. You may need some other criteria
 to end the recursion depending on your use case.
 
 Disclaimer: I'm relatively new to proto myself, so the experts might
 have better solutions!
 
 Cheers,
 


-- 
Dr. Karsten Ahnert
Ambrosys GmbH - Gesellschaft für Management komplexer Systeme
Geschwister-Scholl-Str. 63a
D-14471 Potsdam

Tel: +4917682001688
Fax: +493319791300

Ambrosys GmbH - Gesellschaft für Management komplexer Systems
Gesellschaft mit beschränkter Haftung
Sitz der Gesellschaft: Geschwister-Scholl-Str. 63a, 14471 Potsdam
Registergericht: Amtsgericht Potsdam, HRB 21228 P
Geschäftsführer: Dr. Karsten Ahnert, Dr. Markus Abel
___
proto mailing list
proto@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/proto


Re: [proto] Manipulating an expression tree

2011-04-06 Thread Eric Niebler
(Please don't top-post. Rearranging...)

On 4/7/2011 5:45 AM, Karsten Ahnert wrote:
 On 04/06/2011 10:53 PM, Bart Janssens wrote:
 On Wed, Apr 6, 2011 at 10:29 PM, Karsten Ahnert
 karsten.ahn...@ambrosys.de wrote:
 Is there a direct way to transform an expression tree into another one?
 For example, is it possible that every proto::plus  node is
 transformed to it left child? I tried to solve this problem via protos
 build-in transforms without success. It seems that they are suited for
 evaluation of an existing tree, but I might be wrong.

 Hi Karsten,

 I'm pretty sure they can do both. For your example, I think something
 along the lines of this might work (untested):

 struct LeftPlus :
   boost::proto::or_
   
 boost::proto::terminalboost::proto::_,
 boost::proto::when
 
   boost::proto::plusboost::proto::_, boost::proto::_,
   LeftPlus(boost::proto::_left)
 ,
 boost::proto::nary_expr boost::proto::_, boost::proto::varargLeftPlus 
 
   
 {};

 This should recurse through expressions and replace sequences of
 pluses with the left-most terminal. You may need some other criteria
 to end the recursion depending on your use case.

 Disclaimer: I'm relatively new to proto myself, so the experts might
 have better solutions!

 Cheers,

 Great! It works perfectly, alltough I don't understand the code
 completely yet.

It takes time, but it'll be worth it. You can't do much with Proto with
grokking grammars and transforms.

 Another question is: can a node have a state. In my algorithm it would
 be nice, if every proto::multiplies  node stores some intermediate
 values which are used during later evaluations of the tree.

No. Intermediate expression nodes carry no run-time state in their
nodes. They only carry compile-time information in the form of the tag.
That's how a plus node is distinguished from a minus node, for instance.

If you need to compute intermediate values, you can use a transform to
build a parallel structure.

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



signature.asc
Description: OpenPGP digital signature
___
proto mailing list
proto@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/proto