|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r61236 - in trunk/libs/spirit/example/scheme: . detail test
From: joel_at_[hidden]
Date: 2010-04-13 00:42:55
Author: djowel
Date: 2010-04-13 00:42:54 EDT (Tue, 13 Apr 2010)
New Revision: 61236
URL: http://svn.boost.org/trac/boost/changeset/61236
Log:
+ optimized argument synthesis
+ renaming
Added:
trunk/libs/spirit/example/scheme/detail/scheme_composite_call.hpp
- copied, changed from r61235, /trunk/libs/spirit/example/scheme/detail/scheme_function_composer_call.hpp
Removed:
trunk/libs/spirit/example/scheme/detail/scheme_function_composer_call.hpp
Text files modified:
trunk/libs/spirit/example/scheme/detail/scheme_composite_call.hpp | 2
trunk/libs/spirit/example/scheme/detail/scheme_function_call.hpp | 6 +-
trunk/libs/spirit/example/scheme/scheme_compiler.hpp | 30 +++++------
trunk/libs/spirit/example/scheme/scheme_interpreter.hpp | 106 ++++++++++++++++++++++-----------------
trunk/libs/spirit/example/scheme/scheme_intinsics.hpp | 37 ++++++-------
trunk/libs/spirit/example/scheme/test/scheme.cpp | 1
6 files changed, 95 insertions(+), 87 deletions(-)
Copied: trunk/libs/spirit/example/scheme/detail/scheme_composite_call.hpp (from r61235, /trunk/libs/spirit/example/scheme/detail/scheme_function_composer_call.hpp)
==============================================================================
--- /trunk/libs/spirit/example/scheme/detail/scheme_function_composer_call.hpp (original)
+++ trunk/libs/spirit/example/scheme/detail/scheme_composite_call.hpp 2010-04-13 00:42:54 EDT (Tue, 13 Apr 2010)
@@ -17,7 +17,7 @@
#define BOOST_PP_ITERATION_PARAMS_1 \
(3, (3, BOOST_PP_DEC(SCHEME_COMPOSITE_LIMIT), \
- "libs/spirit/example/scheme/detail/scheme_function_composer_call.hpp"))
+ "libs/spirit/example/scheme/detail/scheme_composite_call.hpp"))
#include BOOST_PP_ITERATE()
#undef SCHEME_PUSH_ELEMENT
Modified: trunk/libs/spirit/example/scheme/detail/scheme_function_call.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/detail/scheme_function_call.hpp (original)
+++ trunk/libs/spirit/example/scheme/detail/scheme_function_call.hpp 2010-04-13 00:42:54 EDT (Tue, 13 Apr 2010)
@@ -13,7 +13,7 @@
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
-#define SCHEME_PUSH_ELEMENT(z, n, _) elements.push_back(_##n);
+#define SCHEME_PUSH_ELEMENT(z, n, _) elements[n] = _##n;
#define BOOST_PP_ITERATION_PARAMS_1 \
(3, (3, BOOST_PP_DEC(SCHEME_COMPOSITE_LIMIT), \
@@ -36,9 +36,9 @@
template <BOOST_PP_ENUM_PARAMS(N, typename A)>
utree operator()(BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& _)) const
{
- utree elements;
+ boost::array<utree, N> elements;
BOOST_PP_REPEAT(N, SCHEME_PUSH_ELEMENT, _);
- return f(elements);
+ return f(get_range(elements));
}
#undef N
Deleted: trunk/libs/spirit/example/scheme/detail/scheme_function_composer_call.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/detail/scheme_function_composer_call.hpp 2010-04-13 00:42:54 EDT (Tue, 13 Apr 2010)
+++ (empty file)
@@ -1,47 +0,0 @@
-/*=============================================================================
- 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)
-==============================================================================*/
-#ifndef BOOST_PP_IS_ITERATING
-#ifndef SCHEME_FUNCTION_COMPOSER_CALL_HPP
-#define SCHEME_FUNCTION_COMPOSER_CALL_HPP
-
-#include <boost/preprocessor/iterate.hpp>
-#include <boost/preprocessor/repetition/enum_params.hpp>
-#include <boost/preprocessor/repetition/enum_binary_params.hpp>
-#include <boost/preprocessor/repetition/repeat.hpp>
-
-#define SCHEME_PUSH_ELEMENT(z, n, _) elements.push_back(as_function(_##n));
-
-#define BOOST_PP_ITERATION_PARAMS_1 \
- (3, (3, BOOST_PP_DEC(SCHEME_COMPOSITE_LIMIT), \
- "libs/spirit/example/scheme/detail/scheme_function_composer_call.hpp"))
-#include BOOST_PP_ITERATE()
-
-#undef SCHEME_PUSH_ELEMENT
-
-#endif
-
-///////////////////////////////////////////////////////////////////////////////
-//
-// Preprocessor vertical repetition code
-//
-///////////////////////////////////////////////////////////////////////////////
-#else // defined(BOOST_PP_IS_ITERATING)
-
-#define N BOOST_PP_ITERATION()
-
- template <BOOST_PP_ENUM_PARAMS(N, typename A)>
- actor operator()(BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& _)) const
- {
- actor_list elements;
- BOOST_PP_REPEAT(N, SCHEME_PUSH_ELEMENT, _);
- return derived()(elements);
- }
-
-#undef N
-#endif // defined(BOOST_PP_IS_ITERATING)
-
-
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-13 00:42:54 EDT (Tue, 13 Apr 2010)
@@ -36,7 +36,7 @@
using base_type::operator();
actor operator()(actor_list const& elements) const
{
- return actor(lambda_function(f, elements));
+ return actor(apply_function(f, elements));
}
};
@@ -50,16 +50,17 @@
environment(environment* parent = 0)
: outer(parent) {}
- void define(std::string const& name, function_composer const& def)
+ template <typename FunctionCall>
+ void define(std::string const& name, FunctionCall const& f)
{
// $$$ use exceptions here? $$$
BOOST_ASSERT(definitions.find(name) == definitions.end());
- definitions[name] = def;
+ definitions[name] = function_call(f);
}
- function_composer* find(std::string const& name)
+ function_call* find(std::string const& name)
{
- std::map<std::string, function_composer>::iterator
+ std::map<std::string, function_call>::iterator
i = definitions.find(name);
if (i != definitions.end())
return &i->second;
@@ -73,7 +74,7 @@
private:
environment* outer;
- std::map<std::string, function_composer> definitions;
+ std::map<std::string, function_call> definitions;
};
actor compile(utree const& ast, environment& env, actor_list& fragments);
@@ -103,7 +104,7 @@
actor operator()(utf8_symbol_range const& str) const
{
std::string name(str.begin(), str.end());
- if (function_composer* mf = env.find(name))
+ if (function_call* mf = env.find(name))
{
actor_list flist;
return (*mf)(flist);
@@ -119,15 +120,11 @@
{
environment local_env(&this->env);
for (std::size_t i = 0; i < args.size(); ++i)
- {
- boost::function<actor(actor_list const&)>
- f = boost::bind(arg, i);
- local_env.define(args[i], f);
- }
+ local_env.define(args[i], boost::bind(arg, i));
fragments.push_back(actor());
actor& f = fragments.back();
- env.define(name, function_composer(external_function(f)));
+ env.define(name, external_function(f));
f = compile(body, local_env, fragments);
}
@@ -137,7 +134,7 @@
{
fragments.push_back(actor());
actor& f = fragments.back();
- env.define(name, function_composer(external_function(f)));
+ env.define(name, external_function(f));
f = compile(body, env, fragments);
}
@@ -170,7 +167,7 @@
return actor(val(utf8_symbol("<define " + fname + ">")));
}
- if (function_composer* mf = env.find(name))
+ if (function_call* mf = env.find(name))
{
actor_list flist;
Iterator i = range.begin(); ++i;
@@ -183,7 +180,8 @@
}
};
- actor compile(utree const& ast, environment& env, actor_list& fragments)
+ inline actor compile(
+ utree const& ast, environment& env, actor_list& fragments)
{
return utree::visit(ast, compiler(env, fragments));
}
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 00:42:54 EDT (Tue, 13 Apr 2010)
@@ -10,6 +10,8 @@
#include <list>
#include <boost/function.hpp>
#include <boost/foreach.hpp>
+#include <boost/array.hpp>
+#include <boost/scoped_array.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
#define SCHEME_COMPOSITE_LIMIT 10
@@ -23,6 +25,13 @@
///////////////////////////////////////////////////////////////////////////
// actor
///////////////////////////////////////////////////////////////////////////
+ struct actor;
+
+ typedef std::list<actor> actor_list;
+ typedef boost::iterator_range<utree const*> args_type;
+ typedef boost::function<utree(args_type args)> actor_function;
+ typedef boost::function<actor(actor_list const&)> delayed_function;
+
struct actor
{
typedef utree result_type;
@@ -30,7 +39,7 @@
actor()
: f() {}
- actor(boost::function<utree(utree const& args)> const& f)
+ actor(actor_function const& f)
: f(f) {}
bool empty() const
@@ -40,47 +49,43 @@
utree operator()() const
{
- return f(utree());
+ return f(args_type());
}
- utree operator()(utree const& x) const
+ utree operator()(args_type x) const
{
- if (x.which() == utree_type::list_type)
- {
- return f(x);
- }
- else
- {
- utree elements;
- elements.push_back(x);
- return f(elements);
- }
+ return f(x);
}
template <typename A0>
utree operator()(A0 const& _0) const
{
- utree elements;
- elements.push_back(_0);
- return f(elements);
+ boost::array<utree, 1> elements;
+ elements[0] = _0;
+ return f(get_range(elements));
}
template <typename A0, typename A1>
utree operator()(A0 const& _0, A1 const& _1) const
{
- utree elements;
- elements.push_back(_0);
- elements.push_back(_1);
- return f(elements);
+ boost::array<utree, 2> elements;
+ elements[0] = _0;
+ elements[1] = _1;
+ return f(get_range(elements));
}
// More operators
#include "detail/scheme_function_call.hpp"
- boost::function<utree(utree const& args)> f;
- };
+ actor_function f;
- typedef std::list<actor> actor_list;
+ template <std::size_t n>
+ static args_type
+ get_range(boost::array<utree, n> const& array)
+ {
+ return args_type(array.begin(), array.end());
+ }
+ };
///////////////////////////////////////////////////////////////////////////
// values
@@ -91,7 +96,7 @@
value(utree const& val) : val(val) {}
typedef utree result_type;
- utree operator()(utree const& /*args*/) const
+ utree operator()(args_type /*args*/) const
{
return utree(boost::ref(val));
}
@@ -117,7 +122,7 @@
argument(int n) : n(n) {}
typedef utree result_type;
- utree operator()(utree const& args) const
+ utree operator()(args_type args) const
{
return utree(boost::ref(args[n]));
}
@@ -187,7 +192,7 @@
}
// More operators
- #include "detail/scheme_function_composer_call.hpp"
+ #include "detail/scheme_composite_call.hpp"
Derived const& derived() const
{
@@ -196,23 +201,18 @@
};
///////////////////////////////////////////////////////////////////////////
- // function_composer
+ // function_call
///////////////////////////////////////////////////////////////////////////
- struct function_composer : composite<function_composer>
+ struct function_call : composite<function_call>
{
- boost::function<actor(actor_list const&)> f;
+ delayed_function f;
- function_composer()
- : f(f) {}
+ function_call()
+ : f() {}
- function_composer(boost::function<actor(actor_list const&)> const& f)
+ function_call(delayed_function const& f)
: f(f) {}
- bool empty() const
- {
- return f.empty();
- }
-
using base_type::operator();
actor operator()(actor_list const& elements) const
{
@@ -221,25 +221,36 @@
};
///////////////////////////////////////////////////////////////////////////
- // lambda
+ // function
///////////////////////////////////////////////////////////////////////////
- struct lambda_function
+ struct apply_function
{
actor_list elements;
// we must hold f by reference because functions can be recursive
boost::reference_wrapper<actor const> f;
- lambda_function(actor const& f, actor_list const& elements)
+
+ apply_function(actor const& f, actor_list const& elements)
: elements(elements), f(f) {}
typedef utree result_type;
- utree operator()(utree const& args) const
+ utree operator()(args_type args) const
{
- utree fargs;
- BOOST_FOREACH(actor const& element, elements)
+ if (!elements.empty())
+ {
+ boost::scoped_array<utree>
+ fargs(new utree[elements.size()]);
+ std::size_t i = 0;
+ BOOST_FOREACH(actor const& element, elements)
+ {
+ fargs[i++] = element(args);
+ }
+ utree const* fi = fargs.get();
+ return f.get()(args_type(fi, fi+elements.size()));
+ }
+ else
{
- fargs.push_back(element(args));
+ return f.get()();
}
- return f.get()(fargs);
}
};
@@ -247,10 +258,13 @@
{
actor f;
+ function() : f() {}
+ function(actor const& f) : f(f) {}
+
using base_type::operator();
actor operator()(actor_list const& elements) const
{
- return actor(lambda_function(f, elements));
+ return actor(apply_function(f, elements));
}
function& operator=(function const& other)
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 00:42:54 EDT (Tue, 13 Apr 2010)
@@ -24,7 +24,7 @@
: cond(cond), then(then), else_(else_) {}
typedef utree result_type;
- utree operator()(utree const& args) const
+ utree operator()(args_type args) const
{
return cond(args).as<bool>() ? then(args) : else_(args);
}
@@ -43,8 +43,7 @@
}
};
- function_composer const if_
- = function_composer(if_composer());
+ if_composer const if_ = if_composer();
///////////////////////////////////////////////////////////////////////////
// less_than_equal
@@ -57,7 +56,7 @@
: a(a), b(b) {}
typedef utree result_type;
- utree operator()(utree const& args) const
+ utree operator()(args_type args) const
{
return a(args) <= b(args);
}
@@ -75,10 +74,9 @@
}
};
- function_composer const less_than_equal
- = function_composer(less_than_equal_composer());
-
- function_composer const lte = less_than_equal; // synonym
+ less_than_equal_composer const less_than_equal
+ = less_than_equal_composer();
+ less_than_equal_composer const lte = less_than_equal; // synonym
///////////////////////////////////////////////////////////////////////////
// vararg_function
@@ -92,7 +90,7 @@
: elements(elements) {}
using composite<Derived>::operator();
- utree operator()(utree const& args) const
+ utree operator()(args_type args) const
{
actor_list::const_iterator i = elements.begin();
utree result = (*i++)(args);
@@ -125,17 +123,16 @@
}
};
- struct plus_composer
+ struct plus_composer : composite<plus_composer>
{
- typedef actor result_type;
+ using base_type::operator();
actor operator()(actor_list const& elements) const
{
return actor(plus_function(elements));
}
};
- function_composer const plus
- = function_composer(plus_composer());
+ plus_composer const plus = plus_composer();
///////////////////////////////////////////////////////////////////////////
// minus
@@ -151,17 +148,16 @@
}
};
- struct minus_composer
+ struct minus_composer : composite<minus_composer>
{
- typedef actor result_type;
+ using base_type::operator();
actor operator()(actor_list const& elements) const
{
return actor(minus_function(elements));
}
};
- function_composer const minus
- = function_composer(minus_composer());
+ minus_composer const minus = minus_composer();
///////////////////////////////////////////////////////////////////////////
// times
@@ -177,17 +173,16 @@
}
};
- struct times_composer
+ struct times_composer : composite<times_composer>
{
- typedef actor result_type;
+ using base_type::operator();
actor operator()(actor_list const& elements) const
{
return actor(times_function(elements));
}
};
- function_composer const times
- = function_composer(times_composer());
+ times_composer const times = times_composer();
}
#endif
Modified: trunk/libs/spirit/example/scheme/test/scheme.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/test/scheme.cpp (original)
+++ trunk/libs/spirit/example/scheme/test/scheme.cpp 2010-04-13 00:42:54 EDT (Tue, 13 Apr 2010)
@@ -9,6 +9,7 @@
#include "../input/sexpr.hpp"
#include "../input/parse_sexpr_impl.hpp"
#include "../scheme_compiler.hpp"
+#include "../utree_io.hpp"
#include <iostream>
#include <fstream>
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