|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r61223 - in trunk/libs/spirit/example/scheme: . test
From: joel_at_[hidden]
Date: 2010-04-12 06:12:28
Author: djowel
Date: 2010-04-12 06:12:27 EDT (Mon, 12 Apr 2010)
New Revision: 61223
URL: http://svn.boost.org/trac/boost/changeset/61223
Log:
refactorings
Text files modified:
trunk/libs/spirit/example/scheme/scheme_compiler.hpp | 24 +---
trunk/libs/spirit/example/scheme/scheme_interpreter.hpp | 175 ----------------------------------------
trunk/libs/spirit/example/scheme/test/scheme.cpp | 1
3 files changed, 8 insertions(+), 192 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-12 06:12:27 EDT (Mon, 12 Apr 2010)
@@ -11,6 +11,9 @@
#include <map>
#include <boost/bind.hpp>
+#include "scheme_intinsics.hpp"
+#include "scheme_interpreter.hpp"
+
namespace scheme
{
///////////////////////////////////////////////////////////////////////////////
@@ -34,19 +37,11 @@
void define(std::string const& name, function_composer const& def)
{
- // $$$ use exceptions here $$$
+ // $$$ use exceptions here? $$$
BOOST_ASSERT(definitions.find(name) == definitions.end());
definitions[name] = def;
}
- //~ function_composer&
- //~ forward_declare(std::string const& name)
- //~ {
- //~ // $$$ use exceptions here $$$
- //~ BOOST_ASSERT(definitions.find(name) == definitions.end());
- //~ return definitions[name];
- //~ }
-
function_composer* find(std::string const& name)
{
std::map<std::string, function_composer>::iterator
@@ -119,10 +114,6 @@
actor& f = fragments.back();
env.define(name, lambda(f, args.size()));
f = compile(body, local_env, fragments);
-
-
- //~ function_composer& fc = env.forward_declare(name);
- //~ fc = lambda(compile(body, local_env), args.size());
}
void define_nullary_function(
@@ -139,6 +130,7 @@
actor operator()(boost::iterator_range<Iterator> const& range) const
{
std::string name(get_symbol(*range.begin()));
+ std::string fname;
if (name == "define")
{
@@ -148,7 +140,7 @@
// a function
utree const& decl = *i++;
Iterator di = decl.begin();
- std::string fname(get_symbol(*di++));
+ fname = get_symbol(*di++);
std::vector<std::string> args;
while (di != decl.end())
args.push_back(get_symbol(*di++));
@@ -157,10 +149,10 @@
else
{
// constants (nullary functions)
- std::string fname(get_symbol(*i++));
+ fname = get_symbol(*i++);
define_nullary_function(fname, *i);
}
- return actor(val(utf8_symbol("<function>")));
+ return actor(val(utf8_symbol("<define " + fname + ">")));
}
if (function_composer* mf = env.find(name))
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-12 06:12:27 EDT (Mon, 12 Apr 2010)
@@ -202,181 +202,6 @@
};
///////////////////////////////////////////////////////////////////////////
- // if
- ///////////////////////////////////////////////////////////////////////////
- struct if_function
- {
- actor cond;
- actor then;
- actor else_;
- if_function(
- actor const& cond, actor const& then, actor const& else_)
- : cond(cond), then(then), else_(else_) {}
-
- typedef utree result_type;
- utree operator()(utree const& args) const
- {
- return cond(args).as<bool>() ? then(args) : else_(args);
- }
- };
-
- struct if_composer
- {
- typedef actor result_type;
- actor operator()(actor_list const& elements) const
- {
- actor_list::const_iterator i = elements.begin();
- actor if_ = *i++;
- actor then = *i++;
- actor else_ = *i;
- return actor(if_function(if_, then, else_));
- }
- };
-
- function_composer const if_
- = function_composer(if_composer());
-
- ///////////////////////////////////////////////////////////////////////////
- // less_than_equal
- ///////////////////////////////////////////////////////////////////////////
- struct less_than_equal_function
- {
- actor a;
- actor b;
- less_than_equal_function(actor const& a, actor const& b)
- : a(a), b(b) {}
-
- typedef utree result_type;
- utree operator()(utree const& args) const
- {
- return a(args) <= b(args);
- }
- };
-
- struct less_than_equal_composer
- {
- typedef actor result_type;
- 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));
- }
- };
-
- function_composer const less_than_equal
- = function_composer(less_than_equal_composer());
-
- ///////////////////////////////////////////////////////////////////////////
- // plus
- ///////////////////////////////////////////////////////////////////////////
- struct plus_function
- {
- actor_list elements;
- plus_function(actor_list const& elements)
- : elements(elements) {}
-
- typedef utree result_type;
- utree operator()(utree const& 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)
- {
- result = result + element(args);
- }
- return result;
- }
- };
-
- struct plus_composer
- {
- typedef actor result_type;
- actor operator()(actor_list const& elements) const
- {
- return actor(plus_function(elements));
- }
- };
-
- function_composer const plus
- = function_composer(plus_composer());
-
- ///////////////////////////////////////////////////////////////////////////
- // minus
- ///////////////////////////////////////////////////////////////////////////
- struct minus_function
- {
- actor_list elements;
- minus_function(actor_list const& elements)
- : elements(elements) {}
-
- typedef utree result_type;
- utree operator()(utree const& 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)
- {
- result = result - element(args);
- }
- return result;
- }
- };
-
- struct minus_composer
- {
- typedef actor result_type;
- actor operator()(actor_list const& elements) const
- {
- return actor(minus_function(elements));
- }
- };
-
- function_composer const minus
- = function_composer(minus_composer());
-
- ///////////////////////////////////////////////////////////////////////////
- // times
- ///////////////////////////////////////////////////////////////////////////
- struct times_function
- {
- actor_list elements;
- times_function(actor_list const& elements)
- : elements(elements) {}
-
- typedef utree result_type;
- utree operator()(utree const& 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)
- {
- result = result * element(args);
- }
- return result;
- }
- };
-
- struct times_composer
- {
- typedef actor result_type;
- actor operator()(actor_list const& elements) const
- {
- return actor(times_function(elements));
- }
- };
-
- function_composer const times
- = function_composer(times_composer());
-
- ///////////////////////////////////////////////////////////////////////////
// lambda
///////////////////////////////////////////////////////////////////////////
struct lambda_function
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-12 06:12:27 EDT (Mon, 12 Apr 2010)
@@ -8,7 +8,6 @@
#include "../input/sexpr.hpp"
#include "../input/parse_sexpr_impl.hpp"
-#include "../scheme_interpreter.hpp"
#include "../scheme_compiler.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