1 | /////////////////////////////////////////////////////////////////////////////// |
---|
2 | /// \file fold.hpp |
---|
3 | /// A special-purpose proto compiler for merging sequences of binary operations. |
---|
4 | /// It compiles the right operand and passes the result as state while compiling |
---|
5 | /// the left. Or, it might do the left first, if you choose. |
---|
6 | // |
---|
7 | // Copyright 2004 Eric Niebler. Distributed under the Boost |
---|
8 | // Software License, Version 1.0. (See accompanying file |
---|
9 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
10 | |
---|
11 | #ifndef BOOST_PROTO_COMPILER_FOLD_HPP_EAN_04_01_2005 |
---|
12 | #define BOOST_PROTO_COMPILER_FOLD_HPP_EAN_04_01_2005 |
---|
13 | |
---|
14 | #include <boost/xpressive/proto/proto_fwd.hpp> |
---|
15 | |
---|
16 | namespace boost { namespace proto |
---|
17 | { |
---|
18 | |
---|
19 | /////////////////////////////////////////////////////////////////////////////// |
---|
20 | // fold_compiler |
---|
21 | // Compiles the right side and passes the result as state while compiling the left. |
---|
22 | // This is useful for serializing a tree. |
---|
23 | template<typename OpTag, typename DomainTag, bool RightFirst> |
---|
24 | struct fold_compiler |
---|
25 | { |
---|
26 | // sample compiler implementation for sequencing |
---|
27 | template<typename Op, typename State, typename Visitor> |
---|
28 | struct apply |
---|
29 | { |
---|
30 | typedef typename right_type<Op>::type right_type; |
---|
31 | typedef typename left_type<Op>::type left_type; |
---|
32 | |
---|
33 | // compile the right branch |
---|
34 | typedef typename compiler<typename tag_type<right_type>::type, DomainTag>:: |
---|
35 | BOOST_NESTED_TEMPLATE apply |
---|
36 | < |
---|
37 | right_type |
---|
38 | , State |
---|
39 | , Visitor |
---|
40 | >::type right_compiled_type; |
---|
41 | |
---|
42 | // forward the result of the right branch to the left |
---|
43 | typedef typename compiler<typename tag_type<left_type>::type, DomainTag>:: |
---|
44 | BOOST_NESTED_TEMPLATE apply |
---|
45 | < |
---|
46 | left_type |
---|
47 | , right_compiled_type |
---|
48 | , Visitor |
---|
49 | >::type type; |
---|
50 | }; |
---|
51 | |
---|
52 | template<typename Op, typename State, typename Visitor> |
---|
53 | static typename apply<Op, State, Visitor>::type |
---|
54 | call(Op const &op, State const &state, Visitor &visitor) |
---|
55 | { |
---|
56 | return proto::compile( |
---|
57 | proto::left(op) |
---|
58 | , proto::compile(proto::right(op), state, visitor, DomainTag()) |
---|
59 | , visitor |
---|
60 | , DomainTag() |
---|
61 | ); |
---|
62 | } |
---|
63 | }; |
---|
64 | |
---|
65 | /////////////////////////////////////////////////////////////////////////////// |
---|
66 | // fold_compiler |
---|
67 | // Compiles the left side and passes the result as state while compiling the right. |
---|
68 | // This is useful for serializing a tree. |
---|
69 | template<typename OpTag, typename DomainTag> |
---|
70 | struct fold_compiler<OpTag, DomainTag, false> |
---|
71 | { |
---|
72 | // sample compiler implementation for sequencing |
---|
73 | template<typename Op, typename State, typename Visitor> |
---|
74 | struct apply |
---|
75 | { |
---|
76 | typedef typename right_type<Op>::type right_type; |
---|
77 | typedef typename left_type<Op>::type left_type; |
---|
78 | |
---|
79 | // compile the right branch |
---|
80 | typedef typename compiler<typename tag_type<left_type>::type, DomainTag>:: |
---|
81 | BOOST_NESTED_TEMPLATE apply |
---|
82 | < |
---|
83 | left_type |
---|
84 | , State |
---|
85 | , Visitor |
---|
86 | >::type left_compiled_type; |
---|
87 | |
---|
88 | // forward the result of the right branch to the left |
---|
89 | typedef typename compiler<typename tag_type<right_type>::type, DomainTag>:: |
---|
90 | BOOST_NESTED_TEMPLATE apply |
---|
91 | < |
---|
92 | right_type |
---|
93 | , left_compiled_type |
---|
94 | , Visitor |
---|
95 | >::type type; |
---|
96 | }; |
---|
97 | |
---|
98 | template<typename Op, typename State, typename Visitor> |
---|
99 | static typename apply<Op, State, Visitor>::type |
---|
100 | call(Op const &op, State const &state, Visitor &visitor) |
---|
101 | { |
---|
102 | return proto::compile( |
---|
103 | proto::right(op) |
---|
104 | , proto::compile(proto::left(op), state, visitor, DomainTag()) |
---|
105 | , visitor |
---|
106 | , DomainTag() |
---|
107 | ); |
---|
108 | } |
---|
109 | }; |
---|
110 | |
---|
111 | }} |
---|
112 | |
---|
113 | #endif |
---|