|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r61282 - in trunk/libs/spirit/example/scheme: . detail test
From: joel_at_[hidden]
Date: 2010-04-14 21:40:43
Author: djowel
Date: 2010-04-14 21:40:42 EDT (Wed, 14 Apr 2010)
New Revision: 61282
URL: http://svn.boost.org/trac/boost/changeset/61282
Log:
more updates
Added:
trunk/libs/spirit/example/scheme/test/factorial.scm (contents, props changed)
trunk/libs/spirit/example/scheme/test/scheme_test1.cpp
- copied, changed from r61262, /trunk/libs/spirit/example/scheme/test/scheme.cpp
trunk/libs/spirit/example/scheme/test/scheme_test1.scm
- copied unchanged from r61262, /trunk/libs/spirit/example/scheme/test/scheme_test.scm
trunk/libs/spirit/example/scheme/test/scheme_test2.cpp (contents, props changed)
trunk/libs/spirit/example/scheme/test/scheme_test3.cpp (contents, props changed)
Removed:
trunk/libs/spirit/example/scheme/test/scheme.cpp
trunk/libs/spirit/example/scheme/test/scheme_test.scm
Text files modified:
trunk/libs/spirit/example/scheme/detail/scheme_composite_call.hpp | 2
trunk/libs/spirit/example/scheme/detail/scheme_function_call.hpp | 2
trunk/libs/spirit/example/scheme/scheme_compiler.hpp | 53 +++++-----
trunk/libs/spirit/example/scheme/scheme_interpreter.hpp | 208 +++++++++++++++++++++------------------
trunk/libs/spirit/example/scheme/scheme_intrinsics.hpp | 27 ++--
trunk/libs/spirit/example/scheme/test/scheme_test1.cpp | 25 +++-
6 files changed, 170 insertions(+), 147 deletions(-)
Modified: trunk/libs/spirit/example/scheme/detail/scheme_composite_call.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/detail/scheme_composite_call.hpp (original)
+++ trunk/libs/spirit/example/scheme/detail/scheme_composite_call.hpp 2010-04-14 21:40:42 EDT (Wed, 14 Apr 2010)
@@ -34,7 +34,7 @@
#define N BOOST_PP_ITERATION()
template <BOOST_PP_ENUM_PARAMS(N, typename A)>
- actor operator()(BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& _)) const
+ function operator()(BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& _)) const
{
actor_list elements;
BOOST_PP_REPEAT(N, 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-14 21:40:42 EDT (Wed, 14 Apr 2010)
@@ -38,7 +38,7 @@
{
boost::array<utree, N> elements;
BOOST_PP_REPEAT(N, SCHEME_PUSH_ELEMENT, _);
- return f(get_range(elements));
+ return derived()(get_range(elements));
}
#undef N
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-14 21:40:42 EDT (Wed, 14 Apr 2010)
@@ -19,7 +19,7 @@
///////////////////////////////////////////////////////////////////////////////
// The environment
///////////////////////////////////////////////////////////////////////////////
- typedef boost::function<actor(actor_list const&)> compiled_function;
+ typedef boost::function<function(actor_list const&)> compiled_function;
class environment
{
@@ -58,27 +58,27 @@
///////////////////////////////////////////////////////////////////////////////
// The compiler
///////////////////////////////////////////////////////////////////////////////
- actor compile(
+ function compile(
utree const& ast, environment& env, actor_list& fragments);
struct external_function : composite<external_function>
{
// we must hold f by reference because functions can be recursive
- boost::reference_wrapper<actor const> f;
+ boost::reference_wrapper<function const> f;
- external_function(actor const& f)
+ external_function(function const& f)
: f(f) {}
using base_type::operator();
- actor operator()(actor_list const& elements) const
+ function operator()(actor_list const& elements) const
{
- return actor(lambda_function(f, elements));
+ return function(lambda_function(f, elements));
}
};
struct compiler
{
- typedef actor result_type;
+ typedef function result_type;
environment& env;
actor_list& fragments;
@@ -87,18 +87,18 @@
{
}
- actor operator()(nil) const
+ function operator()(nil) const
{
return scheme::val(utree());
}
template <typename T>
- actor operator()(T const& val) const
+ function operator()(T const& val) const
{
return scheme::val(utree(val));
}
- actor operator()(utf8_symbol_range const& str) const
+ function operator()(utf8_symbol_range const& str) const
{
std::string name(str.begin(), str.end());
if (compiled_function* mf = env.find(name))
@@ -108,10 +108,10 @@
}
// $$$ throw? $$$
BOOST_ASSERT(false);
- return actor();
+ return function();
}
- actor make_lambda(
+ function make_lambda(
std::vector<std::string> const& args,
utree const& body) const
{
@@ -121,19 +121,20 @@
return compile(body, local_env, fragments);
}
- void define_function(
+ function define_function(
std::string const& name,
std::vector<std::string> const& args,
utree const& body) const
{
- fragments.push_back(actor());
- actor& f = fragments.back();
+ fragments.push_back(function());
+ function& f = fragments.back();
env.define(name, external_function(f));
f = make_lambda(args, body);
+ return f;
}
template <typename Iterator>
- actor operator()(boost::iterator_range<Iterator> const& range) const
+ function operator()(boost::iterator_range<Iterator> const& range) const
{
std::string name(get_symbol(*range.begin()));
@@ -158,8 +159,7 @@
fname = get_symbol(*i++);
}
- define_function(fname, args, *i);
- return actor(val(utf8_symbol("<define " + fname + ">")));
+ return define_function(fname, args, *i);
}
if (name == "lambda")
@@ -184,7 +184,7 @@
}
BOOST_ASSERT(false);
- return actor(); // $$$ implement me $$$
+ return function(); // $$$ implement me $$$
}
static std::string get_symbol(utree const& s)
@@ -194,7 +194,7 @@
}
};
- inline actor compile(
+ inline function compile(
utree const& ast, environment& env, actor_list& fragments)
{
return utree::visit(ast, compiler(env, fragments));
@@ -208,7 +208,7 @@
{
BOOST_FOREACH(utree const& program, ast)
{
- scheme::actor f = compile(program, env, fragments);
+ scheme::function f = compile(program, env, fragments);
results.push_back(f);
}
}
@@ -226,10 +226,10 @@
///////////////////////////////////////////////////////////////////////////
// interpreter
///////////////////////////////////////////////////////////////////////////
- template <typename Source>
- struct interpreter : composite<interpreter<Source> >
+ struct interpreter : actor<interpreter>
{
- interpreter(Source const& in, environment* outer = 0)
+ template <typename Source>
+ interpreter(Source& in, environment* outer = 0)
{
if (outer == 0)
build_basic_environment(env);
@@ -245,10 +245,9 @@
}
}
- using composite<interpreter<Source> >::operator();
- actor operator()(actor_list const& elements) const
+ utree eval(args_type args) const
{
- return actor(lambda_function(flist.back(), elements));
+ return flist.back()(args);
}
environment env;
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-14 21:40:42 EDT (Wed, 14 Apr 2010)
@@ -23,40 +23,31 @@
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
- // actor
+ // typedefs
///////////////////////////////////////////////////////////////////////////
- struct actor;
+ struct function;
- typedef std::list<actor> actor_list;
+ typedef std::list<function> actor_list;
typedef boost::iterator_range<utree const*> args_type;
typedef boost::function<utree(args_type args)> actor_function;
+ ///////////////////////////////////////////////////////////////////////////
+ // actor
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename Derived>
struct actor
{
typedef utree result_type;
+ typedef actor<Derived> base_type;
- actor()
- : f() {}
-
- actor(actor_function const& f)
- : f(f)
- {
- BOOST_ASSERT(!f.empty());
- }
-
- bool empty() const
+ utree operator()(args_type args) const
{
- return f.empty();
+ return derived().eval(args);
}
utree operator()() const
{
- return f(args_type());
- }
-
- utree operator()(args_type x) const
- {
- return f(x);
+ return derived().eval(args_type());
}
template <typename A0>
@@ -64,7 +55,7 @@
{
boost::array<utree, 1> elements;
elements[0] = _0;
- return f(get_range(elements));
+ return derived().eval(get_range(elements));
}
template <typename A0, typename A1>
@@ -73,32 +64,60 @@
boost::array<utree, 2> elements;
elements[0] = _0;
elements[1] = _1;
- return f(get_range(elements));
+ return derived().eval(get_range(elements));
}
// More operators
#include "detail/scheme_function_call.hpp"
- actor_function f;
-
template <std::size_t n>
static args_type
get_range(boost::array<utree, n> const& array)
{
return args_type(array.begin(), array.end());
}
+
+ Derived const& derived() const
+ {
+ return *static_cast<Derived const*>(this);
+ }
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ // function
+ ///////////////////////////////////////////////////////////////////////////
+ struct function : actor<function>
+ {
+ actor_function f;
+ function()
+ : f() {}
+
+ function(actor_function const& f)
+ : f(f)
+ {
+ BOOST_ASSERT(!f.empty());
+ }
+
+ bool empty() const
+ {
+ return f.empty();
+ }
+
+ utree eval(args_type args) const
+ {
+ return f(args);
+ }
};
///////////////////////////////////////////////////////////////////////////
// values
///////////////////////////////////////////////////////////////////////////
- struct value_function
+ struct value_function : actor<value_function>
{
utree val;
value_function(utree const& val) : val(val) {}
- typedef utree result_type;
- utree operator()(args_type /*args*/) const
+ utree eval(args_type /*args*/) const
{
return utree(boost::ref(val));
}
@@ -106,10 +125,10 @@
struct value
{
- typedef actor result_type;
- actor operator()(utree const& val) const
+ typedef function result_type;
+ function operator()(utree const& val) const
{
- return actor(value_function(val));
+ return function(value_function(val));
}
};
@@ -118,13 +137,12 @@
///////////////////////////////////////////////////////////////////////////
// arguments
///////////////////////////////////////////////////////////////////////////
- struct argument_function
+ struct argument_function : actor<argument_function>
{
std::size_t n;
argument_function(std::size_t n) : n(n) {}
- typedef utree result_type;
- utree operator()(args_type args) const
+ utree eval(args_type args) const
{
return utree(boost::ref(args[n]));
}
@@ -132,24 +150,24 @@
struct argument
{
- typedef actor result_type;
- actor operator()(std::size_t n) const
+ typedef function result_type;
+ function operator()(std::size_t n) const
{
- return actor(argument_function(n));
+ return function(argument_function(n));
}
};
argument const arg = {};
- actor const _1 = arg(0);
- actor const _2 = arg(1);
- actor const _3 = arg(2);
- actor const _4 = arg(3);
- actor const _5 = arg(4);
- actor const _6 = arg(5);
- actor const _7 = arg(6);
- actor const _8 = arg(7);
- actor const _9 = arg(8);
- actor const _10 = arg(10);
+ function const _1 = arg(0);
+ function const _2 = arg(1);
+ function const _3 = arg(2);
+ function const _4 = arg(3);
+ function const _5 = arg(4);
+ function const _6 = arg(5);
+ function const _7 = arg(6);
+ function const _8 = arg(7);
+ function const _9 = arg(8);
+ function const _10 = arg(10);
///////////////////////////////////////////////////////////////////////////
// composite
@@ -157,29 +175,29 @@
template <typename Derived>
struct composite
{
- typedef actor result_type;
+ typedef function result_type;
typedef composite<Derived> base_type;
- actor operator()(actor_list const& elements) const
+ function operator()(actor_list const& elements) const
{
- return derived()(elements);
+ return derived().compose(elements);
}
template <typename A0>
- actor operator()(A0 const& _0) const
+ function operator()(A0 const& _0) const
{
actor_list elements;
elements.push_back(as_function(_0));
- return derived()(elements);
+ return derived().compose(elements);
}
template <typename A0, typename A1>
- actor operator()(A0 const& _0, A1 const& _1) const
+ function operator()(A0 const& _0, A1 const& _1) const
{
actor_list elements;
elements.push_back(as_function(_0));
elements.push_back(as_function(_1));
- return derived()(elements);
+ return derived().compose(elements);
}
// More operators
@@ -191,12 +209,12 @@
}
template <typename T>
- static actor as_function(T const& val)
+ static function as_function(T const& val)
{
return scheme::val(utree(val));
}
- static actor const& as_function(actor const& f)
+ static function const& as_function(function const& f)
{
return f;
}
@@ -206,19 +224,18 @@
// unary_function
///////////////////////////////////////////////////////////////////////////
template <typename Derived>
- struct unary_function
+ struct unary_function : actor<unary_function<Derived> >
{
- actor a;
+ function a;
typedef unary_function<Derived> base_type;
- unary_function(actor const& a)
+ unary_function(function const& a)
: a(a)
{
BOOST_ASSERT(!a.empty());
}
- typedef utree result_type;
- utree operator()(args_type args) const
+ utree eval(args_type args) const
{
return derived().eval(a(args));
}
@@ -232,10 +249,9 @@
template <typename Function>
struct unary_composite : composite<unary_composite<Function> >
{
- using composite<unary_composite<Function> >::operator();
- actor operator()(actor_list const& elements) const
+ function compose(actor_list const& elements) const
{
- return actor(Function(elements.front()));
+ return function(Function(elements.front()));
}
};
@@ -243,21 +259,20 @@
// binary_function
///////////////////////////////////////////////////////////////////////////
template <typename Derived>
- struct binary_function
+ struct binary_function : actor<binary_function<Derived> >
{
- actor a;
- actor b;
+ function a;
+ function b;
typedef binary_function<Derived> base_type;
- binary_function(actor const& a, actor const& b)
+ binary_function(function const& a, function const& b)
: a(a), b(b)
{
BOOST_ASSERT(!a.empty());
BOOST_ASSERT(!b.empty());
}
- typedef utree result_type;
- utree operator()(args_type args) const
+ utree eval(args_type args) const
{
return derived().eval(a(args), b(args));
}
@@ -271,13 +286,12 @@
template <typename Function>
struct binary_composite : composite<binary_composite<Function> >
{
- using composite<binary_composite<Function> >::operator();
- actor operator()(actor_list const& elements) const
+ function compose(actor_list const& elements) const
{
actor_list::const_iterator i = elements.begin();
- actor a = *i++;
- actor b = *i;
- return actor(Function(a, b));
+ function a = *i++;
+ function b = *i;
+ return function(Function(a, b));
}
};
@@ -285,27 +299,27 @@
// nary_function
///////////////////////////////////////////////////////////////////////////
template <typename Derived>
- struct nary_function : composite<Derived>
+ struct nary_function : actor<nary_function<Derived> >
{
- typedef nary_function<Derived> base_type;
actor_list elements;
+ typedef nary_function<Derived> base_type;
+
nary_function(actor_list const& elements)
: elements(elements)
{
- BOOST_FOREACH(actor const& element, elements)
+ BOOST_FOREACH(function const& element, elements)
{
BOOST_ASSERT(!element.empty());
}
}
- using composite<Derived>::operator();
- utree operator()(args_type args) const
+ utree eval(args_type 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)
+ BOOST_FOREACH(function const& element, rest)
{
if (!derived().eval(result, element(args)))
break; // allow short-circuit evaluation
@@ -322,34 +336,33 @@
template <typename Function>
struct nary_composite : composite<nary_composite<Function> >
{
- using composite<nary_composite<Function> >::operator();
- actor operator()(actor_list const& elements) const
+ function compose(actor_list const& elements) const
{
- return actor(Function(elements));
+ return function(Function(elements));
}
};
///////////////////////////////////////////////////////////////////////////
- // function
+ // lambda
///////////////////////////////////////////////////////////////////////////
- struct lambda_function
+ struct lambda_function : actor<lambda_function>
{
actor_list elements;
// we must hold f by reference because functions can be recursive
- boost::reference_wrapper<actor const> f;
+ boost::reference_wrapper<function const> f;
- lambda_function(actor const& f, actor_list const& elements)
+ lambda_function(function const& f, actor_list const& elements)
: elements(elements), f(f) {}
typedef utree result_type;
- utree operator()(args_type args) const
+ utree eval(args_type args) const
{
if (!elements.empty())
{
boost::scoped_array<utree>
fargs(new utree[elements.size()]);
std::size_t i = 0;
- BOOST_FOREACH(actor const& element, elements)
+ BOOST_FOREACH(function const& element, elements)
{
fargs[i++] = element(args);
}
@@ -363,26 +376,25 @@
}
};
- struct function : composite<function>
+ struct lambda : composite<lambda>
{
- actor f;
+ function f;
- function() : f() {}
- function(actor const& f) : f(f) {}
+ lambda() : f() {}
+ lambda(function const& f) : f(f) {}
- using base_type::operator();
- actor operator()(actor_list const& elements) const
+ function compose(actor_list const& elements) const
{
- return actor(lambda_function(f, elements));
+ return function(lambda_function(f, elements));
}
- function& operator=(function const& other)
+ lambda& operator=(lambda const& other)
{
f = other.f;
return *this;
}
- function& operator=(actor const& f_)
+ lambda& operator=(function const& f_)
{
f = f_;
return *this;
Modified: trunk/libs/spirit/example/scheme/scheme_intrinsics.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/scheme_intrinsics.hpp (original)
+++ trunk/libs/spirit/example/scheme/scheme_intrinsics.hpp 2010-04-14 21:40:42 EDT (Wed, 14 Apr 2010)
@@ -14,13 +14,13 @@
///////////////////////////////////////////////////////////////////////////
// if
///////////////////////////////////////////////////////////////////////////
- struct if_function
+ struct if_function : actor<if_function>
{
- actor cond;
- actor then;
- actor else_;
+ function cond;
+ function then;
+ function else_;
if_function(
- actor const& cond, actor const& then, actor const& else_)
+ function const& cond, function const& then, function const& else_)
: cond(cond), then(then), else_(else_)
{
BOOST_ASSERT(!cond.empty());
@@ -29,7 +29,7 @@
}
typedef utree result_type;
- utree operator()(args_type args) const
+ utree eval(args_type args) const
{
return cond(args).as<bool>() ? then(args) : else_(args);
}
@@ -37,14 +37,13 @@
struct if_composite : composite<if_composite>
{
- using base_type::operator();
- actor operator()(actor_list const& elements) const
+ function compose(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 if_ = *i++;
+ function then = *i++;
+ function else_ = *i;
+ return function(if_function(if_, then, else_));
}
};
@@ -56,7 +55,7 @@
struct less_than_function
: binary_function<less_than_function>
{
- less_than_function(actor const& a, actor const& b)
+ less_than_function(function const& a, function const& b)
: base_type(a, b) {}
typedef utree result_type;
@@ -79,7 +78,7 @@
struct less_than_equal_function
: binary_function<less_than_equal_function>
{
- less_than_equal_function(actor const& a, actor const& b)
+ less_than_equal_function(function const& a, function const& b)
: base_type(a, b) {}
typedef utree result_type;
Added: trunk/libs/spirit/example/scheme/test/factorial.scm
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/scheme/test/factorial.scm 2010-04-14 21:40:42 EDT (Wed, 14 Apr 2010)
@@ -0,0 +1,4 @@
+; The hello-world for interpreters ;-)
+(define (factorial n)
+ (if (<= n 0) 1
+ (* n (factorial (- n 1)))))
Deleted: trunk/libs/spirit/example/scheme/test/scheme.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/test/scheme.cpp 2010-04-14 21:40:42 EDT (Wed, 14 Apr 2010)
+++ (empty file)
@@ -1,106 +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)
-=============================================================================*/
-#include <boost/config/warning_disable.hpp>
-
-#include "../input/sexpr.hpp"
-#include "../input/parse_sexpr_impl.hpp"
-#include "../scheme_compiler.hpp"
-#include "../utree_io.hpp"
-#include <iostream>
-#include <fstream>
-
-inline std::ostream& println(std::ostream& out, scheme::utree const& val)
-{
- out << val << std::endl;
- return out;
-}
-
-///////////////////////////////////////////////////////////////////////////////
-// Main program
-///////////////////////////////////////////////////////////////////////////////
-int main(int argc, char **argv)
-{
- { // testing the c++ side
-
- using scheme::if_;
- using scheme::plus;
- using scheme::times;
- using scheme::minus;
- using scheme::lte;
- using scheme::_1;
- using scheme::_2;
- using scheme::function;
-
- std::cout << "result: " << plus(11, 22, 33) () << std::endl;
- std::cout << "result: " << plus(11, 22, _1) (33) << std::endl;
- std::cout << "result: " << plus(11, _1, _2) (22, 33) << std::endl;
- std::cout << "result: " << plus(11, plus(_1, _2)) (22, 33) << std::endl;
-
- function factorial;
- factorial = if_(lte(_1, 0), 1, times(_1, factorial(minus(_1, 1))));
-
- std::cout << "result: " << factorial(_1) (10) << std::endl;
- }
-
- char const* filename = NULL;
- if (argc > 1)
- {
- filename = argv[1];
- }
- else
- {
- std::cerr << "Error: No input file provided." << std::endl;
- return 1;
- }
-
- std::ifstream in(filename, std::ios_base::in);
-
- if (!in)
- {
- std::cerr << "Error: Could not open input file: "
- << filename << std::endl;
- return 1;
- }
-
- // Ignore the BOM marking the beginning of a UTF-8 file in Windows
- char c = in.peek();
- if (c == '\xef')
- {
- char s[3];
- in >> s[0] >> s[1] >> s[2];
- s[3] = '\0';
- if (s != std::string("\xef\xbb\xbf"))
- {
- std::cerr << "Error: Unexpected characters from input file: "
- << filename << std::endl;
- return 1;
- }
- }
-
- scheme::utree program;
- if (scheme::input::parse_sexpr_list(in, program))
- {
- std::cout << "success: " << std::endl;
- scheme::environment env;
- scheme::build_basic_environment(env);
- scheme::actor_list fragments;
- scheme::actor_list flist;
- compile_all(program, env, flist, fragments);
- BOOST_FOREACH(scheme::actor const& f, flist)
- {
- std::cout << "result: " << f() << std::endl;
- }
- }
- else
- {
- std::cout << "parse error" << std::endl;
- }
-
- return 0;
-}
-
-
Deleted: trunk/libs/spirit/example/scheme/test/scheme_test.scm
==============================================================================
--- trunk/libs/spirit/example/scheme/test/scheme_test.scm 2010-04-14 21:40:42 EDT (Wed, 14 Apr 2010)
+++ (empty file)
@@ -1,24 +0,0 @@
-; These tests demostrate the functionality of the scheme
-; compiler/interpreter
-
-(define (dbl x) (+ x x))
-
-(define len 123)
-
-(dbl len) ; 246
-
-; The hello-world for interpreters ;-)
-(define (factorial n)
- (if (<= n 0) 1
- (* n (factorial (- n 1)))))
-
-(factorial 10) ; 3628800
-
-; Fibonacci using lambda
-(define fib
- (lambda (n)
- (if (< n 2)
- n
- (+ (fib (- n 1)) (fib (- n 2))))))
-
-(fib 10) ; 55
\ No newline at end of file
Copied: trunk/libs/spirit/example/scheme/test/scheme_test1.cpp (from r61262, /trunk/libs/spirit/example/scheme/test/scheme.cpp)
==============================================================================
--- /trunk/libs/spirit/example/scheme/test/scheme.cpp (original)
+++ trunk/libs/spirit/example/scheme/test/scheme_test1.cpp 2010-04-14 21:40:42 EDT (Wed, 14 Apr 2010)
@@ -33,14 +33,14 @@
using scheme::lte;
using scheme::_1;
using scheme::_2;
- using scheme::function;
+ using scheme::lambda;
std::cout << "result: " << plus(11, 22, 33) () << std::endl;
std::cout << "result: " << plus(11, 22, _1) (33) << std::endl;
std::cout << "result: " << plus(11, _1, _2) (22, 33) << std::endl;
std::cout << "result: " << plus(11, plus(_1, _2)) (22, 33) << std::endl;
- function factorial;
+ lambda factorial;
factorial = if_(lte(_1, 0), 1, times(_1, factorial(minus(_1, 1))));
std::cout << "result: " << factorial(_1) (10) << std::endl;
@@ -90,10 +90,23 @@
scheme::actor_list fragments;
scheme::actor_list flist;
compile_all(program, env, flist, fragments);
- BOOST_FOREACH(scheme::actor const& f, flist)
- {
- std::cout << "result: " << f() << std::endl;
- }
+
+ scheme::actor_list::iterator i = flist.begin();
+
+ std::cout << "the 1st is the define dbl:" << std::endl;
+ std::cout << "(dbl 555): " << (*i++)(555) << std::endl;
+ std::cout << "the 2nd is the define len:" << std::endl;
+ std::cout << "len: " << (*i++)() << std::endl;
+ std::cout << "the 3rd is a function call:" << std::endl;
+ std::cout << "(dbl len): " << (*i++)() << std::endl;
+ std::cout << "the 4th is the define factorial:" << std::endl;
+ std::cout << "(factorial 5): " << (*i++)(5) << std::endl;
+ std::cout << "the 5th is a function call:" << std::endl;
+ std::cout << "(factorial 10): " << (*i++)() << std::endl;
+ std::cout << "the 6th is the define fib:" << std::endl;
+ std::cout << "(fib 5): " << (*i++)(5) << std::endl;
+ std::cout << "the 7th is a function call:" << std::endl;
+ std::cout << "(fib 10): " << (*i++)() << std::endl;
}
else
{
Added: trunk/libs/spirit/example/scheme/test/scheme_test2.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/scheme/test/scheme_test2.cpp 2010-04-14 21:40:42 EDT (Wed, 14 Apr 2010)
@@ -0,0 +1,55 @@
+/*=============================================================================
+ 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/config/warning_disable.hpp>
+
+#include "../input/sexpr.hpp"
+#include "../input/parse_sexpr_impl.hpp"
+#include "../scheme_compiler.hpp"
+#include "../utree_io.hpp"
+#include <iostream>
+#include <fstream>
+
+///////////////////////////////////////////////////////////////////////////////
+// Main program
+///////////////////////////////////////////////////////////////////////////////
+int main()
+{
+ char const* filename = "factorial.scm";
+ std::ifstream in(filename, std::ios_base::in);
+
+ if (!in)
+ {
+ std::cerr << "Error: Could not open input file: "
+ << filename << std::endl;
+ return 1;
+ }
+
+ // Ignore the BOM marking the beginning of a UTF-8 file in Windows
+ char c = in.peek();
+ if (c == '\xef')
+ {
+ char s[3];
+ in >> s[0] >> s[1] >> s[2];
+ s[3] = '\0';
+ if (s != std::string("\xef\xbb\xbf"))
+ {
+ std::cerr << "Error: Unexpected characters from input file: "
+ << filename << std::endl;
+ return 1;
+ }
+ }
+
+ using scheme::interpreter;
+ using scheme::_1;
+
+ scheme::interpreter factorial(in);
+ std::cout << factorial(10) << std::endl;
+
+ return 0;
+}
+
+
Added: trunk/libs/spirit/example/scheme/test/scheme_test3.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/scheme/test/scheme_test3.cpp 2010-04-14 21:40:42 EDT (Wed, 14 Apr 2010)
@@ -0,0 +1,30 @@
+/*=============================================================================
+ 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/config/warning_disable.hpp>
+
+#include "../input/sexpr.hpp"
+#include "../input/parse_sexpr_impl.hpp"
+#include "../scheme_compiler.hpp"
+#include "../utree_io.hpp"
+
+///////////////////////////////////////////////////////////////////////////////
+// Main program
+///////////////////////////////////////////////////////////////////////////////
+int main()
+{
+ using scheme::interpreter;
+ using scheme::_1;
+ using scheme::utree;
+
+ utree src = "(define (factorial n) (if (<= n 0) 1 (* n (factorial (- n 1)))))";
+ scheme::interpreter factorial(src);
+ std::cout << factorial(10) << std::endl;
+
+ return 0;
+}
+
+
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