File: transforming.qbk

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (153 lines) | stat: -rw-r--r-- 5,868 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
[/==============================================================================
    Copyright (C) 2001-2010 Joel de Guzman
    Copyright (C) 2001-2005 Dan Marsden
    Copyright (C) 2001-2010 Thomas Heller

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
===============================================================================/]

[section Transforming the Expression Tree]

This example will show how to write __phoenix_actions__ that transform the
Phoenix AST.

[:
"/Lisp macros transform the program structure itself, with the full language
available to express such transformations./"

[@http://en.wikipedia.org/wiki/Lisp_macro#Lisp_macros Wikipedia]
]

What we want to do is to invert some arithmetic operators, i.e. plus will be
transformed to minus, minus to plus, multiplication to division and division to
multiplication.

Let's start with defining our default action:

[def __proto_nary_expr__    [@http://www.boost.org/doc/libs/release/doc/html/boost/proto/nary_expr.html `proto::nary_expr`]]
[def __proto_vararg__       [@http://www.boost.org/doc/libs/release/doc/html/boost/proto/vararg.html `proto::vararg`]]
[def __proto_when__         [@http://www.boost.org/doc/libs/release/doc/html/boost/proto/when.html `proto::when`]]
[def __proto_underscore__   [@http://www.boost.org/doc/libs/release/doc/html/boost/proto/_.html `proto::_`]]
[def __proto_make_expr__    [@http://www.boost.org/doc/libs/release/doc/html/boost/proto/functional/make_expr.html `proto::functional::make_expr`]]

    struct invert_actions
    {
        template <typename Rule>
        struct when
            : __proto_nary_expr__
                  __proto_underscore__
                , __proto_vararg__
                      __proto_when__<__proto_underscore__, phoenix::evaluator(__proto_underscore__, phoenix::_context)
                  >
              >
        {};
    };

Wow, this looks complicated! Granted you need to know a little bit about __proto__
(For a good introduction read through the
 [@http://cpp-next.com/archive/2010/08/expressive-c-introduction/ Expressive C++] series).

By default, we don't want to do anything, well, not exactly nothing, but just
continue transformation into its arguments.

So, it is done by the following:

* For each arguments are passed to evaluator (with the current context, that contains our invert_actions)
* Create new expression using current expression tag, what is done by __proto_underscore__, with the result of evaluated arguments

So, after the basics are set up, we can start by writing the transformations we
want to have on our tree:

    // Transform plus to minus
    template <>
    struct invert_actions::when<phoenix::rule::plus>
        : __proto_call__<
            __proto_make_expr__<proto::tag::minus>(
                phoenix::evaluator(proto::_left, phoenix::_context)
              , phoenix::evaluator(proto::_right, phoenix::_context)
            )
        >
    {};

What is done is the following:

* The left expression is passed to evaluator (with the current context, that contains our invert_actions)
* The right expression is passed to evaluator (with the current context, that contains our invert_actions)
* The result of these two __proto_transforms__ are passed to __proto_make_expr__ which returns the freshly created expression

After you know what is going on, maybe the rest doesn't look so scary anymore:

    // Transform minus to plus
    template <>
    struct invert_actions::when<phoenix::rule::minus>
        : __proto_call__<
            __proto_make_expr__<proto::tag::plus>(
                phoenix::evaluator(proto::_left, phoenix::_context)
              , phoenix::evaluator(proto::_right, phoenix::_context)
            )
        >
    {};

    // Transform multiplies to divides
    template <>
    struct invert_actions::when<phoenix::rule::multiplies>
        : __proto_call__<
            __proto_make_expr__<proto::tag::divides>(
                phoenix::evaluator(proto::_left, phoenix::_context)
              , phoenix::evaluator(proto::_right, phoenix::_context)
            )
        >
    {};

    // Transform divides to multiplies
    template <>
    struct invert_actions::when<phoenix::rule::divides>
        : __proto_call__<
            __proto_make_expr__<proto::tag::multiplies>(
                phoenix::evaluator(proto::_left, phoenix::_context)
              , phoenix::evaluator(proto::_right, phoenix::_context)
            )
        >
    {};

That's it! Now that we have our actions defined, we want to evaluate some of our expressions with them:

    template <typename Expr>
    // Calculate the result type: our transformed AST
    typename boost::result_of<
        phoenix::evaluator(
            Expr const&
          , phoenix::result_of::context<int, invert_actions>::type
        )
    >::type
    invert(Expr const & expr)
    {
        return 
            // Evaluate it with our actions
            phoenix::eval(
                expr
              , phoenix::context(
                    int()
                  , invert_actions()
                )
            );
    }

Run some tests to see if it is working:

    invert(_1);                    // --> _1
    invert(_1 + _2);               // --> _1 - _2
    invert(_1 + _2 - _3);          // --> _1 - _2 + _3
    invert(_1 * _2);               // --> _1 / _2
    invert(_1 * _2 / _3);          // --> _1 / _2 * _3
    invert(_1 * _2 + _3);          // --> _1 / _2 - _3
    invert(_1 * _2 - _3);          // --> _1 / _2 + _2
    invert(if_(_1 * _4)[_2 - _3]); // --> if_(_1 / _4)[_2 + _3]
    _1 * invert(_2 - _3));         // --> _1 * _2 + _3

__note__ The complete example can be found here: [@../../example/invert.cpp example/invert.cpp]

/Pretty simple .../

[endsect]