PageRenderTime 38ms CodeModel.GetById 19ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/phoenix/doc/html/phoenix/examples/adding_an_expression.html

http://hadesmem.googlecode.com/
HTML | 158 lines | 151 code | 7 blank | 0 comment | 0 complexity | 5beae5f3efa38be73d0dd2fd9ad3902e MD5 | raw file
  1<html>
  2<head>
  3<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4<title>Adding an expression</title>
  5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
  6<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
  7<link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Phoenix 3.0">
  8<link rel="up" href="../examples.html" title="Advanced Examples">
  9<link rel="prev" href="extending_actors.html" title="Extending Actors">
 10<link rel="next" href="transforming_the_expression_tree.html" title="Transforming the Expression Tree">
 11</head>
 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
 13<table cellpadding="2" width="100%"><tr>
 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
 15<td align="center"><a href="../../../../../../index.html">Home</a></td>
 16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
 20</tr></table>
 21<hr>
 22<div class="spirit-nav">
 23<a accesskey="p" href="extending_actors.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="transforming_the_expression_tree.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
 24</div>
 25<div class="section">
 26<div class="titlepage"><div><div><h3 class="title">
 27<a name="phoenix.examples.adding_an_expression"></a><a class="link" href="adding_an_expression.html" title="Adding an expression">Adding an expression</a>
 28</h3></div></div></div>
 29<p>
 30        This is not a toy example. This is actually part of the library. Remember
 31        the <a class="link" href="../modules/statement/while__statement.html" title="while_ Statement"><code class="computeroutput"><span class="keyword">while</span></code></a> lazy statement? Putting together
 32        everything we've learned so far, we eill present it here in its entirety
 33        (verbatim):
 34      </p>
 35<pre class="programlisting"><span class="identifier">BOOST_PHOENIX_DEFINE_EXPRESSION</span><span class="special">(</span>
 36    <span class="special">(</span><span class="identifier">boost</span><span class="special">)(</span><span class="identifier">phoenix</span><span class="special">)(</span><span class="identifier">while_</span><span class="special">)</span>
 37  <span class="special">,</span> <span class="special">(</span><span class="identifier">meta_grammar</span><span class="special">)</span>           <span class="comment">// Cond
 38</span>    <span class="special">(</span><span class="identifier">meta_grammar</span><span class="special">)</span>           <span class="comment">// Do
 39</span><span class="special">)</span>
 40
 41<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">phoenix</span>
 42<span class="special">{</span>
 43    <span class="keyword">struct</span> <span class="identifier">while_eval</span>
 44    <span class="special">{</span>
 45        <span class="keyword">typedef</span> <span class="keyword">void</span> <span class="identifier">result_type</span><span class="special">;</span>
 46
 47        <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Cond</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Do</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Context</span><span class="special">&gt;</span>
 48        <span class="identifier">result_type</span>
 49        <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">Cond</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">cond</span><span class="special">,</span> <span class="identifier">Do</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">do_</span><span class="special">,</span> <span class="identifier">Context</span> <span class="special">&amp;</span> <span class="identifier">ctx</span><span class="special">)</span> <span class="keyword">const</span>
 50        <span class="special">{</span>
 51            <span class="keyword">while</span><span class="special">(</span><span class="identifier">eval</span><span class="special">(</span><span class="identifier">cond</span><span class="special">,</span> <span class="identifier">ctx</span><span class="special">))</span>
 52            <span class="special">{</span>
 53                <span class="identifier">eval</span><span class="special">(</span><span class="identifier">do_</span><span class="special">,</span> <span class="identifier">ctx</span><span class="special">);</span>
 54            <span class="special">}</span>
 55        <span class="special">}</span>
 56    <span class="special">};</span>
 57    
 58    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Dummy</span><span class="special">&gt;</span>
 59    <span class="keyword">struct</span> <span class="identifier">default_actions</span><span class="special">::</span><span class="identifier">when</span><span class="special">&lt;</span><span class="identifier">rule</span><span class="special">::</span><span class="identifier">while_</span><span class="special">,</span> <span class="identifier">Dummy</span><span class="special">&gt;</span>
 60        <span class="special">:</span> <span class="identifier">call</span><span class="special">&lt;</span><span class="identifier">while_eval</span><span class="special">,</span> <span class="identifier">Dummy</span><span class="special">&gt;</span>
 61    <span class="special">{};</span>
 62
 63    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Cond</span><span class="special">&gt;</span>
 64    <span class="keyword">struct</span> <span class="identifier">while_gen</span>
 65    <span class="special">{</span>
 66        <span class="identifier">while_gen</span><span class="special">(</span><span class="identifier">Cond</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">cond</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">cond</span><span class="special">(</span><span class="identifier">cond</span><span class="special">)</span> <span class="special">{}</span>
 67
 68        <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Do</span><span class="special">&gt;</span>
 69        <span class="keyword">typename</span> <span class="identifier">expression</span><span class="special">::</span><span class="identifier">while_</span><span class="special">&lt;</span><span class="identifier">Cond</span><span class="special">,</span> <span class="identifier">Do</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="keyword">const</span>
 70        <span class="keyword">operator</span><span class="special">[](</span><span class="identifier">Do</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">do_</span><span class="special">)</span> <span class="keyword">const</span>
 71        <span class="special">{</span>
 72            <span class="keyword">return</span> <span class="identifier">expression</span><span class="special">::</span><span class="identifier">while_</span><span class="special">&lt;</span><span class="identifier">Cond</span><span class="special">,</span> <span class="identifier">Do</span><span class="special">&gt;::</span><span class="identifier">make</span><span class="special">(</span><span class="identifier">cond</span><span class="special">,</span> <span class="identifier">do_</span><span class="special">);</span>
 73        <span class="special">}</span>
 74
 75        <span class="identifier">Cond</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">cond</span><span class="special">;</span>
 76    <span class="special">};</span>
 77
 78    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Cond</span><span class="special">&gt;</span>
 79    <span class="identifier">while_gen</span><span class="special">&lt;</span><span class="identifier">Cond</span><span class="special">&gt;</span> <span class="keyword">const</span>
 80    <span class="identifier">while_</span><span class="special">(</span><span class="identifier">Cond</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">cond</span><span class="special">)</span>
 81    <span class="special">{</span>
 82        <span class="keyword">return</span> <span class="identifier">while_gen</span><span class="special">&lt;</span><span class="identifier">Cond</span><span class="special">&gt;(</span><span class="identifier">cond</span><span class="special">);</span>
 83    <span class="special">}</span>
 84<span class="special">}}</span>
 85</pre>
 86<p>
 87        <code class="computeroutput"><span class="identifier">while_eval</span></code> is an example
 88        of how to evaluate an expression. It gets called in the <code class="computeroutput"><span class="identifier">rule</span><span class="special">::</span><span class="keyword">while</span></code> action.
 89        <code class="computeroutput"><span class="identifier">while_gen</span></code> and <code class="computeroutput"><span class="identifier">while_</span></code> are the expression template front
 90        ends. Let's break this apart to undestand what's happening. Let's start at
 91        the bottom. It's easier that way.
 92      </p>
 93<p>
 94        When you write:
 95      </p>
 96<pre class="programlisting"><span class="identifier">while_</span><span class="special">(</span><span class="identifier">cond</span><span class="special">)</span>
 97</pre>
 98<p>
 99        we generate an instance of <code class="computeroutput"><span class="identifier">while_gen</span><span class="special">&lt;</span><span class="identifier">Cond</span><span class="special">&gt;</span></code>, where <code class="computeroutput"><span class="identifier">Cond</span></code>
100        is the type of <code class="computeroutput"><span class="identifier">cond</span></code>. <code class="computeroutput"><span class="identifier">cond</span></code> can be an arbitrarily complex actor
101        expression. The <code class="computeroutput"><span class="identifier">while_gen</span></code>
102        template class has an <code class="computeroutput"><span class="keyword">operator</span><span class="special">[]</span></code> accepting another expression. If we write:
103      </p>
104<pre class="programlisting"><span class="identifier">while_</span><span class="special">(</span><span class="identifier">cond</span><span class="special">)</span>
105<span class="special">[</span>
106    <span class="identifier">do_</span>
107<span class="special">]</span>
108</pre>
109<p>
110        it will generate a proper composite with the type:
111      </p>
112<pre class="programlisting"><span class="identifier">expression</span><span class="special">::</span><span class="identifier">while_</span><span class="special">&lt;</span><span class="identifier">Cond</span><span class="special">,</span> <span class="identifier">Do</span><span class="special">&gt;::</span><span class="identifier">type</span>
113</pre>
114<p>
115        where <code class="computeroutput"><span class="identifier">Cond</span></code> is the type of
116        <code class="computeroutput"><span class="identifier">cond</span></code> and <code class="computeroutput"><span class="identifier">Do</span></code>
117        is the type of <code class="computeroutput"><span class="identifier">do_</span></code>. Notice
118        how we are using Phoenix's <a class="link" href="../inside/expression.html" title="Phoenix Expressions">Expression</a>
119        mechanism here
120      </p>
121<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Do</span><span class="special">&gt;</span>
122<span class="keyword">typename</span> <span class="identifier">expression</span><span class="special">::</span><span class="identifier">while_</span><span class="special">&lt;</span><span class="identifier">Cond</span><span class="special">,</span> <span class="identifier">Do</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="keyword">const</span>
123<span class="keyword">operator</span><span class="special">[](</span><span class="identifier">Do</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">do_</span><span class="special">)</span> <span class="keyword">const</span>
124<span class="special">{</span>
125    <span class="keyword">return</span> <span class="identifier">expression</span><span class="special">::</span><span class="identifier">while_</span><span class="special">&lt;</span><span class="identifier">Cond</span><span class="special">,</span> <span class="identifier">Do</span><span class="special">&gt;::</span><span class="identifier">make</span><span class="special">(</span><span class="identifier">cond</span><span class="special">,</span> <span class="identifier">do_</span><span class="special">);</span>
126<span class="special">}</span>
127</pre>
128<p>
129        Finally, the <code class="computeroutput"><span class="identifier">while_eval</span></code> does
130        its thing:
131      </p>
132<pre class="programlisting"><span class="keyword">while</span><span class="special">(</span><span class="identifier">eval</span><span class="special">(</span><span class="identifier">cond</span><span class="special">,</span> <span class="identifier">ctx</span><span class="special">))</span>
133<span class="special">{</span>
134    <span class="identifier">eval</span><span class="special">(</span><span class="identifier">do_</span><span class="special">,</span> <span class="identifier">ctx</span><span class="special">);</span>
135<span class="special">}</span>
136</pre>
137<p>
138        <code class="computeroutput"><span class="identifier">cond</span></code> and <code class="computeroutput"><span class="identifier">do_</span></code>,
139        at this point, are instances of <a class="link" href="../inside/actor.html" title="Actors in Detail">Actor</a>.
140        <code class="computeroutput"><span class="identifier">cond</span></code> and <code class="computeroutput"><span class="identifier">do_</span></code>
141        are the <a class="link" href="../inside/actor.html" title="Actors in Detail">Actors</a> passed as parameters
142        by <code class="computeroutput"><span class="identifier">call</span></code>, ctx is the <a class="link" href="../inside/actor.html" title="Actors in Detail">Context</a>
143      </p>
144</div>
145<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
146<td align="left"></td>
147<td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005, 2010 Joel de Guzman, Dan Marsden, Thomas Heller<p>
148        Distributed under the Boost Software License, Version 1.0. (See accompanying
149        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
150      </p>
151</div></td>
152</tr></table>
153<hr>
154<div class="spirit-nav">
155<a accesskey="p" href="extending_actors.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="transforming_the_expression_tree.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
156</div>
157</body>
158</html>