$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r55492 - trunk/libs/spirit/example/karma
From: hartmut.kaiser_at_[hidden]
Date: 2009-08-09 15:02:07
Author: hkaiser
Date: 2009-08-09 15:02:06 EDT (Sun, 09 Aug 2009)
New Revision: 55492
URL: http://svn.boost.org/trac/boost/changeset/55492
Log:
Spirit: simplified a couple of the examples
Text files modified: 
   trunk/libs/spirit/example/karma/calc2_ast.hpp      |    62 ----------------------------------------
   trunk/libs/spirit/example/karma/calc2_ast_dump.cpp |    41 ++++++++++++++------------              
   trunk/libs/spirit/example/karma/calc2_ast_rpn.cpp  |    43 ++++++++++++++-------------             
   trunk/libs/spirit/example/karma/calc2_ast_vm.cpp   |    46 ++++++++++++++--------------            
   4 files changed, 67 insertions(+), 125 deletions(-)
Modified: trunk/libs/spirit/example/karma/calc2_ast.hpp
==============================================================================
--- trunk/libs/spirit/example/karma/calc2_ast.hpp	(original)
+++ trunk/libs/spirit/example/karma/calc2_ast.hpp	2009-08-09 15:02:06 EDT (Sun, 09 Aug 2009)
@@ -165,66 +165,4 @@
 boost::phoenix::function<unary_expr<'+'> > pos;
 boost::phoenix::function<unary_expr<'-'> > neg;
 
-///////////////////////////////////////////////////////////////////////////////
-//  A couple of phoenix functions helping to access the elements of the 
-//  generated AST
-///////////////////////////////////////////////////////////////////////////////
-template <typename T>
-struct get_element
-{
-    template <typename T1>
-    struct result { typedef T const& type; };
-
-    T const& operator()(expression_ast const& expr) const
-    {
-        return boost::get<T>(expr.expr);
-    }
-};
-
-boost::phoenix::function<get_element<int> > _int;
-boost::phoenix::function<get_element<binary_op> > _bin_op;
-boost::phoenix::function<get_element<unary_op> > _unary_op;
-
-///////////////////////////////////////////////////////////////////////////////
-struct get_left
-{
-    template <typename T1>
-    struct result { typedef expression_ast const& type; };
-
-    expression_ast const& operator()(binary_op const& bin_op) const
-    {
-        return bin_op.left;
-    }
-};
-
-boost::phoenix::function<get_left> _left;
-
-struct get_right
-{
-    template <typename T1>
-    struct result { typedef expression_ast const& type; };
-
-    template <typename Node>
-    expression_ast const& operator()(Node const& op) const
-    {
-        return op.right;
-    }
-};
-
-boost::phoenix::function<get_right> _right;
-
-struct get_op
-{
-    template <typename T1>
-    struct result { typedef char type; };
-
-    template <typename Node>
-    char operator()(Node const& bin_op) const
-    {
-        return bin_op.op;
-    }
-};
-
-boost::phoenix::function<get_op> _op;
-
 #endif
Modified: trunk/libs/spirit/example/karma/calc2_ast_dump.cpp
==============================================================================
--- trunk/libs/spirit/example/karma/calc2_ast_dump.cpp	(original)
+++ trunk/libs/spirit/example/karma/calc2_ast_dump.cpp	2009-08-09 15:02:06 EDT (Sun, 09 Aug 2009)
@@ -24,6 +24,7 @@
 
 #include <boost/spirit/include/qi.hpp>
 #include <boost/spirit/include/karma.hpp>
+#include <boost/fusion/include/adapt_struct.hpp>
 
 using namespace boost::spirit;
 using namespace boost::spirit::ascii;
@@ -62,6 +63,24 @@
     qi::rule<Iterator, expression_ast(), space_type> expression, term, factor;
 };
 
+// We need to tell fusion about our binary_op and unary_op structs
+// to make them a first-class fusion citizen
+//
+// Note: we register the members exactly in the same sequence as we need them 
+//       in the grammar
+BOOST_FUSION_ADAPT_STRUCT(
+    binary_op,
+    (expression_ast, left)
+    (char, op)
+    (expression_ast, right)
+)
+
+BOOST_FUSION_ADAPT_STRUCT(
+    unary_op,
+    (char, op)
+    (expression_ast, right)
+)
+
 ///////////////////////////////////////////////////////////////////////////////
 //  Our AST grammar for the generator, this just dumps the AST as a expression
 ///////////////////////////////////////////////////////////////////////////////
@@ -71,25 +90,9 @@
 {
     dump_ast() : dump_ast::base_type(ast_node)
     {
-        ast_node %= 
-                int_        [_1 = _int(_val)]
-            |   binary_node [_1 = _bin_op(_val)]
-            |   unary_node  [_1 = _unary_op(_val)]
-            ;
-
-        binary_node = 
-                ('(' << ast_node << char_ << ast_node << ')')
-                [ 
-                    _1 = _left(_val), _2 = _op(_val), _3 = _right(_val)
-                ]
-            ;
-
-        unary_node =
-                ('(' << char_ << ast_node << ')')
-                [
-                    _1 = _op(_val), _2 = _right(_val)
-                ]
-            ;
+        ast_node %= int_ | binary_node | unary_node;
+        binary_node %= '(' << ast_node << char_ << ast_node << ')';
+        unary_node %= '(' << char_ << ast_node << ')';
     }
 
     karma::rule<OuputIterator, expression_ast(), space_type> ast_node;
Modified: trunk/libs/spirit/example/karma/calc2_ast_rpn.cpp
==============================================================================
--- trunk/libs/spirit/example/karma/calc2_ast_rpn.cpp	(original)
+++ trunk/libs/spirit/example/karma/calc2_ast_rpn.cpp	2009-08-09 15:02:06 EDT (Sun, 09 Aug 2009)
@@ -24,6 +24,7 @@
 
 #include <boost/spirit/include/qi.hpp>
 #include <boost/spirit/include/karma.hpp>
+#include <boost/fusion/include/adapt_struct.hpp>
 
 using namespace boost::spirit;
 using namespace boost::spirit::ascii;
@@ -62,6 +63,24 @@
     qi::rule<Iterator, expression_ast(), space_type> expression, term, factor;
 };
 
+// We need to tell fusion about our binary_op and unary_op structs
+// to make them a first-class fusion citizen
+//
+// Note: we register the members exactly in the same sequence as we need them 
+//       in the grammar
+BOOST_FUSION_ADAPT_STRUCT(
+    binary_op,
+    (expression_ast, left)
+    (expression_ast, right)
+    (char, op)
+)
+
+BOOST_FUSION_ADAPT_STRUCT(
+    unary_op,
+    (expression_ast, right)
+    (char, op)
+)
+
 ///////////////////////////////////////////////////////////////////////////////
 //  Our AST grammar for the generator, this prints the AST in reverse polish 
 //  notation
@@ -72,27 +91,9 @@
 {
     ast_rpn() : ast_rpn::base_type(ast_node)
     {
-        ast_node %= 
-                int_        [_1 = _int(_val)]
-            |   binary_node [_1 = _bin_op(_val)]
-            |   unary_node  [_1 = _unary_op(_val)]
-            ;
-
-        binary_node = 
-                (ast_node << ast_node << char_)
-                [ 
-                    _1 = _left(_val), _2 = _right(_val), _3 = _op(_val)
-                ]
-            ;
-
-        unary_node =
-//                 verbatim [
-                    ('(' << ast_node << char_ << ')')
-                    [
-                        _1 = _right(_val), _2 = _op(_val)
-                    ]
-//                 ]
-            ;
+        ast_node %= int_ | binary_node | unary_node;
+        binary_node %= ast_node << ast_node << char_;
+        unary_node %= '(' << ast_node << char_ << ')';
     }
 
     karma::rule<OuputIterator, expression_ast(), space_type> ast_node;
Modified: trunk/libs/spirit/example/karma/calc2_ast_vm.cpp
==============================================================================
--- trunk/libs/spirit/example/karma/calc2_ast_vm.cpp	(original)
+++ trunk/libs/spirit/example/karma/calc2_ast_vm.cpp	2009-08-09 15:02:06 EDT (Sun, 09 Aug 2009)
@@ -25,6 +25,7 @@
 
 #include <boost/spirit/include/qi.hpp>
 #include <boost/spirit/include/karma.hpp>
+#include <boost/fusion/include/adapt_struct.hpp>
 
 using namespace boost::spirit;
 using namespace boost::spirit::ascii;
@@ -128,6 +129,24 @@
     }
 }
 
+// We need to tell fusion about our binary_op and unary_op structs
+// to make them a first-class fusion citizen
+//
+// Note: we register the members exactly in the same sequence as we need them 
+//       in the grammar
+BOOST_FUSION_ADAPT_STRUCT(
+    binary_op,
+    (expression_ast, left)
+    (expression_ast, right)
+    (int, op)
+)
+
+BOOST_FUSION_ADAPT_STRUCT(
+    unary_op,
+    (expression_ast, right)
+    (int, op)
+)
+
 ///////////////////////////////////////////////////////////////////////////////
 //  Our AST grammar for the generator, this just dumps the AST as a expression
 ///////////////////////////////////////////////////////////////////////////////
@@ -137,29 +156,10 @@
 {
     generate_byte_code() : generate_byte_code::base_type(ast_node)
     {
-        ast_node %= 
-                int_node    [_1 = _int(_val)]
-            |   binary_node [_1 = _bin_op(_val)]
-            |   unary_node  [_1 = _unary_op(_val)]
-            ;
-
-        int_node %=
-                dword(op_int) << dword
-            ;
-
-        binary_node = 
-                (ast_node << ast_node << byte_)
-                [ 
-                    _1 = _left(_val), _2 = _right(_val), _3 = _op(_val)
-                ]
-            ;
-
-        unary_node =
-                (ast_node << byte_)
-                [
-                    _1 = _right(_val), _2 = _op(_val)
-                ]
-            ;
+        ast_node %= int_node | binary_node | unary_node;
+        int_node %= dword(op_int) << dword;
+        binary_node %= ast_node << ast_node << byte_;
+        unary_node %= ast_node << byte_;
     }
 
     karma::rule<OuputIterator, expression_ast(), Delimiter> ast_node;