Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61503 - in trunk/libs/spirit/example/scheme: scheme test test/scheme
From: joel_at_[hidden]
Date: 2010-04-23 01:26:16


Author: djowel
Date: 2010-04-23 01:26:14 EDT (Fri, 23 Apr 2010)
New Revision: 61503
URL: http://svn.boost.org/trac/boost/changeset/61503

Log:
block scopes working well.
Removed:
   trunk/libs/spirit/example/scheme/test/scheme/factorial.scm
   trunk/libs/spirit/example/scheme/test/scheme/scheme_test1.scm
Text files modified:
   trunk/libs/spirit/example/scheme/scheme/compiler.hpp | 16 +++++
   trunk/libs/spirit/example/scheme/scheme/interpreter.hpp | 111 ++++++++++++++++++++++++++++++---------
   trunk/libs/spirit/example/scheme/test/Jamfile | 2
   trunk/libs/spirit/example/scheme/test/scheme/scheme_test1.cpp | 38 -------------
   4 files changed, 101 insertions(+), 66 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-23 01:26:14 EDT (Fri, 23 Apr 2010)
@@ -107,6 +107,15 @@
         }
     };
 
+ struct no_body : scheme_exception
+ {
+ ~no_body() throw() {}
+ virtual const char* what() const throw()
+ {
+ return "scheme: No expression in body.";
+ }
+ };
+
 ///////////////////////////////////////////////////////////////////////////////
 // The environment
 ///////////////////////////////////////////////////////////////////////////////
@@ -252,6 +261,9 @@
             }
 
             actor_list flist;
+ if (body.size() == 0)
+ throw no_body();
+
             BOOST_FOREACH(utree const& item, body)
             {
                 function f = compile(item, local_env, fragments, line, source_file);
@@ -286,10 +298,10 @@
                 fragments.push_back(function());
                 function& f = fragments.back();
                 env.define(name, external_function(f, env.level()), args.size(), fixed_arity);
- f = make_lambda(args, fixed_arity, body)(); // unprotect (evaluate returns a function)
+ f = make_lambda(args, fixed_arity, body)(); // unprotect (eval returns a function)
                 return f;
             }
- catch (compilation_error const&)
+ catch (std::exception const&)
             {
                 env.undefine(name);
                 throw;

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 01:26:14 EDT (Fri, 23 Apr 2010)
@@ -146,7 +146,8 @@
     ///////////////////////////////////////////////////////////////////////////
     // arguments
     ///////////////////////////////////////////////////////////////////////////
- struct argument_function : actor<argument_function>
+ template <bool scoped = true>
+ struct argument_function : actor<argument_function<scoped> >
     {
         std::size_t n;
         std::size_t level;
@@ -169,32 +170,59 @@
         }
     };
 
+ template <>
+ struct argument_function<false> : actor<argument_function<false> >
+ {
+ std::size_t n;
+ argument_function(std::size_t n, std::size_t level = 0)
+ : n(n)
+ {}
+
+ utree eval(scope const& env) const
+ {
+ scope const* eptr = &env;
+ utree const& arg = (*eptr)[n];
+ if (arg.which() != utree_type::function_type)
+ return utree(boost::ref(arg));
+ else
+ return arg.eval(*eptr);
+ }
+ };
+
+ template <bool scoped = true>
     struct argument
     {
         typedef function result_type;
         function operator()(std::size_t n, std::size_t level = 0) const
         {
- return function(argument_function(n, level));
+ return function(argument_function<scoped>(n, level));
         }
     };
 
- argument const arg = {};
- 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);
+ // scoped arg
+ argument<true> const arg = {};
+
+ // unscoped arg
+ argument<false> const unscoped_arg = {};
+
+ // unscoped args
+ function const _1 = unscoped_arg(0);
+ function const _2 = unscoped_arg(1);
+ function const _3 = unscoped_arg(2);
+ function const _4 = unscoped_arg(3);
+ function const _5 = unscoped_arg(4);
+ function const _6 = unscoped_arg(5);
+ function const _7 = unscoped_arg(6);
+ function const _8 = unscoped_arg(7);
+ function const _9 = unscoped_arg(8);
+ function const _10 = unscoped_arg(10);
 
     ///////////////////////////////////////////////////////////////////////////
     // variable arguments.
     // Collects the arguments from n to last in a utree list.
     ///////////////////////////////////////////////////////////////////////////
- struct vararg_function : actor<vararg_function>
+ template <bool scoped = true>
+ struct vararg_function : actor<vararg_function<scoped> >
     {
         std::size_t level;
         std::size_t n;
@@ -222,26 +250,57 @@
         }
     };
 
+ template <>
+ struct vararg_function<false> : actor<vararg_function<false> >
+ {
+ std::size_t n;
+ vararg_function(std::size_t n, std::size_t level = 0)
+ : n(n)
+ {}
+
+ utree eval(scope const& env) const
+ {
+ scope const* eptr = &env;
+ utree result;
+ for (std::size_t i = n; i < eptr->size(); ++i)
+ {
+ utree const& arg = (*eptr)[i];
+ if (arg.which() != utree_type::function_type)
+ result.push_back(utree(boost::ref(arg)));
+ else
+ result.push_back(arg.eval(*eptr));
+ }
+ return result;
+ }
+ };
+
+ template <bool scoped = true>
     struct vararg
     {
         typedef function result_type;
         function operator()(std::size_t n, std::size_t level = 0) const
         {
- return function(vararg_function(n, level));
+ return function(vararg_function<scoped>(n, level));
         }
     };
 
- vararg const varg = {};
- function const _1_ = varg(0);
- function const _2_ = varg(1);
- function const _3_ = varg(2);
- function const _4_ = varg(3);
- function const _5_ = varg(4);
- function const _6_ = varg(5);
- function const _7_ = varg(6);
- function const _8_ = varg(7);
- function const _9_ = varg(8);
- function const _10_ = varg(10);
+ // scoped varg
+ vararg<true> const varg = {};
+
+ // unscoped arg
+ vararg<false> const unscoped_varg = {};
+
+ // unscoped vargs
+ function const _1_ = unscoped_varg(0);
+ function const _2_ = unscoped_varg(1);
+ function const _3_ = unscoped_varg(2);
+ function const _4_ = unscoped_varg(3);
+ function const _5_ = unscoped_varg(4);
+ function const _6_ = unscoped_varg(5);
+ function const _7_ = unscoped_varg(6);
+ function const _8_ = unscoped_varg(7);
+ function const _9_ = unscoped_varg(8);
+ function const _10_ = unscoped_varg(10);
 
     ///////////////////////////////////////////////////////////////////////////
     // composite

Modified: trunk/libs/spirit/example/scheme/test/Jamfile
==============================================================================
--- trunk/libs/spirit/example/scheme/test/Jamfile (original)
+++ trunk/libs/spirit/example/scheme/test/Jamfile 2010-04-23 01:26:14 EDT (Fri, 23 Apr 2010)
@@ -22,7 +22,7 @@
 
     # run utree tests
     [ run utree/utree_test.cpp : : : : ]
- #[ run scheme/scheme_test1.cpp : scheme/scheme_test1.scm : : : ]
+ [ run scheme/scheme_test1.cpp : : : : ]
     [ run scheme/scheme_test2.cpp : scheme/scheme_test.scm test1 test2 test3 test4 : : : ]
     [ run scheme/scheme_test3.cpp : : : : ]
 

Deleted: trunk/libs/spirit/example/scheme/test/scheme/factorial.scm
==============================================================================
--- trunk/libs/spirit/example/scheme/test/scheme/factorial.scm 2010-04-23 01:26:14 EDT (Fri, 23 Apr 2010)
+++ (empty file)
@@ -1,7 +0,0 @@
-; The hello-world for interpreters ;-)
-(define (factorial n)
- (if (<= n 0) 1
- (* n (factorial (- n 1)))))
-
-(define (test1)
- (= (factorial 10) 3628800))
\ No newline at end of file

Modified: trunk/libs/spirit/example/scheme/test/scheme/scheme_test1.cpp
==============================================================================
--- trunk/libs/spirit/example/scheme/test/scheme/scheme_test1.cpp (original)
+++ trunk/libs/spirit/example/scheme/test/scheme/scheme_test1.cpp 2010-04-23 01:26:14 EDT (Fri, 23 Apr 2010)
@@ -16,7 +16,7 @@
 ///////////////////////////////////////////////////////////////////////////////
 // Main program
 ///////////////////////////////////////////////////////////////////////////////
-int main(int argc, char **argv)
+int main()
 {
     using scheme::utree;
 
@@ -42,42 +42,6 @@
         BOOST_TEST(factorial(_1) (10) == utree(3628800));
     }
 
- BOOST_TEST(argc > 1);
- char const* filename = filename = argv[1];
- std::ifstream in(filename, std::ios_base::in);
-
- BOOST_TEST(in);
-
- // 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';
- BOOST_TEST(s != std::string("\xef\xbb\xbf"));
- }
-
- //~ scheme::utree program;
- //~ BOOST_TEST(scheme::input::parse_sexpr_list(in, program, filename));
-
- //~ scheme::environment env;
- //~ scheme::build_basic_environment(env);
- //~ scheme::actor_list fragments;
- //~ scheme::actor_list flist;
- //~ compile_all(program, env, flist, fragments, filename);
-
- //~ scheme::actor_list::iterator i = flist.begin();
-
- //~ BOOST_TEST((*i++)(555) == 1110);
- //~ BOOST_TEST((*i++)() == 123);
- //~ BOOST_TEST((*i++)() == 246);
- //~ BOOST_TEST((*i++)(5) == 120);
- //~ BOOST_TEST((*i++)() == 3628800);
- //~ BOOST_TEST((*i++)(5) == 5);
- //~ BOOST_TEST((*i++)() == 55);
- //~ BOOST_TEST((*i++)() == 21);
-
     return boost::report_errors();
 }
 

Deleted: trunk/libs/spirit/example/scheme/test/scheme/scheme_test1.scm
==============================================================================
--- trunk/libs/spirit/example/scheme/test/scheme/scheme_test1.scm 2010-04-23 01:26:14 EDT (Fri, 23 Apr 2010)
+++ (empty file)
@@ -1,26 +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
-
-(+ 1 2 3 4 5 6) ; 21 varargs
\ No newline at end of file


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