Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61521 - in trunk/libs/spirit/example/scheme: scheme test test/utree utree utree/detail
From: joel_at_[hidden]
Date: 2010-04-23 20:40:40


Author: djowel
Date: 2010-04-23 20:40:39 EDT (Fri, 23 Apr 2010)
New Revision: 61521
URL: http://svn.boost.org/trac/boost/changeset/61521

Log:
Added string_range_type (shallow strings).
Added:
   trunk/libs/spirit/example/scheme/test/qi_interpreter.cpp (contents, props changed)
Text files modified:
   trunk/libs/spirit/example/scheme/scheme/interpreter.hpp | 6 +++---
   trunk/libs/spirit/example/scheme/test/utree/utree_test.cpp | 15 +++++++++++++++
   trunk/libs/spirit/example/scheme/utree/detail/utree_detail1.hpp | 9 +++++++++
   trunk/libs/spirit/example/scheme/utree/detail/utree_detail2.hpp | 17 +++++++++++++++++
   trunk/libs/spirit/example/scheme/utree/utree.hpp | 8 +++++++-
   5 files changed, 51 insertions(+), 4 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-23 20:40:39 EDT (Fri, 23 Apr 2010)
@@ -170,7 +170,7 @@
         }
     };
 
- template <>
+ template <> // scoped = false
     struct argument_function<false> : actor<argument_function<false> >
     {
         std::size_t n;
@@ -250,7 +250,7 @@
         }
     };
 
- template <>
+ template <> // scoped = false
     struct vararg_function<false> : actor<vararg_function<false> >
     {
         std::size_t n;
@@ -287,7 +287,7 @@
     // scoped varg
     vararg<true> const varg = {};
 
- // unscoped arg
+ // unscoped varg
     vararg<false> const unscoped_varg = {};
 
     // unscoped vargs

Added: trunk/libs/spirit/example/scheme/test/qi_interpreter.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/scheme/test/qi_interpreter.cpp 2010-04-23 20:40:39 EDT (Fri, 23 Apr 2010)
@@ -0,0 +1,34 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#include <boost/detail/lightweight_test.hpp>
+#include <boost/config/warning_disable.hpp>
+
+#include <input/sexpr.hpp>
+#include <input/parse_sexpr_impl.hpp>
+#include <scheme/compiler.hpp>
+#include <utree/io.hpp>
+
+namespace scheme
+{
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Main program
+///////////////////////////////////////////////////////////////////////////////
+int main()
+{
+ using scheme::interpreter;
+ using scheme::utree;
+
+ utree src = "(define (factorial n) (if (<= n 0) 1 (* n (factorial (- n 1)))))";
+ scheme::interpreter program(src);
+ BOOST_TEST(program["factorial"](10) == 3628800);
+
+ return boost::report_errors();
+}
+
+

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-23 20:40:39 EDT (Fri, 23 Apr 2010)
@@ -302,5 +302,20 @@
         BOOST_TEST(alias[1] == 3);
     }
 
+ {
+ using scheme::utf8_string_range;
+ using scheme::shallow;
+
+ // shallow string ranges
+ char const* s = "Hello, World";
+ utree val(utf8_string_range(s, s + std::strlen(s)), shallow);
+ check(val, "\"Hello, World\"");
+
+ utf8_string_range r = val.get<utf8_string_range>();
+ utf8_string_range pf(r.begin()+1, r.end()-1);
+ val = utree(pf, shallow);
+ check(val, "\"ello, Worl\"");
+ }
+
     return boost::report_errors();
 }

Modified: trunk/libs/spirit/example/scheme/utree/detail/utree_detail1.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/utree/detail/utree_detail1.hpp (original)
+++ trunk/libs/spirit/example/scheme/utree/detail/utree_detail1.hpp 2010-04-23 20:40:39 EDT (Fri, 23 Apr 2010)
@@ -65,6 +65,15 @@
     };
 
     ///////////////////////////////////////////////////////////////////////////
+ // A range of char*s
+ ///////////////////////////////////////////////////////////////////////////
+ struct string_range
+ {
+ char const* first;
+ char const* last;
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
     // Our POD fast string. This implementation is very primitive and is not
     // meant to be used stand-alone. This is the internal data representation
     // of strings in our utree. This is deliberately a POD to allow it to be

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-23 20:40:39 EDT (Fri, 23 Apr 2010)
@@ -468,6 +468,9 @@
                 case type::string_type:
                     return f(utf8_string_range(x.s.str(), x.s.size()));
 
+ case type::string_range_type:
+ return f(utf8_string_range(x.sr.first, x.sr.last));
+
                 case type::symbol_type:
                     return f(utf8_symbol_range(x.s.str(), x.s.size()));
 
@@ -527,6 +530,10 @@
                     return visit_impl::apply(y, detail::bind(
                         f, utf8_string_range(x.s.str(), x.s.size())));
 
+ case type::string_range_type:
+ return visit_impl::apply(y, detail::bind(
+ f, utf8_string_range(x.sr.first, x.sr.last)));
+
                 case type::symbol_type:
                     return visit_impl::apply(y, detail::bind(
                         f, utf8_symbol_range(x.s.str(), x.s.size())));
@@ -673,6 +680,13 @@
         set_type(type::range_type);
     }
 
+ inline utree::utree(utf8_string_range const& str, shallow_tag)
+ {
+ this->sr.first = str.begin();
+ this->sr.last = str.end();
+ set_type(type::string_range_type);
+ }
+
     inline utree::utree(utree const& other)
     {
         copy(other);
@@ -1204,6 +1218,9 @@
             case type::range_type:
                 r = other.r;
                 break;
+ case type::string_range_type:
+ sr = other.sr;
+ break;
             case type::function_type:
                 pf = other.pf->clone();
                 break;

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-23 20:40:39 EDT (Fri, 23 Apr 2010)
@@ -42,6 +42,7 @@
             int_type,
             double_type,
             string_type,
+ string_range_type,
             symbol_type,
             binary_type,
             list_type,
@@ -169,10 +170,13 @@
     // - an integer
     // - a double
     // - a string
+ // - a string range
     // - a symbol (identifier)
     // - binary data
     // - a (doubly linked) list of utree
+ // - an iterator_range of list::iterator
     // - a reference to a utree
+ // - a function
     //
     // The utree has minimal memory footprint. The data structure size is
     // 16 bytes on a 32-bit platform. Being a container of itself, it can
@@ -209,12 +213,13 @@
         utree(boost::iterator_range<Iter> r);
         utree(range r, shallow_tag);
         utree(const_range r, shallow_tag);
+ utree(utf8_string_range const& str, shallow_tag);
 
         template <typename F>
         utree(stored_function<F> const& pf);
 
         template <typename Base, utree_type::info type_>
- utree(basic_string<Base, type_> const& bin);
+ utree(basic_string<Base, type_> const& str);
 
         utree(utree const& other);
         ~utree();
@@ -341,6 +346,7 @@
             detail::fast_string s;
             detail::list l;
             detail::range r;
+ detail::string_range sr;
             bool b;
             int i;
             double d;


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