Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61446 - in trunk/libs/spirit/example/scheme: scheme test/scheme test/utree utree utree/detail
From: joel_at_[hidden]
Date: 2010-04-21 00:12:50


Author: djowel
Date: 2010-04-21 00:12:48 EDT (Wed, 21 Apr 2010)
New Revision: 61446
URL: http://svn.boost.org/trac/boost/changeset/61446

Log:
+ adding function_type to utree
+ factoring out utree i/o and adding a PP define to choose simple i/o and spirit i/o using qi and karma.
Text files modified:
   trunk/libs/spirit/example/scheme/scheme/compiler.hpp | 2
   trunk/libs/spirit/example/scheme/scheme/interpreter.hpp | 1
   trunk/libs/spirit/example/scheme/test/scheme/scheme_test2.cpp | 1
   trunk/libs/spirit/example/scheme/test/utree/utree_test.cpp | 16 ++++
   trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp | 62 +++++++++++++++-
   trunk/libs/spirit/example/scheme/utree/io.hpp | 5 +
   trunk/libs/spirit/example/scheme/utree/operators.hpp | 147 ++++++++++++++++++++-------------------
   trunk/libs/spirit/example/scheme/utree/utree.hpp | 37 +++++++++
   8 files changed, 190 insertions(+), 81 deletions(-)

Modified: trunk/libs/spirit/example/scheme/scheme/compiler.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/scheme/compiler.hpp (original)
+++ trunk/libs/spirit/example/scheme/scheme/compiler.hpp 2010-04-21 00:12:48 EDT (Wed, 21 Apr 2010)
@@ -372,7 +372,7 @@
             {
                 f = compile(program, env, fragments, line, source_file);
             }
- catch (compilation_error const& x)
+ catch (compilation_error const&)
             {
                 continue; // try the next expression
             }

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-21 00:12:48 EDT (Wed, 21 Apr 2010)
@@ -29,7 +29,6 @@
     struct function;
 
     typedef std::list<function> actor_list;
- typedef boost::iterator_range<utree const*> args_type;
     typedef boost::function<utree(args_type args)> stored_function;
 
     ///////////////////////////////////////////////////////////////////////////

Modified: trunk/libs/spirit/example/scheme/test/scheme/scheme_test2.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/test/scheme/scheme_test2.cpp (original)
+++ trunk/libs/spirit/example/scheme/test/scheme/scheme_test2.cpp 2010-04-21 00:12:48 EDT (Wed, 21 Apr 2010)
@@ -7,6 +7,7 @@
 #include <boost/detail/lightweight_test.hpp>
 #include <boost/config/warning_disable.hpp>
 
+#include <input/parse_sexpr_impl.hpp>
 #include <scheme/compiler.hpp>
 #include <utree/io.hpp>
 #include <iostream>

Modified: trunk/libs/spirit/example/scheme/test/utree/utree_test.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/test/utree/utree_test.cpp (original)
+++ trunk/libs/spirit/example/scheme/test/utree/utree_test.cpp 2010-04-21 00:12:48 EDT (Wed, 21 Apr 2010)
@@ -17,9 +17,17 @@
 {
     std::stringstream s;
     s << val;
- BOOST_TEST(s.str() == expected + " ");
+ BOOST_ASSERT(s.str() == expected + " ");
 }
 
+struct one_two_three
+{
+ scheme::utree operator()(scheme::args_type) const
+ {
+ return scheme::utree(123);
+ }
+};
+
 int main()
 {
     using scheme::utree;
@@ -255,5 +263,11 @@
         BOOST_TEST(x.tag() == 123);
     }
 
+ {
+ // test functions
+ utree f = scheme::polymorphic_function<one_two_three>();
+ f.eval(scheme::args_type());
+ }
+
     return boost::report_errors();
 }

Modified: trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp (original)
+++ trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp 2010-04-21 00:12:48 EDT (Wed, 21 Apr 2010)
@@ -538,6 +538,30 @@
 
 namespace scheme
 {
+ template <typename F>
+ polymorphic_function<F>::polymorphic_function(F f)
+ : f(f)
+ {
+ }
+
+ template <typename F>
+ polymorphic_function<F>::~polymorphic_function()
+ {
+ };
+
+ template <typename F>
+ utree polymorphic_function<F>::operator()(args_type args) const
+ {
+ return f(args);
+ }
+
+ template <typename F>
+ polymorphic_function_base*
+ polymorphic_function<F>::clone() const
+ {
+ return new polymorphic_function<F>(*this);
+ }
+
     inline utree::utree()
     {
         set_type(type::nil_type);
@@ -594,8 +618,15 @@
         set_type(type::reference_type);
     }
 
+ template <typename F>
+ inline utree::utree(polymorphic_function<F> const& pf)
+ : pf(new polymorphic_function<F>(pf))
+ {
+ set_type(type::function_type);
+ }
+
     template <typename Iter>
- utree::utree(boost::iterator_range<Iter> r)
+ inline utree::utree(boost::iterator_range<Iter> r)
     {
         set_type(type::nil_type);
         assign(r.begin(), r.end());
@@ -686,8 +717,17 @@
         return *this;
     }
 
+ template <typename F>
+ utree& utree::operator=(polymorphic_function<F> const& pf)
+ {
+ free();
+ pf = new polymorphic_function<F>(pf);
+ set_type(type::function_type);
+ return *this;
+ }
+
     template <typename Iter>
- utree& utree::operator=(boost::iterator_range<Iter> r)
+ inline utree& utree::operator=(boost::iterator_range<Iter> r)
     {
         free();
         assign(r.begin(), r.end());
@@ -1007,6 +1047,9 @@
             case type::list_type:
                 l.free();
                 break;
+ case type::function_type:
+ delete pf;
+ break;
             default:
                 break;
         };
@@ -1031,6 +1074,9 @@
             case type::reference_type:
                 p = other.p;
                 break;
+ case type::function_type:
+ pf = other.pf->clone();
+ break;
             case type::string_type:
             case type::symbol_type:
             case type::binary_type:
@@ -1044,8 +1090,8 @@
     }
 
     template <typename T>
- struct is_iterator_range
- : boost::mpl::false_
+ struct is_iterator_range
+ : boost::mpl::false_
     {};
 
     template <typename Iterator>
@@ -1076,7 +1122,7 @@
         To operator()(From const& val) const
         {
             // boost::iterator_range has a templated constructor, accepting
- // any argument and hence any type is 'convertible' to it.
+ // any argument and hence any type is 'convertible' to it.
             typedef typename boost::mpl::eval_if<
                 is_iterator_range<To>
               , boost::is_same<From, To>, boost::is_convertible<From, To>
@@ -1112,6 +1158,12 @@
         ensure_list_type();
         s.tag(tag);
     }
+
+ inline utree utree::eval(args_type args) const
+ {
+ BOOST_ASSERT(get_type() == type::function_type);
+ return (*pf)(args);
+ }
 }
 
 #if defined(BOOST_MSVC)

Modified: trunk/libs/spirit/example/scheme/utree/io.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/utree/io.hpp (original)
+++ trunk/libs/spirit/example/scheme/utree/io.hpp 2010-04-21 00:12:48 EDT (Wed, 21 Apr 2010)
@@ -8,6 +8,9 @@
 
 #include <utree/utree.hpp>
 #include <utree/operators.hpp>
+
+#if defined(SCHEME_USE_SPIRIT_IO)
+
 #include <input/parse_sexpr_impl.hpp>
 #include <output/generate_sexpr_impl.hpp>
 
@@ -29,3 +32,5 @@
 }
 
 #endif
+
+#endif

Modified: trunk/libs/spirit/example/scheme/utree/operators.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/utree/operators.hpp (original)
+++ trunk/libs/spirit/example/scheme/utree/operators.hpp 2010-04-21 00:12:48 EDT (Wed, 21 Apr 2010)
@@ -147,73 +147,74 @@
         }
     };
 
-// struct utree_print
-// {
-// typedef void result_type;
-//
-// std::ostream& out;
-// utree_print(std::ostream& out) : out(out) {}
-//
-// void operator()(scheme::nil) const
-// {
-// out << "nil";
-// }
-//
-// template <typename T>
-// void operator()(T val) const
-// {
-// out << val;
-// }
-//
-// void operator()(bool b) const
-// {
-// out << (b ? "true" : "false");
-// }
-//
-// void operator()(binary_range const& b) const
-// {
-// out << "b";
-// out.width(2);
-// out.fill('0');
-//
-// typedef binary_range::const_iterator iterator;
-// for (iterator i = b.begin(); i != b.end(); ++i)
-// out << std::hex << int((unsigned char)*i);
-// out << std::dec;
-// }
-//
-// void operator()(utf8_string_range const& str) const
-// {
-// typedef utf8_string_range::const_iterator iterator;
-// iterator i = str.begin();
-// out << '"';
-// for (; i != str.end(); ++i)
-// out << *i;
-// out << '"';
-// }
-//
-// void operator()(utf8_symbol_range const& str) const
-// {
-// typedef utf8_symbol_range::const_iterator iterator;
-// iterator i = str.begin();
-// for (; i != str.end(); ++i)
-// out << *i;
-// }
-//
-// template <typename Iterator>
-// void operator()(boost::iterator_range<Iterator> const& range) const
-// {
-// typedef typename boost::iterator_range<Iterator>::const_iterator iterator;
-// (*this)('(');
-// for (iterator i = range.begin(); i != range.end(); ++i)
-// {
-// if (i != range.begin())
-// (*this)(' ');
-// scheme::utree::visit(*i, *this);
-// }
-// (*this)(')');
-// }
-// };
+#if !defined(SCHEME_USE_SPIRIT_IO)
+
+ struct utree_print
+ {
+ typedef void result_type;
+
+ std::ostream& out;
+ utree_print(std::ostream& out) : out(out) {}
+
+ void operator()(scheme::nil) const
+ {
+ out << "<nil> ";
+ }
+
+ template <typename T>
+ void operator()(T val) const
+ {
+ out << val << ' ';
+ }
+
+ void operator()(bool b) const
+ {
+ out << (b ? "true" : "false") << ' ';
+ }
+
+ void operator()(binary_range const& b) const
+ {
+ out << "#";
+ out.width(2);
+ out.fill('0');
+
+ typedef binary_range::const_iterator iterator;
+ for (iterator i = b.begin(); i != b.end(); ++i)
+ out << std::hex << int((unsigned char)*i);
+ out << std::dec << "# ";
+ }
+
+ void operator()(utf8_string_range const& str) const
+ {
+ typedef utf8_string_range::const_iterator iterator;
+ iterator i = str.begin();
+ out << '"';
+ for (; i != str.end(); ++i)
+ out << *i;
+ out << "\" ";
+ }
+
+ void operator()(utf8_symbol_range const& str) const
+ {
+ typedef utf8_symbol_range::const_iterator iterator;
+ iterator i = str.begin();
+ for (; i != str.end(); ++i)
+ out << *i;
+ }
+
+ template <typename Iterator>
+ void operator()(boost::iterator_range<Iterator> const& range) const
+ {
+ typedef typename boost::iterator_range<Iterator>::const_iterator iterator;
+ (*this)('(');
+ for (iterator i = range.begin(); i != range.end(); ++i)
+ {
+ scheme::utree::visit(*i, *this);
+ }
+ (*this)(')');
+ }
+ };
+#endif
 
     template <typename Base>
     struct logical_function
@@ -423,11 +424,13 @@
         return !(a < b);
     }
 
-// inline std::ostream& operator<<(std::ostream& out, utree const& x)
-// {
-// utree::visit(x, utree_print(out));
-// return out;
-// }
+#if !defined(SCHEME_USE_SPIRIT_IO)
+ inline std::ostream& operator<<(std::ostream& out, utree const& x)
+ {
+ utree::visit(x, utree_print(out));
+ return out;
+ }
+#endif
 
     SCHEME_CREATE_LOGICAL_FUNCTION(and_, a&&b);
     SCHEME_CREATE_LOGICAL_FUNCTION(or_, a||b);

Modified: trunk/libs/spirit/example/scheme/utree/utree.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/utree/utree.hpp (original)
+++ trunk/libs/spirit/example/scheme/utree/utree.hpp 2010-04-21 00:12:48 EDT (Wed, 21 Apr 2010)
@@ -45,7 +45,8 @@
             symbol_type,
             binary_type,
             list_type,
- reference_type
+ reference_type,
+ function_type
         };
     };
 
@@ -130,6 +131,29 @@
     utf8_symbol;
 
     ///////////////////////////////////////////////////////////////////////////
+ // Our function type
+ ///////////////////////////////////////////////////////////////////////////
+ class utree;
+ typedef boost::iterator_range<utree const*> args_type;
+
+ struct polymorphic_function_base
+ {
+ virtual ~polymorphic_function_base() {};
+ virtual utree operator()(args_type args) const = 0;
+ virtual polymorphic_function_base* clone() const = 0;
+ };
+
+ template <typename F>
+ struct polymorphic_function : polymorphic_function_base
+ {
+ F f;
+ polymorphic_function(F f = F());
+ virtual ~polymorphic_function();
+ virtual utree operator()(args_type args) const;
+ virtual polymorphic_function_base* clone() const;
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
     // The main utree (Universal Tree) class
     // The utree is a hierarchical, dynamic type that can store:
     // - a nil
@@ -172,9 +196,13 @@
         utree(char const* str, std::size_t len);
         utree(std::string const& str);
         utree(boost::reference_wrapper<utree> ref);
+
         template <typename Iter>
         utree(boost::iterator_range<Iter> r);
 
+ template <typename F>
+ utree(polymorphic_function<F> const& pf);
+
         template <typename Base, utree_type::info type_>
         utree(basic_string<Base, type_> const& bin);
 
@@ -189,6 +217,10 @@
         utree& operator=(char const* s);
         utree& operator=(std::string const& s);
         utree& operator=(boost::reference_wrapper<utree> ref);
+
+ template <typename F>
+ utree& operator=(polymorphic_function<F> const& pf);
+
         template <typename Iter>
         utree& operator=(boost::iterator_range<Iter> r);
 
@@ -275,6 +307,8 @@
         short tag() const;
         void tag(short tag);
 
+ utree eval(args_type args) const;
+
     private:
 
         typedef utree_type type;
@@ -300,6 +334,7 @@
             int i;
             double d;
             utree* p;
+ polymorphic_function_base* pf;
         };
     };
 }


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