Major updates to the C++ API. Please see trunk/src/example.cpp to follow along

This commit is contained in:
Jason Turner
2009-07-18 18:05:54 +00:00
parent ac817ff33a
commit 4d4c26bf73
14 changed files with 451 additions and 416 deletions

View File

@@ -20,7 +20,7 @@
#include "dispatchkit/dispatchkit.hpp" #include "dispatchkit/dispatchkit.hpp"
#include "dispatchkit/bootstrap.hpp" #include "dispatchkit/bootstrap.hpp"
#include "dispatchkit/bootstrap_stl.hpp" #include "dispatchkit/bootstrap_stl.hpp"
#include "dispatchkit/function_call.hpp"
namespace chaiscript namespace chaiscript
{ {
/** /**
@@ -113,10 +113,10 @@ namespace chaiscript
* Special type for returned values * Special type for returned values
*/ */
struct Return_Value { struct Return_Value {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
TokenPtr location; TokenPtr location;
Return_Value(const dispatchkit::Boxed_Value &return_value, const TokenPtr where) : retval(return_value), location(where) { } Return_Value(const Boxed_Value &return_value, const TokenPtr where) : retval(return_value), location(where) { }
}; };
/** /**

View File

@@ -10,7 +10,7 @@
#include "dispatchkit.hpp" #include "dispatchkit.hpp"
#include "register_function.hpp" #include "register_function.hpp"
namespace dispatchkit namespace chaiscript
{ {
/** /**
* Set of helper functions for common operators * Set of helper functions for common operators
@@ -215,7 +215,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_oper_equals(Dispatch_Engine &s) void add_oper_equals(Dispatch_Engine &s)
{ {
register_function(s, &equals<const T&, const T&>, "="); s.add(fun(&equals<const T&, const T&>), "=");
} }
/** /**
@@ -224,7 +224,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_oper_add(Dispatch_Engine &s) void add_oper_add(Dispatch_Engine &s)
{ {
register_function(s, &add<T, const T&, const T&>, "+"); s.add(fun(&add<T, const T&, const T&>), "+");
} }
/** /**
@@ -233,7 +233,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_oper_add_equals(Dispatch_Engine &s) void add_oper_add_equals(Dispatch_Engine &s)
{ {
register_function(s, &addsequal<T, T>, "+="); s.add(fun(&addsequal<T, T>), "+=");
} }
/** /**
@@ -242,7 +242,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_oper_subtract(Dispatch_Engine &s) void add_oper_subtract(Dispatch_Engine &s)
{ {
register_function(s, &subtract<T, const T&, const T&>, "-"); s.add(fun(&subtract<T, const T&, const T&>), "-");
} }
/** /**
@@ -251,7 +251,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_oper_divide(Dispatch_Engine &s) void add_oper_divide(Dispatch_Engine &s)
{ {
register_function(s, &divide<T, const T&, const T&>, "/"); s.add(fun(&divide<T, const T&, const T&>), "/");
} }
/** /**
@@ -260,7 +260,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_oper_multiply(Dispatch_Engine &s) void add_oper_multiply(Dispatch_Engine &s)
{ {
register_function(s, &multiply<T, const T&, const T&>, "*"); s.add(fun(&multiply<T, const T&, const T&>), "*");
} }
/** /**
@@ -269,7 +269,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_oper_not_equals(Dispatch_Engine &s) void add_oper_not_equals(Dispatch_Engine &s)
{ {
register_function(s, &not_equals<const T&, const T&>, "!="); s.add(fun(&not_equals<const T&, const T&>), "!=");
} }
/** /**
@@ -278,7 +278,7 @@ namespace dispatchkit
template<typename T, typename U> template<typename T, typename U>
void add_oper_assign_overload(Dispatch_Engine &s) void add_oper_assign_overload(Dispatch_Engine &s)
{ {
register_function(s, &assign<T,U>, "="); s.add(fun(&assign<T,U>), "=");
} }
@@ -288,7 +288,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_oper_assign(Dispatch_Engine &s) void add_oper_assign(Dispatch_Engine &s)
{ {
register_function(s, &assign<T,T>, "="); s.add(fun(&assign<T,T>), "=");
} }
@@ -298,7 +298,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_oper_assign_pod(Dispatch_Engine &s) void add_oper_assign_pod(Dispatch_Engine &s)
{ {
register_function(s, &assign_pod<T>, "="); s.add(fun(&assign_pod<T>), "=");
} }
@@ -308,7 +308,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_oper_less_than(Dispatch_Engine &s) void add_oper_less_than(Dispatch_Engine &s)
{ {
register_function(s, &less_than<const T&, const T&>, "<"); s.add(fun(&less_than<const T&, const T&>), "<");
} }
/** /**
@@ -317,7 +317,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_oper_greater_than(Dispatch_Engine &s) void add_oper_greater_than(Dispatch_Engine &s)
{ {
register_function(s, &greater_than<const T&, const T&>, ">"); s.add(fun(&greater_than<const T&, const T&>), ">");
} }
/** /**
@@ -326,7 +326,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_oper_less_than_equals(Dispatch_Engine &s) void add_oper_less_than_equals(Dispatch_Engine &s)
{ {
register_function(s, &less_than_equals<const T&, const T&>, "<="); s.add(fun(&less_than_equals<const T&, const T&>), "<=");
} }
/** /**
@@ -335,7 +335,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_oper_greater_than_equals(Dispatch_Engine &s) void add_oper_greater_than_equals(Dispatch_Engine &s)
{ {
register_function(s, &greater_than_equals<const T&, const T&>, ">="); s.add(fun(&greater_than_equals<const T&, const T&>), ">=");
} }
/** /**
@@ -345,12 +345,12 @@ namespace dispatchkit
template<typename T, typename R> template<typename T, typename R>
void add_opers_comparison_overload(Dispatch_Engine &s) void add_opers_comparison_overload(Dispatch_Engine &s)
{ {
register_function(s, &equals<const T&, const R&>, "=="); s.add(fun(&equals<const T&, const R&>), "==");
register_function(s, &not_equals<const T&, const R&>, "!="); s.add(fun(&not_equals<const T&, const R&>), "!=");
register_function(s, &less_than<const T&, const R&>, "<"); s.add(fun(&less_than<const T&, const R&>), "<");
register_function(s, &greater_than<const T&, const R&>, ">"); s.add(fun(&greater_than<const T&, const R&>), ">");
register_function(s, &less_than_equals<const T&, const R&>, "<="); s.add(fun(&less_than_equals<const T&, const R&>), "<=");
register_function(s, &greater_than_equals<const T&, const R&>, ">="); s.add(fun(&greater_than_equals<const T&, const R&>), ">=");
} }
/** /**
@@ -372,18 +372,18 @@ namespace dispatchkit
template<typename Ret, typename T, typename R> template<typename Ret, typename T, typename R>
void add_opers_arithmetic_overload(Dispatch_Engine &s) void add_opers_arithmetic_overload(Dispatch_Engine &s)
{ {
register_function(s, &add<Ret, T, R>, "+"); s.add(fun(&add<Ret, T, R>), "+");
register_function(s, &subtract<Ret, T, R>, "-"); s.add(fun(&subtract<Ret, T, R>), "-");
register_function(s, &divide<Ret, T, R>, "/"); s.add(fun(&divide<Ret, T, R>), "/");
register_function(s, &multiply<Ret, T, R>, "*"); s.add(fun(&multiply<Ret, T, R>), "*");
register_function(s, &timesequal<T, R>, "*="); s.add(fun(&timesequal<T, R>), "*=");
register_function(s, &dividesequal<T, R>, "/="); s.add(fun(&dividesequal<T, R>), "/=");
register_function(s, &subtractsequal<T, R>, "-="); s.add(fun(&subtractsequal<T, R>), "-=");
register_function(s, &addsequal<T, R>, "+="); s.add(fun(&addsequal<T, R>), "+=");
register_function(s, &prefixincrement<T>, "++"); s.add(fun(&prefixincrement<T>), "++");
register_function(s, &prefixdecrement<T>, "--"); s.add(fun(&prefixdecrement<T>), "--");
register_function(s, &prefixnegate<T>, "-"); s.add(fun(&prefixnegate<T>), "-");
register_function(s, &prefixnot<T>, "!"); s.add(fun(&prefixnot<T>), "!");
} }
/** /**
@@ -393,10 +393,10 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_opers_arithmetic_modify_pod(Dispatch_Engine &s) void add_opers_arithmetic_modify_pod(Dispatch_Engine &s)
{ {
register_function(s, &timesequal_pod<T>, "*="); s.add(fun(&timesequal_pod<T>), "*=");
register_function(s, &dividesequal_pod<T>, "/="); s.add(fun(&dividesequal_pod<T>), "/=");
register_function(s, &subtractsequal_pod<T>, "-="); s.add(fun(&subtractsequal_pod<T>), "-=");
register_function(s, &addsequal_pod<T>, "+="); s.add(fun(&addsequal_pod<T>), "+=");
} }
/** /**
@@ -407,8 +407,8 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_copy_constructor(Dispatch_Engine &s, const std::string &type) void add_copy_constructor(Dispatch_Engine &s, const std::string &type)
{ {
s.register_function(build_constructor<T, const T &>(), type); s.add(constructor<T (const T &)>(), type);
s.register_function(build_constructor<T, const T &>(), "clone"); s.add(constructor<T (const T &)>(), "clone");
} }
/** /**
@@ -417,7 +417,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_basic_constructors(Dispatch_Engine &s, const std::string &type) void add_basic_constructors(Dispatch_Engine &s, const std::string &type)
{ {
s.register_function(build_constructor<T>(), type); s.add(constructor<T ()>(), type);
add_copy_constructor<T>(s, type); add_copy_constructor<T>(s, type);
} }
@@ -427,7 +427,7 @@ namespace dispatchkit
template<typename T> template<typename T>
void add_construct_pod(Dispatch_Engine &s, const std::string &type) void add_construct_pod(Dispatch_Engine &s, const std::string &type)
{ {
register_function(s, &construct_pod<T>, type); s.add(fun(&construct_pod<T>), type);
} }
/** /**
@@ -437,7 +437,7 @@ namespace dispatchkit
template<typename T, typename U> template<typename T, typename U>
void add_constructor_overload(Dispatch_Engine &s, const std::string &type) void add_constructor_overload(Dispatch_Engine &s, const std::string &type)
{ {
s.register_function(build_constructor<T, const U &>(), type); s.add(constructor<T (const U &)>(), type);
} }
/** /**
@@ -489,22 +489,22 @@ namespace dispatchkit
template<typename T> template<typename T>
void bootstrap_pod_type(Dispatch_Engine &s, const std::string &name) void bootstrap_pod_type(Dispatch_Engine &s, const std::string &name)
{ {
s.register_type<T>(name); s.add(type_<T>(), name);
add_basic_constructors<T>(s, name); add_basic_constructors<T>(s, name);
add_oper_assign<T>(s); add_oper_assign<T>(s);
add_oper_assign_pod<T>(s); add_oper_assign_pod<T>(s);
add_construct_pod<T>(s, name); add_construct_pod<T>(s, name);
add_opers_arithmetic<T>(s); add_opers_arithmetic<T>(s);
add_opers_arithmetic_modify_pod<T>(s); add_opers_arithmetic_modify_pod<T>(s);
register_function(s, &to_string<T>, "to_string"); s.add(fun(&to_string<T>), "to_string");
register_function(s, &parse_string<T>, "to_" + name); s.add(fun(&parse_string<T>), "to_" + name);
} }
/** /**
* "clone" function for a shared_ptr type. This is used in the case * "clone" function for a shared_ptr type. This is used in the case
* where you do not want to make a deep copy of an object during cloning * where you do not want to make a deep copy of an object during cloning
* but want to instead maintain the shared_ptr. It is needed internally * but want to instead maintain the shared_ptr. It is needed internally
* for handling of boost::shared_ptr<Proxy_Function> object (that is, * for handling of Proxy_Function object (that is,
* function variables. * function variables.
*/ */
template<typename Type> template<typename Type>
@@ -560,12 +560,12 @@ namespace dispatchkit
*/ */
static void add_opers_comparison_pod(Dispatch_Engine &s) static void add_opers_comparison_pod(Dispatch_Engine &s)
{ {
register_function(s, &equals<Boxed_POD_Value, Boxed_POD_Value>, "=="); s.add(fun(&equals<Boxed_POD_Value, Boxed_POD_Value>), "==");
register_function(s, &not_equals<Boxed_POD_Value, Boxed_POD_Value>, "!="); s.add(fun(&not_equals<Boxed_POD_Value, Boxed_POD_Value>), "!=");
register_function(s, &less_than<Boxed_POD_Value, Boxed_POD_Value>, "<"); s.add(fun(&less_than<Boxed_POD_Value, Boxed_POD_Value>), "<");
register_function(s, &greater_than<Boxed_POD_Value, Boxed_POD_Value>, ">"); s.add(fun(&greater_than<Boxed_POD_Value, Boxed_POD_Value>), ">");
register_function(s, &less_than_equals<Boxed_POD_Value, Boxed_POD_Value>, "<="); s.add(fun(&less_than_equals<Boxed_POD_Value, Boxed_POD_Value>), "<=");
register_function(s, &greater_than_equals<Boxed_POD_Value, Boxed_POD_Value>, ">="); s.add(fun(&greater_than_equals<Boxed_POD_Value, Boxed_POD_Value>), ">=");
} }
/** /**
@@ -573,10 +573,10 @@ namespace dispatchkit
*/ */
static void add_opers_arithmetic_pod(Dispatch_Engine &s) static void add_opers_arithmetic_pod(Dispatch_Engine &s)
{ {
register_function(s, &add<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>, "+"); s.add(fun(&add<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "+");
register_function(s, &subtract<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>, "-"); s.add(fun(&subtract<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "-");
register_function(s, &divide<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>, "/"); s.add(fun(&divide<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "/");
register_function(s, &multiply<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>, "*"); s.add(fun(&multiply<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "*");
} }
/** /**
@@ -611,9 +611,9 @@ namespace dispatchkit
throw arity_error(params.size(), 2); throw arity_error(params.size(), 2);
} }
boost::shared_ptr<Proxy_Function> f = boxed_cast<boost::shared_ptr<Proxy_Function> >(params[0]); Proxy_Function f = boxed_cast<Proxy_Function >(params[0]);
return Boxed_Value(boost::shared_ptr<Proxy_Function>(new Bound_Function(f, return Boxed_Value(Proxy_Function(new Bound_Function(f,
std::vector<Boxed_Value>(params.begin() + 1, params.end())))); std::vector<Boxed_Value>(params.begin() + 1, params.end()))));
} }
@@ -628,7 +628,7 @@ namespace dispatchkit
throw arity_error(params.size(), 1); throw arity_error(params.size(), 1);
} }
boost::shared_ptr<Proxy_Function> f = boxed_cast<boost::shared_ptr<Proxy_Function> >(params[0]); Proxy_Function f = boxed_cast<Proxy_Function >(params[0]);
return Boxed_Value(f->types_match(std::vector<Boxed_Value>(params.begin() + 1, params.end()))); return Boxed_Value(f->types_match(std::vector<Boxed_Value>(params.begin() + 1, params.end())));
} }
@@ -638,19 +638,19 @@ namespace dispatchkit
*/ */
static void bootstrap(Dispatch_Engine &s) static void bootstrap(Dispatch_Engine &s)
{ {
s.register_type<void>("void"); s.add(type_<void>(), "void");
s.register_type<bool>("bool"); s.add(type_<bool>(), "bool");
s.register_type<Boxed_Value>("Object"); s.add(type_<Boxed_Value>(), "Object");
s.register_type<Boxed_POD_Value>("PODObject"); s.add(type_<Boxed_POD_Value>(), "PODObject");
s.register_type<Proxy_Function>("function"); s.add(type_<Proxy_Function>(), "function");
add_basic_constructors<bool>(s, "bool"); add_basic_constructors<bool>(s, "bool");
add_oper_assign<std::string>(s); add_oper_assign<std::string>(s);
add_oper_assign<bool>(s); add_oper_assign<bool>(s);
register_function(s, &to_string<const std::string &>, "internal_to_string"); s.add(fun(&to_string<const std::string &>), "internal_to_string");
register_function(s, &to_string<bool>, "internal_to_string"); s.add(fun(&to_string<bool>), "internal_to_string");
register_function(s, &unknown_assign, "="); s.add(fun(&unknown_assign), "=");
bootstrap_pod_type<double>(s, "double"); bootstrap_pod_type<double>(s, "double");
bootstrap_pod_type<int>(s, "int"); bootstrap_pod_type<int>(s, "int");
@@ -660,26 +660,26 @@ namespace dispatchkit
add_opers_comparison_pod(s); add_opers_comparison_pod(s);
add_opers_arithmetic_pod(s); add_opers_arithmetic_pod(s);
register_function(s, &modulus<int, int, int>, "%"); s.add(fun(&modulus<int, int, int>), "%");
register_function(s, &print, "print_string"); s.add(fun(&print), "print_string");
register_function(s, &println, "println_string"); s.add(fun(&println), "println_string");
s.register_function(boost::function<void ()>(boost::bind(&dump_system, boost::ref(s))), "dump_system"); s.add(fun(boost::function<void ()>(boost::bind(&dump_system, boost::ref(s)))), "dump_system");
s.register_function(boost::function<void (Boxed_Value)>(boost::bind(&dump_object, _1, boost::ref(s))), "dump_object"); s.add(fun(boost::function<void (Boxed_Value)>(boost::bind(&dump_object, _1, boost::ref(s)))), "dump_object");
s.register_function(boost::function<bool (Boxed_Value, const std::string &)>(boost::bind(&is_type, boost::ref(s), _2, _1)), s.add(fun(boost::function<bool (Boxed_Value, const std::string &)>(boost::bind(&is_type, boost::ref(s), _2, _1))),
"is_type"); "is_type");
s.register_function(boost::shared_ptr<Proxy_Function>(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))), s.add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))),
"bind"); "bind");
register_function(s, &shared_ptr_clone<Proxy_Function>, "clone"); s.add(fun(&shared_ptr_clone<Proxy_Function_Base>), "clone");
register_function(s, &ptr_assign<Proxy_Function>, "="); s.add(fun(&ptr_assign<Proxy_Function_Base>), "=");
s.register_function(boost::shared_ptr<Proxy_Function>(new Dynamic_Proxy_Function(boost::bind(&call_exists, _1))), s.add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&call_exists, _1))),
"call_exists"); "call_exists");
register_function(s, &type_match, "type_match"); s.add(fun(&type_match), "type_match");
} }
}; };
} }

View File

@@ -17,7 +17,7 @@
#include "register_function.hpp" #include "register_function.hpp"
namespace dispatchkit namespace chaiscript
{ {
/** /**
* Input_Range, based on the D concept of ranges. * Input_Range, based on the D concept of ranges.
@@ -76,24 +76,22 @@ namespace dispatchkit
template<typename ContainerType> template<typename ContainerType>
void bootstrap_input_range(Dispatch_Engine &system, const std::string &type) void bootstrap_input_range(Dispatch_Engine &system, const std::string &type)
{ {
system.register_type<Input_Range<ContainerType> >(type+"_Range"); system.add(type_<Input_Range<ContainerType> >(), type + "_Range");
system.register_type<typename ContainerType::iterator>(type+"_Iterator"); system.add(type_<typename ContainerType::iterator>(), type+"_Iterator");
system.register_function(build_constructor<Input_Range<ContainerType>, ContainerType &>(), "range"); system.add(constructor<Input_Range<ContainerType> (ContainerType &)>(), "range");
system.register_function(build_constructor<Input_Range<ContainerType>, system.add(constructor<Input_Range<ContainerType> (typename ContainerType::iterator)>(), "range");
typename ContainerType::iterator>(), "range");
typedef std::pair<typename ContainerType::iterator, typename ContainerType::iterator> ItrPair; typedef std::pair<typename ContainerType::iterator, typename ContainerType::iterator> ItrPair;
system.register_function(build_constructor<Input_Range<ContainerType>, system.add(constructor<Input_Range<ContainerType> (const ItrPair &)>(), "range");
const ItrPair &>(), "range");
system.register_type<ItrPair>(type+"_Iterator_Pair");
system.add(type_<ItrPair>(), type+"_Iterator_Pair");
register_function(system, &Input_Range<ContainerType>::empty, "empty"); system.add(fun(&Input_Range<ContainerType>::empty), "empty");
register_function(system, &Input_Range<ContainerType>::pop_front, "pop_front"); system.add(fun(&Input_Range<ContainerType>::pop_front), "pop_front");
register_function(system, &Input_Range<ContainerType>::front, "front"); system.add(fun(&Input_Range<ContainerType>::front), "front");
system.register_function(build_constructor<Input_Range<ContainerType>, const Input_Range<ContainerType> &>(), "clone"); system.add(constructor<Input_Range<ContainerType> (const Input_Range<ContainerType> &)>(), "clone");
} }
/** /**
@@ -117,10 +115,10 @@ namespace dispatchkit
//In the interest of runtime safety for the system, we prefer the at() method for [] access, //In the interest of runtime safety for the system, we prefer the at() method for [] access,
//to throw an exception in an out of bounds condition. //to throw an exception in an out of bounds condition.
system.register_function( system.add(
boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::at)), "[]"); fun(boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::at))), "[]");
system.register_function( system.add(
boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::operator[])), "at"); fun(boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::operator[]))), "at");
} }
@@ -144,9 +142,9 @@ namespace dispatchkit
{ {
bootstrap_assignable<ContainerType>(system, type); bootstrap_assignable<ContainerType>(system, type);
register_function(system, &ContainerType::size, "size"); system.add(fun(&ContainerType::size), "size");
register_function(system, &ContainerType::max_size, "max_size"); system.add(fun(&ContainerType::max_size), "max_size");
register_function(system, &ContainerType::empty, "empty"); system.add(fun(&ContainerType::empty), "empty");
} }
/** /**
@@ -167,7 +165,7 @@ namespace dispatchkit
template<typename Type> template<typename Type>
void bootstrap_default_constructible(Dispatch_Engine &system, const std::string &type) void bootstrap_default_constructible(Dispatch_Engine &system, const std::string &type)
{ {
system.register_function(build_constructor<Type>(), type); system.add(constructor<Type ()>(), type);
} }
/** /**
@@ -224,8 +222,8 @@ namespace dispatchkit
insert_name = "insert_at"; insert_name = "insert_at";
} }
register_function(system, &insert_at<ContainerType>, insert_name); system.add(fun(&insert_at<ContainerType>), insert_name);
register_function(system, &erase_at<ContainerType>, "erase_at"); system.add(fun(&erase_at<ContainerType>), "erase_at");
} }
/** /**
@@ -240,7 +238,7 @@ namespace dispatchkit
typedef typename ContainerType::reference (ContainerType::*backptr)(); typedef typename ContainerType::reference (ContainerType::*backptr)();
register_function(system, (backptr(&ContainerType::back)), "back"); system.add(fun(backptr(&ContainerType::back)), "back");
std::string push_back_name; std::string push_back_name;
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value))
@@ -250,8 +248,8 @@ namespace dispatchkit
push_back_name = "push_back"; push_back_name = "push_back";
} }
register_function(system, &ContainerType::push_back, push_back_name); system.add(fun(&ContainerType::push_back), push_back_name);
register_function(system, &ContainerType::pop_back, "pop_back"); system.add(fun(&ContainerType::pop_back), "pop_back");
} }
/** /**
@@ -261,7 +259,7 @@ namespace dispatchkit
template<typename VectorType> template<typename VectorType>
void bootstrap_vector(Dispatch_Engine &system, const std::string &type) void bootstrap_vector(Dispatch_Engine &system, const std::string &type)
{ {
system.register_type<VectorType>(type); system.add(type_<VectorType>(), type);
bootstrap_random_access_container<VectorType>(system, type); bootstrap_random_access_container<VectorType>(system, type);
bootstrap_back_insertion_sequence<VectorType>(system, type); bootstrap_back_insertion_sequence<VectorType>(system, type);
} }
@@ -284,15 +282,15 @@ namespace dispatchkit
template<typename PairType> template<typename PairType>
void bootstrap_pair(Dispatch_Engine &system, const std::string &type) void bootstrap_pair(Dispatch_Engine &system, const std::string &type)
{ {
system.register_type<PairType>(type); system.add(type_<PairType>(), type);
register_member(system, &PairType::first, "first"); system.add(fun(&PairType::first), "first");
register_member(system, &PairType::second, "second"); system.add(fun(&PairType::second), "second");
system.register_function(build_constructor<PairType >(), type); system.add(constructor<PairType ()>(), type);
system.register_function(build_constructor<PairType, const PairType &>(), type); system.add(constructor<PairType (const PairType &)>(), type);
system.register_function(build_constructor<PairType, const PairType &>(), "clone"); system.add(constructor<PairType (const PairType &)>(), "clone");
system.register_function(build_constructor<PairType, const typename PairType::first_type &, const typename PairType::second_type &>(), type); system.add(constructor<PairType (const typename PairType::first_type &, const typename PairType::second_type &)>(), type);
} }
@@ -315,7 +313,7 @@ namespace dispatchkit
void bootstrap_unique_associative_container(Dispatch_Engine &system, const std::string &type) void bootstrap_unique_associative_container(Dispatch_Engine &system, const std::string &type)
{ {
bootstrap_associative_container<ContainerType>(system, type); bootstrap_associative_container<ContainerType>(system, type);
register_function(system, &ContainerType::count, "count"); system.add(fun(&ContainerType::count), "count");
} }
/** /**
@@ -330,7 +328,7 @@ namespace dispatchkit
bootstrap_reversible_container<ContainerType>(system, type); bootstrap_reversible_container<ContainerType>(system, type);
bootstrap_associative_container<ContainerType>(system, type); bootstrap_associative_container<ContainerType>(system, type);
register_function(system, eq_range(&ContainerType::equal_range), "equal_range"); system.add(fun(eq_range(&ContainerType::equal_range)), "equal_range");
} }
/** /**
@@ -351,8 +349,8 @@ namespace dispatchkit
template<typename MapType> template<typename MapType>
void bootstrap_map(Dispatch_Engine &system, const std::string &type) void bootstrap_map(Dispatch_Engine &system, const std::string &type)
{ {
system.register_type<MapType>(type); system.add(type_<MapType>(), type);
register_function(system, &MapType::operator[], "[]"); system.add(fun(&MapType::operator[]), "[]");
bootstrap_unique_sorted_associative_container<MapType>(system, type); bootstrap_unique_sorted_associative_container<MapType>(system, type);
bootstrap_pair_associative_container<MapType>(system, type); bootstrap_pair_associative_container<MapType>(system, type);
} }
@@ -364,19 +362,19 @@ namespace dispatchkit
template<typename String> template<typename String>
void bootstrap_string(Dispatch_Engine &system, const std::string &type) void bootstrap_string(Dispatch_Engine &system, const std::string &type)
{ {
system.register_type<String>(type); system.add(type_<String>(), type);
add_oper_add<String>(system); add_oper_add<String>(system);
add_oper_add_equals<String>(system); add_oper_add_equals<String>(system);
add_opers_comparison<String>(system); add_opers_comparison<String>(system);
bootstrap_random_access_container<String>(system, type); bootstrap_random_access_container<String>(system, type);
bootstrap_sequence<String>(system, type); bootstrap_sequence<String>(system, type);
typedef typename String::size_type (String::*find_func)(const String &, typename String::size_type) const; typedef typename String::size_type (String::*find_func)(const String &, typename String::size_type) const;
register_function(system, find_func(&String::find), "find"); system.add(fun(find_func(&String::find)), "find");
register_function(system, find_func(&String::rfind), "rfind"); system.add(fun(find_func(&String::rfind)), "rfind");
register_function(system, find_func(&String::find_first_of), "find_first_of"); system.add(fun(find_func(&String::find_first_of)), "find_first_of");
register_function(system, find_func(&String::find_last_of), "find_last_of"); system.add(fun(find_func(&String::find_last_of)), "find_last_of");
register_function(system, find_func(&String::find_first_not_of), "find_first_not_of"); system.add(fun(find_func(&String::find_first_not_of)), "find_first_not_of");
register_function(system, find_func(&String::find_last_not_of), "find_last_not_of"); system.add(fun(find_func(&String::find_last_not_of)), "find_last_not_of");
} }
} }

View File

@@ -17,7 +17,7 @@
#include <boost/cstdint.hpp> #include <boost/cstdint.hpp>
#include <boost/type_traits/add_const.hpp> #include <boost/type_traits/add_const.hpp>
namespace dispatchkit namespace chaiscript
{ {
/** /**
* Boxed_Value is the main tool of the dispatchkit. It allows * Boxed_Value is the main tool of the dispatchkit. It allows
@@ -148,6 +148,12 @@ namespace dispatchkit
return data; return data;
} }
template<typename T>
boost::shared_ptr<Data> get(T *t)
{
return get(boost::ref(*t));
}
template<typename T> template<typename T>
boost::shared_ptr<Data> get(boost::reference_wrapper<T> obj) boost::shared_ptr<Data> get(boost::reference_wrapper<T> obj)
{ {
@@ -670,6 +676,12 @@ namespace dispatchkit
return Boxed_POD_Value(ob); return Boxed_POD_Value(ob);
} }
}; };
template<typename T>
Boxed_Value var(T t)
{
return Boxed_Value(t);
}
} }
#endif #endif

View File

@@ -23,22 +23,22 @@
#include "proxy_functions.hpp" #include "proxy_functions.hpp"
#include "proxy_constructors.hpp" #include "proxy_constructors.hpp"
namespace dispatchkit namespace chaiscript
{ {
/** /**
* A Proxy_Function implementation that is able to take * A Proxy_Function implementation that is able to take
* a vector of Proxy_Functions and perform a dispatch on them. It is * a vector of Proxy_Functions and perform a dispatch on them. It is
* used specifically in the case of dealing with Function object variables * used specifically in the case of dealing with Function object variables
*/ */
class Dispatch_Function : public Proxy_Function class Dispatch_Function : public Proxy_Function_Base
{ {
public: public:
Dispatch_Function(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &t_funcs) Dispatch_Function(const std::vector<std::pair<std::string, Proxy_Function > > &t_funcs)
: m_funcs(t_funcs) : m_funcs(t_funcs)
{ {
} }
virtual bool operator==(const Proxy_Function &) const virtual bool operator==(const Proxy_Function_Base &) const
{ {
return false; return false;
} }
@@ -57,7 +57,7 @@ namespace dispatchkit
virtual bool types_match(const std::vector<Boxed_Value> &types) const virtual bool types_match(const std::vector<Boxed_Value> &types) const
{ {
typedef std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > function_vec; typedef std::vector<std::pair<std::string, Proxy_Function > > function_vec;
function_vec::const_iterator begin = m_funcs.begin(); function_vec::const_iterator begin = m_funcs.begin();
function_vec::const_iterator end = m_funcs.end(); function_vec::const_iterator end = m_funcs.end();
@@ -81,7 +81,7 @@ namespace dispatchkit
} }
private: private:
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > m_funcs; std::vector<std::pair<std::string, Proxy_Function > > m_funcs;
}; };
@@ -92,7 +92,7 @@ namespace dispatchkit
class Dispatch_Engine class Dispatch_Engine
{ {
public: public:
typedef std::map<std::string, dispatchkit::Type_Info> Type_Name_Map; typedef std::map<std::string, chaiscript::Type_Info> Type_Name_Map;
typedef std::map<std::string, Boxed_Value> Scope; typedef std::map<std::string, Boxed_Value> Scope;
typedef std::deque<Scope> Stack; typedef std::deque<Scope> Stack;
@@ -105,48 +105,37 @@ namespace dispatchkit
/** /**
* Add a new named Proxy_Function to the system * Add a new named Proxy_Function to the system
*/ */
bool register_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &name) bool add(const Proxy_Function &f, const std::string &name)
{ {
return add_function(f, name); return add_function(f, name);
} }
/**
* Add a generic, named boost::function() to the system
*/
template<typename Function>
bool register_function(const Function &func, const std::string &name)
{
return add_function(boost::shared_ptr<Proxy_Function>(new Proxy_Function_Impl<Function>(func)), name);
}
/** /**
* Set the value of an object, by name. If the object * Set the value of an object, by name. If the object
* is not available in the current scope it is created * is not available in the current scope it is created
*/ */
template<typename Class> void add(const Boxed_Value &obj, const std::string &name)
void set_object(const std::string &name, const Class &obj) {
for (int i = m_scopes.size()-1; i >= 0; --i)
{ {
for (int i = m_scopes.size()-1; i >= 0; --i) std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name);
if (itr != m_scopes[i].end())
{ {
std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name); m_scopes[i][name] = Boxed_Value(obj);
if (itr != m_scopes[i].end()) return;
{
m_scopes[i][name] = Boxed_Value(obj);
return;
}
} }
add_object(name, obj);
} }
add_object(name, obj);
}
/** /**
* Adds a named object to the current scope * Adds a named object to the current scope
*/ */
template<typename Class> void add_object(const std::string &name, const Boxed_Value &obj)
void add_object(const std::string &name, const Class &obj) {
{ m_scopes.back()[name] = Boxed_Value(obj);
m_scopes.back()[name] = Boxed_Value(obj); }
}
/** /**
* Adds a new scope to the stack * Adds a new scope to the stack
@@ -209,24 +198,23 @@ namespace dispatchkit
} }
} }
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> > funcs = get_function_impl(name, false); std::vector<std::pair<std::string, std::multimap<std::string, Proxy_Function >::mapped_type> > funcs = get_function_impl(name, false);
if (funcs.empty()) if (funcs.empty())
{ {
throw std::range_error("Object not known: " + name); throw std::range_error("Object not known: " + name);
} else { } else {
return Boxed_Value(boost::shared_ptr<Proxy_Function>(new Dispatch_Function(funcs))); return Boxed_Value(Proxy_Function(new Dispatch_Function(funcs)));
} }
} }
/** /**
* Registers a new named type * Registers a new named type
*/ */
template<typename Type> void add(const Type_Info &ti, const std::string &name)
void register_type(const std::string &name) {
{ m_types.insert(std::make_pair(name, ti));
m_types.insert(std::make_pair(name, Get_Type_Info<Type>::get())); }
}
/** /**
* Returns the type info for a named type * Returns the type info for a named type
@@ -274,7 +262,7 @@ namespace dispatchkit
/** /**
* Return a function by name * Return a function by name
*/ */
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> > std::vector<std::pair<std::string, std::multimap<std::string, Proxy_Function >::mapped_type> >
get_function(const std::string &t_name) const get_function(const std::string &t_name) const
{ {
return get_function_impl(t_name, true); return get_function_impl(t_name, true);
@@ -283,9 +271,9 @@ namespace dispatchkit
/** /**
* Get a vector of all registered functions * Get a vector of all registered functions
*/ */
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > get_functions() const std::vector<std::pair<std::string, Proxy_Function > > get_functions() const
{ {
return std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > >(m_functions.begin(), m_functions.end()); return std::vector<std::pair<std::string, Proxy_Function > >(m_functions.begin(), m_functions.end());
} }
private: private:
@@ -294,10 +282,10 @@ namespace dispatchkit
* Looks for all registered global functions and optionally for an object * Looks for all registered global functions and optionally for an object
* in scope with the same name * in scope with the same name
*/ */
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> > std::vector<std::pair<std::string, std::multimap<std::string, Proxy_Function >::mapped_type> >
get_function_impl(const std::string &t_name, bool include_objects) const get_function_impl(const std::string &t_name, bool include_objects) const
{ {
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> > funcs; std::vector<std::pair<std::string, std::multimap<std::string, Proxy_Function >::mapped_type> > funcs;
if (include_objects) if (include_objects)
{ {
@@ -305,14 +293,14 @@ namespace dispatchkit
funcs.insert(funcs.end(), funcs.insert(funcs.end(),
std::make_pair( std::make_pair(
t_name, t_name,
boxed_cast<std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type>(get_object(t_name))) boxed_cast<std::multimap<std::string, Proxy_Function >::mapped_type>(get_object(t_name)))
); );
} catch (const bad_boxed_cast &) { } catch (const bad_boxed_cast &) {
} catch (const std::range_error &) { } catch (const std::range_error &) {
} }
} }
std::pair<std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator> range std::pair<std::multimap<std::string, Proxy_Function >::const_iterator, std::multimap<std::string, Proxy_Function >::const_iterator> range
= m_functions.equal_range(t_name); = m_functions.equal_range(t_name);
funcs.insert(funcs.end(), range.first, range.second); funcs.insert(funcs.end(), range.first, range.second);
@@ -324,9 +312,9 @@ namespace dispatchkit
* true if the function was added, false if a function with the * true if the function was added, false if a function with the
* same signature and name already exists. * same signature and name already exists.
*/ */
bool add_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &t_name) bool add_function(const Proxy_Function &f, const std::string &t_name)
{ {
std::pair<std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator> range std::pair<std::multimap<std::string, Proxy_Function >::const_iterator, std::multimap<std::string, Proxy_Function >::const_iterator> range
= m_functions.equal_range(t_name); = m_functions.equal_range(t_name);
while (range.first != range.second) while (range.first != range.second)
@@ -344,7 +332,7 @@ namespace dispatchkit
std::deque<Scope> m_scopes; std::deque<Scope> m_scopes;
std::multimap<std::string, boost::shared_ptr<Proxy_Function> > m_functions; std::multimap<std::string, Proxy_Function > m_functions;
Type_Name_Map m_types; Type_Name_Map m_types;
Boxed_Value m_place_holder; Boxed_Value m_place_holder;
}; };
@@ -368,7 +356,7 @@ namespace dispatchkit
/** /**
* Dump function to stdout * Dump function to stdout
*/ */
void dump_function(const std::pair<const std::string, boost::shared_ptr<Proxy_Function> > &f, const Dispatch_Engine &e) void dump_function(const std::pair<const std::string, Proxy_Function > &f, const Dispatch_Engine &e)
{ {
std::vector<Type_Info> params = f.second->get_param_types(); std::vector<Type_Info> params = f.second->get_param_types();
std::string annotation = f.second->annotation(); std::string annotation = f.second->annotation();
@@ -412,10 +400,10 @@ namespace dispatchkit
} }
std::cout << std::endl; std::cout << std::endl;
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > funcs = s.get_functions(); std::vector<std::pair<std::string, Proxy_Function > > funcs = s.get_functions();
std::cout << "Functions: " << std::endl; std::cout << "Functions: " << std::endl;
for (std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > >::const_iterator itr = funcs.begin(); for (std::vector<std::pair<std::string, Proxy_Function > >::const_iterator itr = funcs.begin();
itr != funcs.end(); itr != funcs.end();
++itr) ++itr)
{ {

View File

@@ -21,7 +21,7 @@
#include <vector> #include <vector>
#include "proxy_functions.hpp" #include "proxy_functions.hpp"
namespace dispatchkit namespace chaiscript
{ {
/** /**
* Internal helper class for handling the return * Internal helper class for handling the return
@@ -35,7 +35,7 @@ namespace dispatchkit
{ {
} }
Ret call(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &t_funcs, Ret call(const std::vector<std::pair<std::string, Proxy_Function > > &t_funcs,
const std::vector<Boxed_Value> &params) const std::vector<Boxed_Value> &params)
{ {
return boxed_cast<Ret>(dispatch(t_funcs, params)); return boxed_cast<Ret>(dispatch(t_funcs, params));
@@ -53,7 +53,7 @@ namespace dispatchkit
{ {
} }
void call(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &t_funcs, void call(const std::vector<std::pair<std::string, Proxy_Function > > &t_funcs,
const std::vector<Boxed_Value> &params) const std::vector<Boxed_Value> &params)
{ {
dispatch(t_funcs, params); dispatch(t_funcs, params);
@@ -65,7 +65,7 @@ namespace dispatchkit
#define BOOST_PP_FILENAME_1 <chaiscript/dispatchkit/function_call.hpp> #define BOOST_PP_FILENAME_1 <chaiscript/dispatchkit/function_call.hpp>
#include BOOST_PP_ITERATE() #include BOOST_PP_ITERATE()
namespace dispatchkit namespace chaiscript
{ {
/** /**
* Build a function caller that knows how to dispatch on a set of functions * Build a function caller that knows how to dispatch on a set of functions
@@ -77,7 +77,7 @@ namespace dispatchkit
*/ */
template<typename FunctionType> template<typename FunctionType>
boost::function<FunctionType> boost::function<FunctionType>
build_function_caller(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs) functor(const std::vector<std::pair<std::string, Proxy_Function > > &funcs)
{ {
FunctionType *p; FunctionType *p;
return build_function_caller_helper(p, funcs); return build_function_caller_helper(p, funcs);
@@ -88,7 +88,7 @@ namespace dispatchkit
* useful in the case that a function is being pass out from scripting back * useful in the case that a function is being pass out from scripting back
* into code * into code
* example: * example:
* void my_function(boost::shared_ptr<Proxy_Function> f) * void my_function(Proxy_Function f)
* { * {
* boost::function<void (int)> local_f = * boost::function<void (int)> local_f =
* build_function_caller(f); * build_function_caller(f);
@@ -98,11 +98,11 @@ namespace dispatchkit
*/ */
template<typename FunctionType> template<typename FunctionType>
boost::function<FunctionType> boost::function<FunctionType>
build_function_caller(boost::shared_ptr<Proxy_Function> func) functor(Proxy_Function func)
{ {
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > funcs; std::vector<std::pair<std::string, Proxy_Function > > funcs;
funcs.push_back(std::make_pair(std::string(), func)); funcs.push_back(std::make_pair(std::string(), func));
return build_function_caller<FunctionType>(funcs); return functor<FunctionType>(funcs);
} }
/** /**
@@ -111,37 +111,24 @@ namespace dispatchkit
*/ */
template<typename FunctionType> template<typename FunctionType>
boost::function<FunctionType> boost::function<FunctionType>
build_function_caller(const Boxed_Value &bv) functor(const Boxed_Value &bv)
{ {
return build_function_caller<FunctionType>(boxed_cast<boost::shared_ptr<Proxy_Function> >(bv)); return functor<FunctionType>(boxed_cast<Proxy_Function >(bv));
} }
/**
* Helper for calling script code as if it were native C++ code
* example:
* boost::function<int (int, int)> f = build_functor(chai, "func(x, y){x+y}");
* \return a boost::function representing the passed in script
* \param[in] e ScriptEngine to build the script execution from
* \param[in] script Script code to build a function from
*/
template<typename FunctionType, typename ScriptEngine>
boost::function<FunctionType> build_functor(ScriptEngine &e, const std::string &script)
{
return build_function_caller<FunctionType>(e.evaluate_string(script));
}
} }
# endif # endif
#else #else
# define n BOOST_PP_ITERATION() # define n BOOST_PP_ITERATION()
namespace dispatchkit namespace chaiscript
{ {
/** /**
* used internally for unwrapping a function call's types * used internally for unwrapping a function call's types
*/ */
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) > template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
Ret function_caller(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs Ret function_caller(const std::vector<std::pair<std::string, Proxy_Function > > &funcs
BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) ) BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
{ {
std::vector<Boxed_Value> params; std::vector<Boxed_Value> params;
@@ -156,7 +143,7 @@ namespace dispatchkit
*/ */
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) > template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param)) > boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param)) >
build_function_caller_helper(Ret (BOOST_PP_ENUM_PARAMS(n, Param)), const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs) build_function_caller_helper(Ret (BOOST_PP_ENUM_PARAMS(n, Param)), const std::vector<std::pair<std::string, Proxy_Function> > &funcs)
{ {
return boost::bind(&function_caller<Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>, funcs return boost::bind(&function_caller<Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>, funcs
BOOST_PP_ENUM_TRAILING(n, curry, ~)); BOOST_PP_ENUM_TRAILING(n, curry, ~));

View File

@@ -19,17 +19,28 @@
#define BOOST_PP_FILENAME_1 <chaiscript/dispatchkit/proxy_constructors.hpp> #define BOOST_PP_FILENAME_1 <chaiscript/dispatchkit/proxy_constructors.hpp>
#include BOOST_PP_ITERATE() #include BOOST_PP_ITERATE()
# endif # endif
namespace chaiscript
{
template<typename T>
Proxy_Function constructor()
{
T *f;
return (build_constructor_(f));
}
}
#else #else
# define n BOOST_PP_ITERATION() # define n BOOST_PP_ITERATION()
namespace dispatchkit namespace chaiscript
{ {
/** /**
* A constructor function, used for creating a new object * A constructor function, used for creating a new object
* of a given type with a given set of params * of a given type with a given set of params
*/ */
template<typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) > template<typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
boost::shared_ptr<Class> constructor( BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) ) boost::shared_ptr<Class> constructor_( BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
{ {
return boost::shared_ptr<Class>(new Class( BOOST_PP_ENUM_PARAMS(n, p) )); return boost::shared_ptr<Class>(new Class( BOOST_PP_ENUM_PARAMS(n, p) ));
} }
@@ -41,10 +52,10 @@ namespace dispatchkit
* \todo See if it is possible to make this not be a variadic function * \todo See if it is possible to make this not be a variadic function
*/ */
template<typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) > template<typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
boost::function<boost::shared_ptr<Class> (BOOST_PP_ENUM_PARAMS(n, Param))> build_constructor() Proxy_Function build_constructor_(Class (*f)(BOOST_PP_ENUM_PARAMS(n, Param)))
{ {
typedef boost::shared_ptr<Class> (*func)(BOOST_PP_ENUM_PARAMS(n, Param)); typedef boost::shared_ptr<Class> (sig)(BOOST_PP_ENUM_PARAMS(n, Param));
return boost::function<boost::shared_ptr<Class> (BOOST_PP_ENUM_PARAMS(n, Param))>(func(&(constructor<Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>))); return Proxy_Function(new Proxy_Function_Impl<sig>(&(constructor_<Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>)));
} }
} }

View File

@@ -7,9 +7,9 @@
#include <boost/preprocessor.hpp> #include <boost/preprocessor.hpp>
#define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info<Param ## n>::get()); #define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info<Param ## n>::get());
#define casthelper(z,n,text) ,dispatchkit::boxed_cast< Param ## n >(params[n]) #define casthelper(z,n,text) ,chaiscript::boxed_cast< Param ## n >(params[n])
#define comparetype(z,n,text) && ((Get_Type_Info<Param ## n>::get() == params[n].get_type_info())) #define comparetype(z,n,text) && ((Get_Type_Info<Param ## n>::get() == params[n].get_type_info()))
#define trycast(z,n,text) dispatchkit::boxed_cast<Param ## n>(params[n]); #define trycast(z,n,text) chaiscript::boxed_cast<Param ## n>(params[n]);
#ifndef BOOST_PP_IS_ITERATING #ifndef BOOST_PP_IS_ITERATING
@@ -26,7 +26,7 @@
#include <stdexcept> #include <stdexcept>
#include <vector> #include <vector>
namespace dispatchkit namespace chaiscript
{ {
/** /**
* Used internally for handling a return value from a Proxy_Function call * Used internally for handling a return value from a Proxy_Function call
@@ -52,18 +52,6 @@ namespace dispatchkit
} }
}; };
/**
* Used internally for handling a return value from a Proxy_Function call
*/
template<typename Ret>
struct Handle_Return<Ret *>
{
Boxed_Value operator()(const boost::function<Ret *()> &f)
{
return Boxed_Value(boost::ref(*f()));
}
};
/** /**
* Used internally for handling a return value from a Proxy_Function call * Used internally for handling a return value from a Proxy_Function call
*/ */
@@ -107,7 +95,7 @@ namespace dispatchkit
* *
* example usage: * example usage:
* Boxed_Value retval = dispatch(dispatchengine.get_function("+"), * Boxed_Value retval = dispatch(dispatchengine.get_function("+"),
* dispatchkit::Param_List_Builder() << 5 << 6); * chaiscript::Param_List_Builder() << 5 << 6);
*/ */
struct Param_List_Builder struct Param_List_Builder
{ {
@@ -154,7 +142,7 @@ namespace dispatchkit
#define BOOST_PP_FILENAME_1 <chaiscript/dispatchkit/proxy_functions.hpp> #define BOOST_PP_FILENAME_1 <chaiscript/dispatchkit/proxy_functions.hpp>
#include BOOST_PP_ITERATE() #include BOOST_PP_ITERATE()
namespace dispatchkit namespace chaiscript
{ {
/** /**
* Pure virtual base class for all Proxy_Function implementations * Pure virtual base class for all Proxy_Function implementations
@@ -164,17 +152,19 @@ namespace dispatchkit
* Dispatch_Engine only knows how to work with Proxy_Function, no other * Dispatch_Engine only knows how to work with Proxy_Function, no other
* function classes. * function classes.
*/ */
class Proxy_Function class Proxy_Function_Base
{ {
public: public:
virtual ~Proxy_Function() {} virtual ~Proxy_Function_Base() {}
virtual Boxed_Value operator()(const std::vector<Boxed_Value> &params) = 0; virtual Boxed_Value operator()(const std::vector<Boxed_Value> &params) = 0;
virtual std::vector<Type_Info> get_param_types() const = 0; virtual std::vector<Type_Info> get_param_types() const = 0;
virtual bool operator==(const Proxy_Function &) const = 0; virtual bool operator==(const Proxy_Function_Base &) const = 0;
virtual bool types_match(const std::vector<Boxed_Value> &types) const = 0; virtual bool types_match(const std::vector<Boxed_Value> &types) const = 0;
virtual std::string annotation() const = 0; virtual std::string annotation() const = 0;
}; };
typedef boost::shared_ptr<Proxy_Function_Base> Proxy_Function;
/** /**
* Exception thrown if a function's guard fails to execute * Exception thrown if a function's guard fails to execute
*/ */
@@ -193,19 +183,19 @@ namespace dispatchkit
* A Proxy_Function implementation that is not type safe, the called function * A Proxy_Function implementation that is not type safe, the called function
* is expecting a vector<Boxed_Value> that it works with how it chooses. * is expecting a vector<Boxed_Value> that it works with how it chooses.
*/ */
class Dynamic_Proxy_Function : public Proxy_Function class Dynamic_Proxy_Function : public Proxy_Function_Base
{ {
public: public:
Dynamic_Proxy_Function( Dynamic_Proxy_Function(
const boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> &t_f, const boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> &t_f,
int t_arity=-1, int t_arity=-1,
const std::string &t_description = "", const std::string &t_description = "",
const boost::shared_ptr<Proxy_Function> &t_guard = boost::shared_ptr<Proxy_Function>()) const Proxy_Function &t_guard = Proxy_Function())
: m_f(t_f), m_arity(t_arity), m_description(t_description), m_guard(t_guard) : m_f(t_f), m_arity(t_arity), m_description(t_description), m_guard(t_guard)
{ {
} }
virtual bool operator==(const Proxy_Function &) const virtual bool operator==(const Proxy_Function_Base &) const
{ {
return false; return false;
} }
@@ -279,7 +269,7 @@ namespace dispatchkit
boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f; boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f;
int m_arity; int m_arity;
std::string m_description; std::string m_description;
boost::shared_ptr<Proxy_Function> m_guard; Proxy_Function m_guard;
}; };
/** /**
@@ -296,16 +286,16 @@ namespace dispatchkit
* at runtime, when call() is executed. * at runtime, when call() is executed.
* it is used for bind(function, param1, _, param2) style calls * it is used for bind(function, param1, _, param2) style calls
*/ */
class Bound_Function : public Proxy_Function class Bound_Function : public Proxy_Function_Base
{ {
public: public:
Bound_Function(const boost::shared_ptr<Proxy_Function> &t_f, Bound_Function(const Proxy_Function &t_f,
const std::vector<Boxed_Value> &t_args) const std::vector<Boxed_Value> &t_args)
: m_f(t_f), m_args(t_args) : m_f(t_f), m_args(t_args)
{ {
} }
virtual bool operator==(const Proxy_Function &) const virtual bool operator==(const Proxy_Function_Base &) const
{ {
return false; return false;
} }
@@ -372,7 +362,7 @@ namespace dispatchkit
} }
private: private:
boost::shared_ptr<Proxy_Function> m_f; Proxy_Function m_f;
std::vector<Boxed_Value> m_args; std::vector<Boxed_Value> m_args;
}; };
@@ -382,17 +372,17 @@ namespace dispatchkit
* type checking of Boxed_Value parameters, in a type safe manner * type checking of Boxed_Value parameters, in a type safe manner
*/ */
template<typename Func> template<typename Func>
class Proxy_Function_Impl : public Proxy_Function class Proxy_Function_Impl : public Proxy_Function_Base
{ {
public: public:
Proxy_Function_Impl(const Func &f) Proxy_Function_Impl(const boost::function<Func> &f)
: m_f(f) : m_f(f)
{ {
} }
virtual ~Proxy_Function_Impl() {} virtual ~Proxy_Function_Impl() {}
virtual bool operator==(const Proxy_Function &t_func) const virtual bool operator==(const Proxy_Function_Base &t_func) const
{ {
try { try {
dynamic_cast<const Proxy_Function_Impl<Func> &>(t_func); dynamic_cast<const Proxy_Function_Impl<Func> &>(t_func);
@@ -409,12 +399,14 @@ namespace dispatchkit
virtual std::vector<Type_Info> get_param_types() const virtual std::vector<Type_Info> get_param_types() const
{ {
return build_param_type_list(m_f); Func *f;
return build_param_type_list(f);
} }
virtual bool types_match(const std::vector<Boxed_Value> &types) const virtual bool types_match(const std::vector<Boxed_Value> &types) const
{ {
return compare_types(m_f, types); Func *f;
return compare_types(f, types);
} }
virtual std::string annotation() const virtual std::string annotation() const
@@ -423,7 +415,7 @@ namespace dispatchkit
} }
private: private:
Func m_f; boost::function<Func> m_f;
}; };
/** /**
@@ -447,10 +439,10 @@ namespace dispatchkit
* each function against the set of parameters, in order, until a matching * each function against the set of parameters, in order, until a matching
* function is found or throw dispatch_error if no matching function is found * function is found or throw dispatch_error if no matching function is found
*/ */
Boxed_Value dispatch(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs, Boxed_Value dispatch(const std::vector<std::pair<std::string, Proxy_Function> > &funcs,
const std::vector<Boxed_Value> &plist) const std::vector<Boxed_Value> &plist)
{ {
for (std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > >::const_iterator itr = funcs.begin(); for (std::vector<std::pair<std::string, Proxy_Function> >::const_iterator itr = funcs.begin();
itr != funcs.end(); itr != funcs.end();
++itr) ++itr)
{ {
@@ -473,14 +465,14 @@ namespace dispatchkit
#else #else
# define n BOOST_PP_ITERATION() # define n BOOST_PP_ITERATION()
namespace dispatchkit namespace chaiscript
{ {
/** /**
* Used by Proxy_Function_Impl to return a list of all param types * Used by Proxy_Function_Impl to return a list of all param types
* it contains. * it contains.
*/ */
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) > template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
std::vector<Type_Info> build_param_type_list(const boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))> &) std::vector<Type_Info> build_param_type_list(Ret (*)(BOOST_PP_ENUM_PARAMS(n, Param)))
{ {
std::vector<Type_Info> ti; std::vector<Type_Info> ti;
ti.push_back(Get_Type_Info<Ret>::get()); ti.push_back(Get_Type_Info<Ret>::get());
@@ -514,7 +506,7 @@ namespace dispatchkit
* registration of two functions with the exact same signatures * registration of two functions with the exact same signatures
*/ */
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)> template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
bool compare_types(const boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))> &, bool compare_types(Ret (*)(BOOST_PP_ENUM_PARAMS(n, Param)),
const std::vector<Boxed_Value> &params) const std::vector<Boxed_Value> &params)
{ {
if (params.size() != n) if (params.size() != n)

View File

@@ -14,8 +14,15 @@
#include <boost/function.hpp> #include <boost/function.hpp>
#include <boost/bind.hpp> #include <boost/bind.hpp>
namespace dispatchkit namespace chaiscript
{ {
template<typename T>
Proxy_Function fun(const boost::function<T> &f)
{
return Proxy_Function(new Proxy_Function_Impl<T>(f));
}
/** /**
* Helper function for register_member function * Helper function for register_member function
*/ */
@@ -31,10 +38,11 @@ namespace dispatchkit
* for example, the case of std::pair<>::first and std::pair<>::second * for example, the case of std::pair<>::first and std::pair<>::second
*/ */
template<typename T, typename Class> template<typename T, typename Class>
void register_member(Dispatch_Engine &s, T Class::* m, const std::string &name) Proxy_Function fun(T Class::* m)
{ {
s.register_function(boost::function<T& (Class *)>(boost::bind(&get_member<T, Class>, m, _1)), name); return fun(boost::function<T& (Class *)>(boost::bind(&get_member<T, Class>, m, _1)));
} }
} }
#define BOOST_PP_ITERATION_LIMITS ( 0, 10 ) #define BOOST_PP_ITERATION_LIMITS ( 0, 10 )
@@ -45,33 +53,33 @@ namespace dispatchkit
#else #else
# define n BOOST_PP_ITERATION() # define n BOOST_PP_ITERATION()
namespace dispatchkit namespace chaiscript
{ {
/** /**
* Register a global function of n parameters with name * Register a global function of n parameters with name
*/ */
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)> template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
void register_function(Dispatch_Engine &s, Ret (*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const std::string &name) Proxy_Function fun(Ret (*f)(BOOST_PP_ENUM_PARAMS(n, Param)))
{ {
s.register_function(boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))>(f), name); return fun(boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))>(f));
} }
/** /**
* Register a class method of n parameters with name * Register a class method of n parameters with name
*/ */
template<typename Ret, typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)> template<typename Ret, typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
void register_function(Dispatch_Engine &s, Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const std::string &name) Proxy_Function fun(Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param)))
{ {
s.register_function(boost::function<Ret (Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f), name); return fun(boost::function<Ret (Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f));
} }
/** /**
* Register a const class method of n parameters with name * Register a const class method of n parameters with name
*/ */
template<typename Ret, typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)> template<typename Ret, typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
void register_function(Dispatch_Engine &s, Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param))const, const std::string &name) Proxy_Function fun(Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param))const)
{ {
s.register_function(boost::function<Ret (const Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f), name); return fun(boost::function<Ret (const Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f));
} }
} }

View File

@@ -16,7 +16,7 @@
#include <boost/type_traits/remove_pointer.hpp> #include <boost/type_traits/remove_pointer.hpp>
#include <boost/ref.hpp> #include <boost/ref.hpp>
namespace dispatchkit namespace chaiscript
{ {
/** /**
* compile time deduced information about a type * compile time deduced information about a type
@@ -115,6 +115,22 @@ namespace dispatchkit
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type)); &typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
} }
}; };
template<typename T>
Type_Info type_(T)
{
return Get_Type_Info<T>::get();
}
template<typename T>
Type_Info type_()
{
return Get_Type_Info<T>::get();
}
} }
#endif #endif

View File

@@ -26,6 +26,27 @@ namespace chaiscript
build_eval_system(); build_eval_system();
} }
/**
* Adds an object to the system: type, function, object
*/
template<typename T>
void add(const T &t, const std::string &name)
{
engine.add(t, name);
}
/**
* Helper for calling script code as if it were native C++ code
* example:
* boost::function<int (int, int)> f = build_functor(chai, "func(x, y){x+y}");
* \return a boost::function representing the passed in script
* \param[in] script Script code to build a function from
*/
template<typename FunctionType>
boost::function<FunctionType> functor(const std::string &script)
{
return chaiscript::functor<FunctionType>(evaluate_string(script));
}
/** /**
* Returns the current evaluation engine * Returns the current evaluation engine
*/ */
@@ -46,9 +67,9 @@ namespace chaiscript
/** /**
* Evaluates the given boxed string, used during eval() inside of a script * Evaluates the given boxed string, used during eval() inside of a script
*/ */
const dispatchkit::Boxed_Value eval(const std::vector<dispatchkit::Boxed_Value> &vals) { const Boxed_Value eval(const std::vector<Boxed_Value> &vals) {
std::string val; std::string val;
val = dispatchkit::boxed_cast<std::string &>(vals[0]); val = boxed_cast<std::string &>(vals[0]);
return evaluate_string(val); return evaluate_string(val);
} }
@@ -79,14 +100,14 @@ namespace chaiscript
* Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude. * Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude.
*/ */
void build_eval_system() { void build_eval_system() {
dispatchkit::Bootstrap::bootstrap(engine); Bootstrap::bootstrap(engine);
dispatchkit::bootstrap_vector<std::vector<dispatchkit::Boxed_Value> >(engine, "Vector"); bootstrap_vector<std::vector<Boxed_Value> >(engine, "Vector");
dispatchkit::bootstrap_string<std::string>(engine, "string"); bootstrap_string<std::string>(engine, "string");
dispatchkit::bootstrap_map<std::map<std::string, dispatchkit::Boxed_Value> >(engine, "Map"); bootstrap_map<std::map<std::string, Boxed_Value> >(engine, "Map");
dispatchkit::bootstrap_pair<std::pair<dispatchkit::Boxed_Value, dispatchkit::Boxed_Value > >(engine, "Pair"); bootstrap_pair<std::pair<Boxed_Value, Boxed_Value > >(engine, "Pair");
engine.register_function(boost::shared_ptr<dispatchkit::Proxy_Function>( engine.add(Proxy_Function(
new dispatchkit::Dynamic_Proxy_Function(boost::bind(&ChaiScript_System<Eval_Engine>::eval, boost::ref(*this), _1), 1)), "eval"); new Dynamic_Proxy_Function(boost::bind(&ChaiScript_System<Eval_Engine>::eval, boost::ref(*this), _1), 1)), "eval");
evaluate_string(chaiscript_prelude, "standard prelude"); evaluate_string(chaiscript_prelude, "standard prelude");
} }
@@ -94,9 +115,9 @@ namespace chaiscript
/** /**
* Evaluates the given string in by parsing it and running the results through the evaluator * Evaluates the given string in by parsing it and running the results through the evaluator
*/ */
dispatchkit::Boxed_Value evaluate_string(const std::string &input, const char *filename = "__EVAL__") { Boxed_Value evaluate_string(const std::string &input, const char *filename = "__EVAL__") {
//debug_print(tokens); //debug_print(tokens);
dispatchkit::Boxed_Value value; Boxed_Value value;
parser.clear_match_stack(); parser.clear_match_stack();
try { try {
@@ -115,12 +136,12 @@ namespace chaiscript
/** /**
* Loads the file specified by filename, evaluates it, and returns the result * Loads the file specified by filename, evaluates it, and returns the result
*/ */
dispatchkit::Boxed_Value evaluate_file(const char *filename) { Boxed_Value evaluate_file(const char *filename) {
return evaluate_string(load_file(filename), filename); return evaluate_string(load_file(filename), filename);
} }
}; };
typedef ChaiScript_System<dispatchkit::Dispatch_Engine> ChaiScript_Engine; typedef ChaiScript_System<Dispatch_Engine> ChaiScript_Engine;
} }
#endif /* CHAISCRIPT_ENGINE_HPP_ */ #endif /* CHAISCRIPT_ENGINE_HPP_ */

View File

@@ -15,14 +15,14 @@ namespace chaiscript
* Helper function that will set up the scope around a function call, including handling the named function parameters * Helper function that will set up the scope around a function call, including handling the named function parameters
*/ */
template <typename Eval_System> template <typename Eval_System>
const dispatchkit::Boxed_Value eval_function (Eval_System &ss, TokenPtr node, const std::vector<std::string> &param_names, const std::vector<dispatchkit::Boxed_Value> &vals) { const Boxed_Value eval_function (Eval_System &ss, TokenPtr node, const std::vector<std::string> &param_names, const std::vector<Boxed_Value> &vals) {
ss.new_scope(); ss.new_scope();
for (unsigned int i = 0; i < param_names.size(); ++i) { for (unsigned int i = 0; i < param_names.size(); ++i) {
ss.add_object(param_names[i], vals[i]); ss.add_object(param_names[i], vals[i]);
} }
dispatchkit::Boxed_Value retval; Boxed_Value retval;
try { try {
retval = eval_token(ss, node); retval = eval_token(ss, node);
@@ -42,8 +42,8 @@ namespace chaiscript
* Evaluates the top-level file node * Evaluates the top-level file node
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_file(Eval_System &ss, TokenPtr node) { Boxed_Value eval_file(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
unsigned int i; unsigned int i;
for (i = 0; i < node->children.size(); ++i) { for (i = 0; i < node->children.size(); ++i) {
retval = eval_token(ss, node->children[i]); retval = eval_token(ss, node->children[i]);
@@ -55,13 +55,13 @@ namespace chaiscript
* Evaluates a variable or function name identifier * Evaluates a variable or function name identifier
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_id(Eval_System &ss, TokenPtr node) { Boxed_Value eval_id(Eval_System &ss, TokenPtr node) {
if (node->text == "true") { if (node->text == "true") {
return dispatchkit::Boxed_Value(true); return Boxed_Value(true);
} }
else if (node->text == "false") { else if (node->text == "false") {
return dispatchkit::Boxed_Value(false); return Boxed_Value(false);
} }
else { else {
try { try {
@@ -77,68 +77,68 @@ namespace chaiscript
* Evaluates a floating point number * Evaluates a floating point number
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_float(Eval_System &, TokenPtr node) { Boxed_Value eval_float(Eval_System &, TokenPtr node) {
return dispatchkit::Boxed_Value(double(atof(node->text.c_str()))); return Boxed_Value(double(atof(node->text.c_str())));
} }
/** /**
* Evaluates an integer * Evaluates an integer
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_int(Eval_System &, TokenPtr node) { Boxed_Value eval_int(Eval_System &, TokenPtr node) {
return dispatchkit::Boxed_Value(atoi(node->text.c_str())); return Boxed_Value(atoi(node->text.c_str()));
} }
/** /**
* Evaluates a quoted string * Evaluates a quoted string
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_quoted_string(Eval_System &, TokenPtr node) { Boxed_Value eval_quoted_string(Eval_System &, TokenPtr node) {
return dispatchkit::Boxed_Value(node->text); return Boxed_Value(node->text);
} }
/** /**
* Evaluates a char group * Evaluates a char group
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_single_quoted_string(Eval_System &, TokenPtr node) { Boxed_Value eval_single_quoted_string(Eval_System &, TokenPtr node) {
return dispatchkit::Boxed_Value(node->text); return Boxed_Value(node->text);
} }
/** /**
* Evaluates a string of equations in reverse order so that the right-most side has precedence * Evaluates a string of equations in reverse order so that the right-most side has precedence
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_equation(Eval_System &ss, TokenPtr node) { Boxed_Value eval_equation(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
unsigned int i; unsigned int i;
retval = eval_token(ss, node->children.back()); retval = eval_token(ss, node->children.back());
if (node->children.size() > 1) { if (node->children.size() > 1) {
for (i = node->children.size()-3; ((int)i) >= 0; i -= 2) { for (i = node->children.size()-3; ((int)i) >= 0; i -= 2) {
if (node->children[i+1]->text == "=") { if (node->children[i+1]->text == "=") {
dispatchkit::Boxed_Value lhs = eval_token(ss, node->children[i]); Boxed_Value lhs = eval_token(ss, node->children[i]);
try { try {
if (lhs.is_unknown()) if (lhs.is_unknown())
{ {
retval = dispatch(ss.get_function("clone"), dispatchkit::Param_List_Builder() << retval); retval = dispatch(ss.get_function("clone"), Param_List_Builder() << retval);
} }
dispatchkit::Param_List_Builder plb; Param_List_Builder plb;
plb << lhs; plb << lhs;
plb << retval; plb << retval;
try { try {
retval = dispatch(ss.get_function(node->children[i+1]->text), plb); retval = dispatch(ss.get_function(node->children[i+1]->text), plb);
} }
catch(const dispatchkit::dispatch_error &){ catch(const dispatch_error &){
throw Eval_Error("Mismatched types in equation", node->children[i+1]); throw Eval_Error("Mismatched types in equation", node->children[i+1]);
} }
} }
catch(const dispatchkit::dispatch_error &){ catch(const dispatch_error &){
throw Eval_Error("Can not clone right hand side of equation", node->children[i+1]); throw Eval_Error("Can not clone right hand side of equation", node->children[i+1]);
} }
} }
else if (node->children[i+1]->text == ":=") { else if (node->children[i+1]->text == ":=") {
dispatchkit::Boxed_Value lhs = eval_token(ss, node->children[i]); Boxed_Value lhs = eval_token(ss, node->children[i]);
if (lhs.is_unknown() || dispatchkit::Bootstrap::type_match(lhs, retval)) { if (lhs.is_unknown() || Bootstrap::type_match(lhs, retval)) {
lhs.assign(retval); lhs.assign(retval);
} }
else { else {
@@ -146,13 +146,13 @@ namespace chaiscript
} }
} }
else { else {
dispatchkit::Param_List_Builder plb; Param_List_Builder plb;
plb << eval_token(ss, node->children[i]); plb << eval_token(ss, node->children[i]);
plb << retval; plb << retval;
try { try {
retval = dispatch(ss.get_function(node->children[i+1]->text), plb); retval = dispatch(ss.get_function(node->children[i+1]->text), plb);
} }
catch(const dispatchkit::dispatch_error &){ catch(const dispatch_error &){
throw Eval_Error("Can not find appropriate '" + node->children[i+1]->text + "'", node->children[i+1]); throw Eval_Error("Can not find appropriate '" + node->children[i+1]->text + "'", node->children[i+1]);
} }
} }
@@ -165,8 +165,8 @@ namespace chaiscript
* Evaluates a variable declaration * Evaluates a variable declaration
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_var_decl(Eval_System &ss, TokenPtr node) { Boxed_Value eval_var_decl(Eval_System &ss, TokenPtr node) {
ss.add_object(node->children[0]->text, dispatchkit::Boxed_Value()); ss.add_object(node->children[0]->text, Boxed_Value());
return ss.get_object(node->children[0]->text); return ss.get_object(node->children[0]->text);
} }
@@ -174,8 +174,8 @@ namespace chaiscript
* Evaluates binary boolean operators. Respects short-circuiting rules. * Evaluates binary boolean operators. Respects short-circuiting rules.
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_expression(Eval_System &ss, TokenPtr node) { Boxed_Value eval_expression(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
unsigned int i; unsigned int i;
retval = eval_token(ss, node->children[0]); retval = eval_token(ss, node->children[0]);
@@ -183,9 +183,9 @@ namespace chaiscript
for (i = 1; i < node->children.size(); i += 2) { for (i = 1; i < node->children.size(); i += 2) {
bool lhs; bool lhs;
try { try {
lhs = dispatchkit::boxed_cast<bool &>(retval); lhs = boxed_cast<bool &>(retval);
} }
catch (const dispatchkit::bad_boxed_cast &) { catch (const bad_boxed_cast &) {
throw Eval_Error("Condition not boolean", node); throw Eval_Error("Condition not boolean", node);
} }
if (node->children[i]->text == "&&") { if (node->children[i]->text == "&&") {
@@ -193,12 +193,12 @@ namespace chaiscript
retval = eval_token(ss, node->children[i+1]); retval = eval_token(ss, node->children[i+1]);
} }
else { else {
retval = dispatchkit::Boxed_Value(false); retval = Boxed_Value(false);
} }
} }
else if (node->children[i]->text == "||") { else if (node->children[i]->text == "||") {
if (lhs) { if (lhs) {
retval = dispatchkit::Boxed_Value(true); retval = Boxed_Value(true);
} }
else { else {
retval = eval_token(ss, node->children[i+1]); retval = eval_token(ss, node->children[i+1]);
@@ -213,21 +213,21 @@ namespace chaiscript
* Evaluates comparison, additions, and multiplications and their relatives * Evaluates comparison, additions, and multiplications and their relatives
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_comp_add_mul(Eval_System &ss, TokenPtr node) { Boxed_Value eval_comp_add_mul(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
unsigned int i; unsigned int i;
retval = eval_token(ss, node->children[0]); retval = eval_token(ss, node->children[0]);
if (node->children.size() > 1) { if (node->children.size() > 1) {
for (i = 1; i < node->children.size(); i += 2) { for (i = 1; i < node->children.size(); i += 2) {
dispatchkit::Param_List_Builder plb; Param_List_Builder plb;
plb << retval; plb << retval;
plb << eval_token(ss, node->children[i + 1]); plb << eval_token(ss, node->children[i + 1]);
try { try {
retval = dispatch(ss.get_function(node->children[i]->text), plb); retval = dispatch(ss.get_function(node->children[i]->text), plb);
} }
catch(const dispatchkit::dispatch_error &){ catch(const dispatch_error &){
throw Eval_Error("Can not find appropriate '" + node->children[i]->text + "'", node->children[i]); throw Eval_Error("Can not find appropriate '" + node->children[i]->text + "'", node->children[i]);
} }
} }
@@ -240,13 +240,13 @@ namespace chaiscript
* Evaluates an array lookup * Evaluates an array lookup
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_array_call(Eval_System &ss, TokenPtr node) { Boxed_Value eval_array_call(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
unsigned int i; unsigned int i;
retval = eval_token(ss, node->children[0]); retval = eval_token(ss, node->children[0]);
for (i = 1; i < node->children.size(); ++i) { for (i = 1; i < node->children.size(); ++i) {
dispatchkit::Param_List_Builder plb; Param_List_Builder plb;
plb << retval; plb << retval;
plb << eval_token(ss, node->children[i]); plb << eval_token(ss, node->children[i]);
try { try {
@@ -255,7 +255,7 @@ namespace chaiscript
catch(std::out_of_range &) { catch(std::out_of_range &) {
throw Eval_Error("Out of bounds exception", node); throw Eval_Error("Out of bounds exception", node);
} }
catch(const dispatchkit::dispatch_error &){ catch(const dispatch_error &){
throw Eval_Error("Can not find appropriate array lookup '[]' " + node->children[i]->text, node->children[i]); throw Eval_Error("Can not find appropriate array lookup '[]' " + node->children[i]->text, node->children[i]);
} }
} }
@@ -267,13 +267,13 @@ namespace chaiscript
* Evaluates a unary negation * Evaluates a unary negation
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_negate(Eval_System &ss, TokenPtr node) { Boxed_Value eval_negate(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
retval = eval_token(ss, node->children[0]); retval = eval_token(ss, node->children[0]);
dispatchkit::Param_List_Builder plb; Param_List_Builder plb;
plb << retval; plb << retval;
plb << dispatchkit::Boxed_Value(-1.0); plb << Boxed_Value(-1.0);
try { try {
return dispatch(ss.get_function("*"), plb); return dispatch(ss.get_function("*"), plb);
@@ -287,29 +287,29 @@ namespace chaiscript
* Evaluates a unary boolean not * Evaluates a unary boolean not
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_not(Eval_System &ss, TokenPtr node) { Boxed_Value eval_not(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
bool cond; bool cond;
try { try {
retval = eval_token(ss, node->children[0]); retval = eval_token(ss, node->children[0]);
cond = dispatchkit::boxed_cast<bool &>(retval); cond = boxed_cast<bool &>(retval);
} }
catch (const dispatchkit::bad_boxed_cast &) { catch (const bad_boxed_cast &) {
throw Eval_Error("Boolean not('!') condition not boolean", node->children[0]); throw Eval_Error("Boolean not('!') condition not boolean", node->children[0]);
} }
return dispatchkit::Boxed_Value(!cond); return Boxed_Value(!cond);
} }
/** /**
* Evaluates any unary prefix * Evaluates any unary prefix
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_prefix(Eval_System &ss, TokenPtr node) { Boxed_Value eval_prefix(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
retval = eval_token(ss, node->children[1]); retval = eval_token(ss, node->children[1]);
dispatchkit::Param_List_Builder plb; Param_List_Builder plb;
plb << retval; plb << retval;
try { try {
@@ -324,25 +324,25 @@ namespace chaiscript
* Evaluates (and generates) an inline array initialization * Evaluates (and generates) an inline array initialization
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_inline_array(Eval_System &ss, TokenPtr node) { Boxed_Value eval_inline_array(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
unsigned int i; unsigned int i;
try { try {
retval = dispatch(ss.get_function("Vector"), dispatchkit::Param_List_Builder()); retval = dispatch(ss.get_function("Vector"), Param_List_Builder());
if (node->children.size() > 0) { if (node->children.size() > 0) {
for (i = 0; i < node->children[0]->children.size(); ++i) { for (i = 0; i < node->children[0]->children.size(); ++i) {
try { try {
dispatchkit::Boxed_Value tmp = eval_token(ss, node->children[0]->children[i]); Boxed_Value tmp = eval_token(ss, node->children[0]->children[i]);
dispatch(ss.get_function("push_back"), dispatchkit::Param_List_Builder() << retval << tmp); dispatch(ss.get_function("push_back"), Param_List_Builder() << retval << tmp);
} }
catch (const dispatchkit::dispatch_error &) { catch (const dispatch_error &) {
throw Eval_Error("Can not find appropriate 'push_back'", node->children[0]->children[i]); throw Eval_Error("Can not find appropriate 'push_back'", node->children[0]->children[i]);
} }
} }
} }
} }
catch (const dispatchkit::dispatch_error &) { catch (const dispatch_error &) {
throw Eval_Error("Can not find appropriate 'Vector()'", node); throw Eval_Error("Can not find appropriate 'Vector()'", node);
} }
@@ -353,13 +353,13 @@ namespace chaiscript
* Evaluates (and generates) an inline range initialization * Evaluates (and generates) an inline range initialization
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_inline_range(Eval_System &ss, TokenPtr node) { Boxed_Value eval_inline_range(Eval_System &ss, TokenPtr node) {
try { try {
return dispatch(ss.get_function("generate_range"), dispatchkit::Param_List_Builder() return dispatch(ss.get_function("generate_range"), Param_List_Builder()
<< eval_token(ss, node->children[0]->children[0]->children[0]) << eval_token(ss, node->children[0]->children[0]->children[0])
<< eval_token(ss, node->children[0]->children[0]->children[1])); << eval_token(ss, node->children[0]->children[0]->children[1]));
} }
catch (const dispatchkit::dispatch_error &) { catch (const dispatch_error &) {
throw Eval_Error("Unable to generate range vector", node); throw Eval_Error("Unable to generate range vector", node);
} }
} }
@@ -368,24 +368,24 @@ namespace chaiscript
* Evaluates (and generates) an inline map initialization * Evaluates (and generates) an inline map initialization
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_inline_map(Eval_System &ss, TokenPtr node) { Boxed_Value eval_inline_map(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
unsigned int i; unsigned int i;
try { try {
retval = dispatch(ss.get_function("Map"), dispatchkit::Param_List_Builder()); retval = dispatch(ss.get_function("Map"), Param_List_Builder());
for (i = 0; i < node->children[0]->children.size(); ++i) { for (i = 0; i < node->children[0]->children.size(); ++i) {
try { try {
dispatchkit::Boxed_Value key = eval_token(ss, node->children[0]->children[i]->children[0]); Boxed_Value key = eval_token(ss, node->children[0]->children[i]->children[0]);
dispatchkit::Boxed_Value slot = dispatch(ss.get_function("[]"), dispatchkit::Param_List_Builder() << retval << key); Boxed_Value slot = dispatch(ss.get_function("[]"), Param_List_Builder() << retval << key);
dispatch(ss.get_function("="), dispatchkit::Param_List_Builder() << slot << eval_token(ss, node->children[0]->children[i]->children[1])); dispatch(ss.get_function("="), Param_List_Builder() << slot << eval_token(ss, node->children[0]->children[i]->children[1]));
} }
catch (const dispatchkit::dispatch_error &) { catch (const dispatch_error &) {
throw Eval_Error("Can not find appropriate '=' for map init", node->children[0]->children[i]); throw Eval_Error("Can not find appropriate '=' for map init", node->children[0]->children[i]);
} }
} }
} }
catch (const dispatchkit::dispatch_error &) { catch (const dispatch_error &) {
throw Eval_Error("Can not find appropriate 'Map()'", node); throw Eval_Error("Can not find appropriate 'Map()'", node);
} }
@@ -396,21 +396,21 @@ namespace chaiscript
* Evaluates a function call, starting with its arguments. Handles resetting the scope to the previous one after the call. * Evaluates a function call, starting with its arguments. Handles resetting the scope to the previous one after the call.
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_fun_call(Eval_System &ss, TokenPtr node) { Boxed_Value eval_fun_call(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
dispatchkit::Param_List_Builder plb; Param_List_Builder plb;
dispatchkit::Dispatch_Engine::Stack prev_stack = ss.get_stack(); Dispatch_Engine::Stack prev_stack = ss.get_stack();
dispatchkit::Dispatch_Engine::Stack new_stack; Dispatch_Engine::Stack new_stack;
unsigned int i; unsigned int i;
new_stack.push_back(dispatchkit::Dispatch_Engine::Scope()); new_stack.push_back(Dispatch_Engine::Scope());
if ((node->children.size() > 1) && (node->children[1]->identifier == Token_Type::Arg_List)) { if ((node->children.size() > 1) && (node->children[1]->identifier == Token_Type::Arg_List)) {
for (i = 0; i < node->children[1]->children.size(); ++i) { for (i = 0; i < node->children[1]->children.size(); ++i) {
plb << eval_token(ss, node->children[1]->children[i]); plb << eval_token(ss, node->children[1]->children[i]);
} }
} }
dispatchkit::Boxed_Value fn; Boxed_Value fn;
try { try {
fn = eval_token(ss, node->children[0]); fn = eval_token(ss, node->children[0]);
} }
@@ -420,10 +420,10 @@ namespace chaiscript
} }
try { try {
ss.set_stack(new_stack); ss.set_stack(new_stack);
retval = (*dispatchkit::boxed_cast<boost::shared_ptr<dispatchkit::Proxy_Function> >(fn))(plb); retval = (*boxed_cast<Proxy_Function >(fn))(plb);
ss.set_stack(prev_stack); ss.set_stack(prev_stack);
} }
catch(const dispatchkit::dispatch_error &e){ catch(const dispatch_error &e){
ss.set_stack(prev_stack); ss.set_stack(prev_stack);
throw Eval_Error(std::string(e.what()) + " with function '" + node->children[0]->text + "'", node->children[0]); throw Eval_Error(std::string(e.what()) + " with function '" + node->children[0]->text + "'", node->children[0]);
} }
@@ -443,21 +443,21 @@ namespace chaiscript
* Evaluates a method/attributes invocation * Evaluates a method/attributes invocation
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_dot_access(Eval_System &ss, TokenPtr node) { Boxed_Value eval_dot_access(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
std::vector<std::pair<std::string, boost::shared_ptr<dispatchkit::Proxy_Function> > > fn; std::vector<std::pair<std::string, Proxy_Function > > fn;
dispatchkit::Dispatch_Engine::Stack prev_stack = ss.get_stack(); Dispatch_Engine::Stack prev_stack = ss.get_stack();
dispatchkit::Dispatch_Engine::Stack new_stack; Dispatch_Engine::Stack new_stack;
unsigned int i, j; unsigned int i, j;
new_stack.push_back(dispatchkit::Dispatch_Engine::Scope()); new_stack.push_back(Dispatch_Engine::Scope());
//todo: Please extract a single way of doing function calls between this and eval_fun_call //todo: Please extract a single way of doing function calls between this and eval_fun_call
retval = eval_token(ss, node->children[0]); retval = eval_token(ss, node->children[0]);
if (node->children.size() > 1) { if (node->children.size() > 1) {
for (i = 1; i < node->children.size(); ++i) { for (i = 1; i < node->children.size(); ++i) {
dispatchkit::Param_List_Builder plb; Param_List_Builder plb;
plb << retval; plb << retval;
if (node->children[i]->children.size() > 1) { if (node->children[i]->children.size() > 1) {
@@ -480,7 +480,7 @@ namespace chaiscript
retval = dispatch(fn, plb); retval = dispatch(fn, plb);
ss.set_stack(prev_stack); ss.set_stack(prev_stack);
} }
catch(const dispatchkit::dispatch_error &e){ catch(const dispatch_error &e){
ss.set_stack(prev_stack); ss.set_stack(prev_stack);
throw Eval_Error(std::string(e.what()) + " with function '" + fun_name + "'", node->children[i]); throw Eval_Error(std::string(e.what()) + " with function '" + fun_name + "'", node->children[i]);
} }
@@ -502,16 +502,16 @@ namespace chaiscript
* Evaluates an if/elseif/else block * Evaluates an if/elseif/else block
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_if(Eval_System &ss, TokenPtr node) { Boxed_Value eval_if(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
unsigned int i; unsigned int i;
retval = eval_token(ss, node->children[0]); retval = eval_token(ss, node->children[0]);
bool cond; bool cond;
try { try {
cond = dispatchkit::boxed_cast<bool &>(retval); cond = boxed_cast<bool &>(retval);
} }
catch (const dispatchkit::bad_boxed_cast &) { catch (const bad_boxed_cast &) {
throw Eval_Error("If condition not boolean", node->children[0]); throw Eval_Error("If condition not boolean", node->children[0]);
} }
if (cond) { if (cond) {
@@ -528,9 +528,9 @@ namespace chaiscript
else if (node->children[i]->text == "else if") { else if (node->children[i]->text == "else if") {
retval = eval_token(ss, node->children[i+1]); retval = eval_token(ss, node->children[i+1]);
try { try {
cond = dispatchkit::boxed_cast<bool &>(retval); cond = boxed_cast<bool &>(retval);
} }
catch (const dispatchkit::bad_boxed_cast &) { catch (const bad_boxed_cast &) {
throw Eval_Error("'else if' condition not boolean", node->children[i+1]); throw Eval_Error("'else if' condition not boolean", node->children[i+1]);
} }
if (cond) { if (cond) {
@@ -549,21 +549,21 @@ namespace chaiscript
* Evaluates a while block * Evaluates a while block
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_while(Eval_System &ss, TokenPtr node) { Boxed_Value eval_while(Eval_System &ss, TokenPtr node) {
bool cond; bool cond;
try { try {
cond = dispatchkit::boxed_cast<bool &>(eval_token(ss, node->children[0])); cond = boxed_cast<bool &>(eval_token(ss, node->children[0]));
} }
catch (const dispatchkit::bad_boxed_cast &) { catch (const bad_boxed_cast &) {
throw Eval_Error("While condition not boolean", node->children[0]); throw Eval_Error("While condition not boolean", node->children[0]);
} }
while (cond) { while (cond) {
try { try {
eval_token(ss, node->children[1]); eval_token(ss, node->children[1]);
try { try {
cond = dispatchkit::boxed_cast<bool &>(eval_token(ss, node->children[0])); cond = boxed_cast<bool &>(eval_token(ss, node->children[0]));
} }
catch (const dispatchkit::bad_boxed_cast &) { catch (const bad_boxed_cast &) {
throw Eval_Error("While condition not boolean", node->children[0]); throw Eval_Error("While condition not boolean", node->children[0]);
} }
} }
@@ -571,26 +571,26 @@ namespace chaiscript
cond = false; cond = false;
} }
} }
return dispatchkit::Boxed_Value(); return Boxed_Value();
} }
/** /**
* Evaluates a for block, including the for's conditions, from left to right * Evaluates a for block, including the for's conditions, from left to right
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_for(Eval_System &ss, TokenPtr node) { Boxed_Value eval_for(Eval_System &ss, TokenPtr node) {
bool cond; bool cond;
try { try {
if (node->children.size() == 4) { if (node->children.size() == 4) {
eval_token(ss, node->children[0]); eval_token(ss, node->children[0]);
cond = dispatchkit::boxed_cast<bool &>(eval_token(ss, node->children[1])); cond = boxed_cast<bool &>(eval_token(ss, node->children[1]));
} }
else { else {
cond = dispatchkit::boxed_cast<bool &>(eval_token(ss, node->children[0])); cond = boxed_cast<bool &>(eval_token(ss, node->children[0]));
} }
} }
catch (const dispatchkit::bad_boxed_cast &) { catch (const bad_boxed_cast &) {
throw Eval_Error("For condition not boolean", node); throw Eval_Error("For condition not boolean", node);
} }
while (cond) { while (cond) {
@@ -598,35 +598,35 @@ namespace chaiscript
if (node->children.size() == 4) { if (node->children.size() == 4) {
eval_token(ss, node->children[3]); eval_token(ss, node->children[3]);
eval_token(ss, node->children[2]); eval_token(ss, node->children[2]);
cond = dispatchkit::boxed_cast<bool &>(eval_token(ss, node->children[1])); cond = boxed_cast<bool &>(eval_token(ss, node->children[1]));
} }
else { else {
eval_token(ss, node->children[2]); eval_token(ss, node->children[2]);
eval_token(ss, node->children[1]); eval_token(ss, node->children[1]);
cond = dispatchkit::boxed_cast<bool &>(eval_token(ss, node->children[0])); cond = boxed_cast<bool &>(eval_token(ss, node->children[0]));
} }
} }
catch (const dispatchkit::bad_boxed_cast &) { catch (const bad_boxed_cast &) {
throw Eval_Error("For condition not boolean", node); throw Eval_Error("For condition not boolean", node);
} }
catch (Break_Loop &) { catch (Break_Loop &) {
cond = false; cond = false;
} }
} }
return dispatchkit::Boxed_Value(); return Boxed_Value();
} }
/** /**
* Evaluates a function definition * Evaluates a function definition
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_def(Eval_System &ss, TokenPtr node) { Boxed_Value eval_def(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
unsigned int i; unsigned int i;
std::vector<std::string> param_names; std::vector<std::string> param_names;
std::string annotation = node->annotation?node->annotation->text:""; std::string annotation = node->annotation?node->annotation->text:"";
boost::shared_ptr<dispatchkit::Dynamic_Proxy_Function> guard; boost::shared_ptr<Dynamic_Proxy_Function> guard;
size_t numparams = 0; size_t numparams = 0;
std::string function_name = node->children[0]->text; std::string function_name = node->children[0]->text;
TokenPtr guardnode; TokenPtr guardnode;
@@ -651,14 +651,14 @@ namespace chaiscript
} }
if (guardnode) { if (guardnode) {
guard = boost::shared_ptr<dispatchkit::Dynamic_Proxy_Function> guard = boost::shared_ptr<Dynamic_Proxy_Function>
(new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, (new Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>,
boost::ref(ss), guardnode, boost::ref(ss), guardnode,
param_names, _1), numparams)); param_names, _1), numparams));
} }
ss.register_function(boost::shared_ptr<dispatchkit::Proxy_Function> ss.add(Proxy_Function
(new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, (new Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>,
boost::ref(ss), node->children.back(), boost::ref(ss), node->children.back(),
param_names, _1), numparams, param_names, _1), numparams,
annotation, guard)), function_name); annotation, guard)), function_name);
@@ -670,8 +670,8 @@ namespace chaiscript
* Evaluates a lambda (anonymous function) * Evaluates a lambda (anonymous function)
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_lambda(Eval_System &ss, TokenPtr node) { Boxed_Value eval_lambda(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
unsigned int i; unsigned int i;
std::vector<std::string> param_names; std::vector<std::string> param_names;
@@ -689,8 +689,8 @@ namespace chaiscript
numparams = 0; numparams = 0;
} }
return dispatchkit::Boxed_Value(boost::shared_ptr<dispatchkit::Proxy_Function>( return Boxed_Value(Proxy_Function(
new dispatchkit::Dynamic_Proxy_Function( new Dynamic_Proxy_Function(
boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), numparams))); boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), numparams)));
} }
@@ -698,8 +698,8 @@ namespace chaiscript
* Evaluates a scoped block. Handles resetting the scope after the block has completed. * Evaluates a scoped block. Handles resetting the scope after the block has completed.
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_block(Eval_System &ss, TokenPtr node) { Boxed_Value eval_block(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
unsigned int i; unsigned int i;
ss.new_scope(); ss.new_scope();
@@ -726,13 +726,13 @@ namespace chaiscript
* Evaluates a return statement * Evaluates a return statement
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_return(Eval_System &ss, TokenPtr node) { Boxed_Value eval_return(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval; Boxed_Value retval;
if (node->children.size() > 0) { if (node->children.size() > 0) {
retval = eval_token(ss, node->children[0]); retval = eval_token(ss, node->children[0]);
} }
else { else {
retval = dispatchkit::Boxed_Value(); retval = Boxed_Value();
} }
throw Return_Value(retval, node); throw Return_Value(retval, node);
} }
@@ -741,7 +741,7 @@ namespace chaiscript
* Evaluates a break statement * Evaluates a break statement
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_break(Eval_System &, TokenPtr node) { Boxed_Value eval_break(Eval_System &, TokenPtr node) {
throw Break_Loop(node); throw Break_Loop(node);
} }
@@ -749,7 +749,7 @@ namespace chaiscript
* Top-level evaluation dispatch for all AST node types * Top-level evaluation dispatch for all AST node types
*/ */
template <typename Eval_System> template <typename Eval_System>
dispatchkit::Boxed_Value eval_token(Eval_System &ss, TokenPtr node) { Boxed_Value eval_token(Eval_System &ss, TokenPtr node) {
switch (node->identifier) { switch (node->identifier) {
case (Token_Type::File) : case (Token_Type::File) :
return eval_file(ss, node); return eval_file(ss, node);
@@ -862,7 +862,7 @@ namespace chaiscript
break; break;
default : default :
return dispatchkit::Boxed_Value(); return Boxed_Value();
} }
} }
} }

View File

@@ -27,9 +27,9 @@ struct System
std::map<std::string, boost::function<std::string (const std::string &) > > m_callbacks; std::map<std::string, boost::function<std::string (const std::string &) > > m_callbacks;
void add_callback(const std::string &t_name, void add_callback(const std::string &t_name,
boost::shared_ptr<dispatchkit::Proxy_Function> t_func) const chaiscript::Proxy_Function &t_func)
{ {
m_callbacks[t_name] = dispatchkit::build_function_caller<std::string (const std::string &)>(t_func); m_callbacks[t_name] = chaiscript::functor<std::string (const std::string &)>(t_func);
} }
@@ -47,17 +47,17 @@ struct System
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
using namespace chaiscript;
ChaiScript_Engine chai;
chaiscript::ChaiScript_Engine chai;
//Create a new system object and share it with the chaiscript engine //Create a new system object and share it with the chaiscript engine
System system; System system;
chai.get_eval_engine().add_object("system", boost::ref(system)); chai.add(var(&system), "system");
//Register the two methods of the System structure. //Register the two methods of the System structure.
dispatchkit::register_function(chai.get_eval_engine(), &System::add_callback, "add_callback"); chai.add(fun(&System::add_callback), "add_callback");
dispatchkit::register_function(chai.get_eval_engine(), &System::do_callbacks, "do_callbacks"); chai.add(fun(&System::do_callbacks), "do_callbacks");
// Let's use chaiscript to add a new lambda callback to our system. // Let's use chaiscript to add a new lambda callback to our system.
// The function "{ 'Callback1' + x }" is created in chaiscript and passed into our C++ application // The function "{ 'Callback1' + x }" is created in chaiscript and passed into our C++ application
@@ -75,18 +75,20 @@ int main(int argc, char *argv[]) {
// then cast your function to that typedef. // then cast your function to that typedef.
typedef void (*PlainLog)(const std::string &); typedef void (*PlainLog)(const std::string &);
typedef void (*ModuleLog)(const std::string &, const std::string &); typedef void (*ModuleLog)(const std::string &, const std::string &);
dispatchkit::register_function(chai.get_eval_engine(), PlainLog(&log), "log"); chai.add(fun(PlainLog(&log)), "log");
dispatchkit::register_function(chai.get_eval_engine(), ModuleLog(&log), "log"); chai.add(fun(ModuleLog(&log)), "log");
chai.evaluate_string("log('Test Message')"); chai.evaluate_string("log('Test Message')");
chai.evaluate_string("log('Test Module', 'Test Message');"); chai.evaluate_string("log('Test Module', 'Test Message');");
//Finally, it is possible to register any boost::function as a system function, in this //Finally, it is possible to register any boost::function as a system function, in this
//way, we can, for instance add a bound member function to the system //way, we can, for instance add a bound member function to the system
chai.get_eval_engine().register_function(boost::function<void ()>(boost::bind(&System::do_callbacks, boost::ref(system), "Bound Test")), "do_callbacks"); chai.add(fun(boost::function<void ()>(boost::bind(&System::do_callbacks, boost::ref(system), "Bound Test"))), "do_callbacks");
//Call bound version of do_callbacks //Call bound version of do_callbacks
chai.evaluate_string("do_callbacks()"); chai.evaluate_string("do_callbacks()");
boost::function<void ()> caller = chai.functor<void ()>("fun() { system.do_callbacks(\"From Functor\"); }");
caller();
} }

View File

@@ -24,7 +24,7 @@ int main(int argc, char *argv[]) {
std::getline(std::cin, input); std::getline(std::cin, input);
while (input != "quit") { while (input != "quit") {
dispatchkit::Boxed_Value val; chaiscript::Boxed_Value val;
if (input == "help") { if (input == "help") {
print_help(); print_help();
@@ -37,7 +37,7 @@ int main(int argc, char *argv[]) {
//Then, we try to print the result of the evaluation to the user //Then, we try to print the result of the evaluation to the user
if (val.get_type_info().m_bare_type_info && *(val.get_type_info().m_bare_type_info) != typeid(void)) { if (val.get_type_info().m_bare_type_info && *(val.get_type_info().m_bare_type_info) != typeid(void)) {
try { try {
dispatchkit::dispatch(chai.get_eval_engine().get_function("print"), dispatchkit::Param_List_Builder() << val); chaiscript::dispatch(chai.get_eval_engine().get_function("print"), chaiscript::Param_List_Builder() << val);
} }
catch (...) { catch (...) {
//If we can't, do nothing //If we can't, do nothing
@@ -57,7 +57,7 @@ int main(int argc, char *argv[]) {
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
std::string filename(argv[i]); std::string filename(argv[i]);
try { try {
dispatchkit::Boxed_Value val = chai.evaluate_file(argv[i]); chaiscript::Boxed_Value val = chai.evaluate_file(argv[i]);
} }
catch (std::exception &e) { catch (std::exception &e) {
std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;