Boost logo

Boost Users :

Subject: [Boost-users] Proto Help
From: David Greene (greened_at_[hidden])
Date: 2009-12-29 23:09:19


I am resurrecting some ancient code written back before Proto was
an official Boost library. Things have changed quite a bit since then
so I need to port the code.

The code was never complete in the first place but I think much of it
still applies.

In any case, my goal is to take a Proto expression tree and transform
it into another kind of tree using custom classes. Essentially it's an
IR translation problem.

Let's take an add node for example. Here's what I have so far:

      struct Base { ... };
      struct Add : Base { ... }; // Custom expression nodes.

      // Transform a two-operand node.
      template<typename NodeType, typename Dummy = boost::proto::callable>
      struct ConstructBinary
            : boost::proto::callable {

         typedef boost:shared_ptr<NodeType> result_type;

         result_type operator()(boost::shared_ptr<Base> left,
                                         boost::shared_ptr<Base> right) {
           return result_type(new NodeType(left, right));
         }
      };

    struct ConstructExpressionGrammar;

    struct AddRule : boost::proto::plus<ConstructExpressionGrammar,
                                                       ConstructExpressionGrammar>
    {};

    // Want to use switch because of the large expression space.
    struct ConstructExpressionGrammarCases {
      // The primary template matches nothing:
      template<typename Tag>
      struct case_ : proto::not_<_> {};
    };

    // Match and construct an Add tree.
    template<>
    struct ConstructExpressionGrammarCases::case_<proto::tag::plus>
      : boost::proto::when<AddRule,
                                     // ??
                                     ConstructBinary<Add>(
                                         ConstructExpressionGrammmar(
                                             boost::proto::_left),
                                         ConstructExpressionGrammmar(
                                             boost::proto::_right))> {};

    struct ConstructExpressionGrammar :
        boost::proto::switch_<ConstructExpressionGrammarCases> {};

That's the basic outline. I don't think this is correct, however.

In the grammar rule when<>, I'm not sure I can make a nested function
type like that. I'm trying to tell the grammar to match the children and
transform them, passing them to the ConstructBinary transformation to
build the Add subtree.

Any help is greatly appreciated. Thanks!

                                       -Dave


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net