Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61238 - trunk/libs/spirit/example/scheme
From: joel_at_[hidden]
Date: 2010-04-13 01:50:15


Author: djowel
Date: 2010-04-13 01:50:15 EDT (Tue, 13 Apr 2010)
New Revision: 61238
URL: http://svn.boost.org/trac/boost/changeset/61238

Log:
good to go!
Text files modified:
   trunk/libs/spirit/example/scheme/scheme_interpreter.hpp | 159 ++++++++++++++++++++++++++++++++++-----
   trunk/libs/spirit/example/scheme/scheme_intinsics.hpp | 99 ++++--------------------
   2 files changed, 153 insertions(+), 105 deletions(-)

Modified: trunk/libs/spirit/example/scheme/scheme_interpreter.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/scheme_interpreter.hpp (original)
+++ trunk/libs/spirit/example/scheme/scheme_interpreter.hpp 2010-04-13 01:50:15 EDT (Tue, 13 Apr 2010)
@@ -89,10 +89,10 @@
     ///////////////////////////////////////////////////////////////////////////
     // values
     ///////////////////////////////////////////////////////////////////////////
- struct value
+ struct value_function
     {
         utree val;
- value(utree const& val) : val(val) {}
+ value_function(utree const& val) : val(val) {}
 
         typedef utree result_type;
         utree operator()(args_type /*args*/) const
@@ -101,24 +101,24 @@
         }
     };
 
- struct value_composer
+ struct value
     {
         typedef actor result_type;
         actor operator()(utree const& val) const
         {
- return actor(value(val));
+ return actor(value_function(val));
         }
     };
 
- value_composer const val = {};
+ value const val = {};
 
     ///////////////////////////////////////////////////////////////////////////
     // arguments
     ///////////////////////////////////////////////////////////////////////////
- struct argument
+ struct argument_function
     {
- int n;
- argument(int n) : n(n) {}
+ std::size_t n;
+ argument_function(std::size_t n) : n(n) {}
 
         typedef utree result_type;
         utree operator()(args_type args) const
@@ -127,16 +127,16 @@
         }
     };
 
- struct argument_composer
+ struct argument
     {
         typedef actor result_type;
- actor operator()(int n) const
+ actor operator()(std::size_t n) const
         {
- return actor(argument(n));
+ return actor(argument_function(n));
         }
     };
 
- argument_composer const arg = {};
+ argument const arg = {};
     actor const _1 = arg(0);
     actor const _2 = arg(1);
     actor const _3 = arg(2);
@@ -151,17 +151,6 @@
     ///////////////////////////////////////////////////////////////////////////
     // composite
     ///////////////////////////////////////////////////////////////////////////
- template <typename T>
- inline actor as_function(T const& val)
- {
- return scheme::val(utree(val));
- }
-
- inline actor const& as_function(actor const& f)
- {
- return f;
- }
-
     template <typename Derived>
     struct composite
     {
@@ -197,6 +186,130 @@
         {
             return *static_cast<Derived const*>(this);
         }
+
+ template <typename T>
+ static actor as_function(T const& val)
+ {
+ return scheme::val(utree(val));
+ }
+
+ static actor const& as_function(actor const& f)
+ {
+ return f;
+ }
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ // unary_function
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename Derived>
+ struct unary_function
+ {
+ actor a;
+ typedef unary_function<Derived> base_type;
+
+ unary_function(actor const& a)
+ : a(a) {}
+
+ typedef utree result_type;
+ utree operator()(args_type args) const
+ {
+ return derived().eval(a(args));
+ }
+
+ Derived const& derived() const
+ {
+ return *static_cast<Derived const*>(this);
+ }
+ };
+
+ template <typename Function>
+ struct unary_composite : composite<unary_composite<Function> >
+ {
+ using base_type::operator();
+ actor operator()(actor_list const& elements) const
+ {
+ return actor(Function(elements.front()));
+ }
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ // binary_function
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename Derived>
+ struct binary_function
+ {
+ actor a;
+ actor b;
+ typedef binary_function<Derived> base_type;
+
+ binary_function(actor const& a, actor const& b)
+ : a(a), b(b) {}
+
+ typedef utree result_type;
+ utree operator()(args_type args) const
+ {
+ return derived().eval(a(args), b(args));
+ }
+
+ Derived const& derived() const
+ {
+ return *static_cast<Derived const*>(this);
+ }
+ };
+
+ template <typename Function>
+ struct binary_composite : composite<binary_composite<Function> >
+ {
+ using base_type::operator();
+ actor operator()(actor_list const& elements) const
+ {
+ actor_list::const_iterator i = elements.begin();
+ actor a = *i++;
+ actor b = *i;
+ return actor(Function(a, b));
+ }
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ // vararg_function
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename Derived>
+ struct vararg_function : composite<Derived>
+ {
+ typedef vararg_function<Derived> base_type;
+ actor_list elements;
+ vararg_function(actor_list const& elements)
+ : elements(elements) {}
+
+ using composite<Derived>::operator();
+ utree operator()(args_type args) const
+ {
+ actor_list::const_iterator i = elements.begin();
+ utree result = (*i++)(args);
+ boost::iterator_range<actor_list::const_iterator>
+ rest(i++, elements.end());
+ BOOST_FOREACH(actor const& element, rest)
+ {
+ derived().eval(result, element(args));
+ }
+ return result;
+ }
+
+ Derived const& derived() const
+ {
+ return *static_cast<Derived const*>(this);
+ }
+ };
+
+ template <typename Function>
+ struct vararg_composite : composite<vararg_composite<Function> >
+ {
+ using base_type::operator();
+ actor operator()(actor_list const& elements) const
+ {
+ return actor(Function(elements));
+ }
     };
 
     ///////////////////////////////////////////////////////////////////////////

Modified: trunk/libs/spirit/example/scheme/scheme_intinsics.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/scheme_intinsics.hpp (original)
+++ trunk/libs/spirit/example/scheme/scheme_intinsics.hpp 2010-04-13 01:50:15 EDT (Tue, 13 Apr 2010)
@@ -30,7 +30,7 @@
         }
     };
 
- struct if_composer : composite<if_composer>
+ struct if_composite : composite<if_composite>
     {
         using base_type::operator();
         actor operator()(actor_list const& elements) const
@@ -43,71 +43,30 @@
         }
     };
 
- if_composer const if_ = if_composer();
+ if_composite const if_ = if_composite();
 
     ///////////////////////////////////////////////////////////////////////////
     // less_than_equal
     ///////////////////////////////////////////////////////////////////////////
     struct less_than_equal_function
+ : binary_function<less_than_equal_function>
     {
- actor a;
- actor b;
         less_than_equal_function(actor const& a, actor const& b)
- : a(a), b(b) {}
+ : base_type(a, b) {}
 
         typedef utree result_type;
- utree operator()(args_type args) const
+ utree eval(utree const& a, utree const& b) const
         {
- return a(args) <= b(args);
+ return a <= b;
         }
     };
 
- struct less_than_equal_composer : composite<less_than_equal_composer>
- {
- using base_type::operator();
- actor operator()(actor_list const& elements) const
- {
- actor_list::const_iterator i = elements.begin();
- actor a = *i++;
- actor b = *i;
- return actor(less_than_equal_function(a, b));
- }
- };
-
- less_than_equal_composer const less_than_equal
- = less_than_equal_composer();
- less_than_equal_composer const lte = less_than_equal; // synonym
-
- ///////////////////////////////////////////////////////////////////////////
- // vararg_function
- ///////////////////////////////////////////////////////////////////////////
- template <typename Derived>
- struct vararg_function : composite<Derived>
- {
- typedef vararg_function<Derived> base_type;
- actor_list elements;
- vararg_function(actor_list const& elements)
- : elements(elements) {}
-
- using composite<Derived>::operator();
- utree operator()(args_type args) const
- {
- actor_list::const_iterator i = elements.begin();
- utree result = (*i++)(args);
- boost::iterator_range<actor_list::const_iterator>
- rest(i++, elements.end());
- BOOST_FOREACH(actor const& element, rest)
- {
- derived().eval(result, element(args));
- }
- return result;
- }
+ struct less_than_equal_composite
+ : binary_composite<less_than_equal_function> {};
 
- Derived const& derived() const
- {
- return *static_cast<Derived const*>(this);
- }
- };
+ less_than_equal_composite const less_than_equal
+ = less_than_equal_composite();
+ less_than_equal_composite const lte = less_than_equal; // synonym
 
     ///////////////////////////////////////////////////////////////////////////
     // plus
@@ -123,16 +82,8 @@
         }
     };
 
- struct plus_composer : composite<plus_composer>
- {
- using base_type::operator();
- actor operator()(actor_list const& elements) const
- {
- return actor(plus_function(elements));
- }
- };
-
- plus_composer const plus = plus_composer();
+ struct plus_composite : vararg_composite<plus_function> {};
+ plus_composite const plus = plus_composite();
 
     ///////////////////////////////////////////////////////////////////////////
     // minus
@@ -148,16 +99,8 @@
         }
     };
 
- struct minus_composer : composite<minus_composer>
- {
- using base_type::operator();
- actor operator()(actor_list const& elements) const
- {
- return actor(minus_function(elements));
- }
- };
-
- minus_composer const minus = minus_composer();
+ struct minus_composite : vararg_composite<minus_function> {};
+ minus_composite const minus = minus_composite();
 
     ///////////////////////////////////////////////////////////////////////////
     // times
@@ -173,16 +116,8 @@
         }
     };
 
- struct times_composer : composite<times_composer>
- {
- using base_type::operator();
- actor operator()(actor_list const& elements) const
- {
- return actor(times_function(elements));
- }
- };
-
- times_composer const times = times_composer();
+ struct times_composite : vararg_composite<times_function> {};
+ times_composite const times = times_composite();
 }
 
 #endif


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk