Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61237 - trunk/libs/spirit/example/scheme
From: joel_at_[hidden]
Date: 2010-04-13 01:03:05


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

Log:
perfection! (almost)
Text files modified:
   trunk/libs/spirit/example/scheme/scheme_compiler.hpp | 66 ++++++++++++++++++++-------------------
   trunk/libs/spirit/example/scheme/scheme_interpreter.hpp | 27 +--------------
   2 files changed, 37 insertions(+), 56 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-13 01:03:05 EDT (Tue, 13 Apr 2010)
@@ -17,32 +17,10 @@
 namespace scheme
 {
 ///////////////////////////////////////////////////////////////////////////////
-// Utilities
+// The environment
 ///////////////////////////////////////////////////////////////////////////////
- inline std::string get_symbol(utree const& s)
- {
- utf8_symbol_range symbol = s.as<utf8_symbol_range>();
- return std::string(symbol.begin(), symbol.end());
- }
-
- struct external_function : composite<external_function>
- {
- // we must hold f by reference because functions can be recursive
- boost::reference_wrapper<actor const> f;
-
- external_function(actor const& f)
- : f(f) {}
+ typedef boost::function<actor(actor_list const&)> compiled_function;
 
- using base_type::operator();
- actor operator()(actor_list const& elements) const
- {
- return actor(apply_function(f, elements));
- }
- };
-
-///////////////////////////////////////////////////////////////////////////////
-// The compiler
-///////////////////////////////////////////////////////////////////////////////
     class environment
     {
     public:
@@ -50,17 +28,17 @@
         environment(environment* parent = 0)
           : outer(parent) {}
 
- template <typename FunctionCall>
- void define(std::string const& name, FunctionCall const& f)
+ template <typename Function>
+ void define(std::string const& name, Function const& f)
         {
             // $$$ use exceptions here? $$$
             BOOST_ASSERT(definitions.find(name) == definitions.end());
- definitions[name] = function_call(f);
+ definitions[name] = compiled_function(f);
         }
 
- function_call* find(std::string const& name)
+ compiled_function* find(std::string const& name)
         {
- std::map<std::string, function_call>::iterator
+ std::map<std::string, compiled_function>::iterator
                 i = definitions.find(name);
             if (i != definitions.end())
                 return &i->second;
@@ -74,11 +52,29 @@
     private:
 
         environment* outer;
- std::map<std::string, function_call> definitions;
+ std::map<std::string, compiled_function> definitions;
     };
 
     actor compile(utree const& ast, environment& env, actor_list& fragments);
 
+///////////////////////////////////////////////////////////////////////////////
+// The compiler
+///////////////////////////////////////////////////////////////////////////////
+ struct external_function : composite<external_function>
+ {
+ // we must hold f by reference because functions can be recursive
+ boost::reference_wrapper<actor const> f;
+
+ external_function(actor const& f)
+ : f(f) {}
+
+ using base_type::operator();
+ actor operator()(actor_list const& elements) const
+ {
+ return actor(lambda_function(f, elements));
+ }
+ };
+
     struct compiler
     {
         typedef actor result_type;
@@ -104,7 +100,7 @@
         actor operator()(utf8_symbol_range const& str) const
         {
             std::string name(str.begin(), str.end());
- if (function_call* mf = env.find(name))
+ if (compiled_function* mf = env.find(name))
             {
                 actor_list flist;
                 return (*mf)(flist);
@@ -167,7 +163,7 @@
                 return actor(val(utf8_symbol("<define " + fname + ">")));
             }
 
- if (function_call* mf = env.find(name))
+ if (compiled_function* mf = env.find(name))
             {
                 actor_list flist;
                 Iterator i = range.begin(); ++i;
@@ -178,6 +174,12 @@
 
             return actor(); // $$$ implement me $$$
         }
+
+ static std::string get_symbol(utree const& s)
+ {
+ utf8_symbol_range symbol = s.as<utf8_symbol_range>();
+ return std::string(symbol.begin(), symbol.end());
+ }
     };
 
     inline actor compile(

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:03:05 EDT (Tue, 13 Apr 2010)
@@ -30,7 +30,6 @@
     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
     {
@@ -201,35 +200,15 @@
     };
 
     ///////////////////////////////////////////////////////////////////////////
- // function_call
- ///////////////////////////////////////////////////////////////////////////
- struct function_call : composite<function_call>
- {
- delayed_function f;
-
- function_call()
- : f() {}
-
- function_call(delayed_function const& f)
- : f(f) {}
-
- using base_type::operator();
- actor operator()(actor_list const& elements) const
- {
- return f(elements);
- }
- };
-
- ///////////////////////////////////////////////////////////////////////////
     // function
     ///////////////////////////////////////////////////////////////////////////
- struct apply_function
+ struct lambda_function
     {
         actor_list elements;
         // we must hold f by reference because functions can be recursive
         boost::reference_wrapper<actor const> f;
 
- apply_function(actor const& f, actor_list const& elements)
+ lambda_function(actor const& f, actor_list const& elements)
           : elements(elements), f(f) {}
 
         typedef utree result_type;
@@ -264,7 +243,7 @@
         using base_type::operator();
         actor operator()(actor_list const& elements) const
         {
- return actor(apply_function(f, elements));
+ return actor(lambda_function(f, elements));
         }
 
         function& operator=(function const& other)


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