Merge pull request #262 from rollbear/develop

Pass Module by reference instead of shared ptr
This commit is contained in:
Jason Turner 2016-04-11 16:58:27 -06:00
commit e57f11fcf4
9 changed files with 402 additions and 371 deletions

View File

@ -40,19 +40,20 @@ namespace chaiscript
{ {
using namespace bootstrap; using namespace bootstrap;
ModulePtr lib = Bootstrap::bootstrap(); auto lib = std::make_shared<Module>();
Bootstrap::bootstrap(*lib);
lib->add(standard_library::vector_type<std::vector<Boxed_Value> >("Vector")); standard_library::vector_type<std::vector<Boxed_Value> >("Vector", *lib);
lib->add(standard_library::string_type<std::string>("string")); standard_library::string_type<std::string>("string", *lib);
lib->add(standard_library::map_type<std::map<std::string, Boxed_Value> >("Map")); standard_library::map_type<std::map<std::string, Boxed_Value> >("Map", *lib);
lib->add(standard_library::pair_type<std::pair<Boxed_Value, Boxed_Value > >("Pair")); standard_library::pair_type<std::pair<Boxed_Value, Boxed_Value > >("Pair", *lib);
#ifndef CHAISCRIPT_NO_THREADS #ifndef CHAISCRIPT_NO_THREADS
lib->add(standard_library::future_type<std::future<chaiscript::Boxed_Value>>("future")); standard_library::future_type<std::future<chaiscript::Boxed_Value>>("future", *lib);
lib->add(chaiscript::fun([](const std::function<chaiscript::Boxed_Value ()> &t_func){ return std::async(std::launch::async, t_func);}), "async"); lib->add(chaiscript::fun([](const std::function<chaiscript::Boxed_Value ()> &t_func){ return std::async(std::launch::async, t_func);}), "async");
#endif #endif
lib->add(json_wrap::library()); json_wrap::library(*lib);
lib->eval(ChaiScript_Prelude::chaiscript_prelude() /*, "standard prelude"*/ ); lib->eval(ChaiScript_Prelude::chaiscript_prelude() /*, "standard prelude"*/ );

View File

@ -52,12 +52,12 @@ namespace chaiscript
} }
template<typename T, typename = typename std::enable_if<std::is_array<T>::value>::type > template<typename T, typename = typename std::enable_if<std::is_array<T>::value>::type >
ModulePtr array(const std::string &type, ModulePtr m = std::make_shared<Module>()) void array(const std::string &type, Module& m)
{ {
typedef typename std::remove_extent<T>::type ReturnType; typedef typename std::remove_extent<T>::type ReturnType;
const auto extent = std::extent<T>::value; const auto extent = std::extent<T>::value;
m->add(user_type<T>(), type); m.add(user_type<T>(), type);
m->add(fun( m.add(fun(
[extent](T& t, size_t index)->ReturnType &{ [extent](T& t, size_t index)->ReturnType &{
if (extent > 0 && index >= extent) { if (extent > 0 && index >= extent) {
throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent)); throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent));
@ -68,7 +68,7 @@ namespace chaiscript
), "[]" ), "[]"
); );
m->add(fun( m.add(fun(
[extent](const T &t, size_t index)->const ReturnType &{ [extent](const T &t, size_t index)->const ReturnType &{
if (extent > 0 && index >= extent) { if (extent > 0 && index >= extent) {
throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent)); throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent));
@ -79,33 +79,29 @@ namespace chaiscript
), "[]" ), "[]"
); );
m->add(fun( m.add(fun(
[extent](const T &) { [extent](const T &) {
return extent; return extent;
}), "size"); }), "size");
return m;
} }
/// \brief Adds a copy constructor for the given type to the given Model /// \brief Adds a copy constructor for the given type to the given Model
/// \param[in] type The name of the type. The copy constructor will be named "type". /// \param[in] type The name of the type. The copy constructor will be named "type".
/// \param[in,out] m The Module to add the copy constructor to /// \param[in,out] m The Module to add the copy constructor to
/// \tparam T The type to add a copy constructor for /// \tparam T The type to add a copy constructor for
/// \returns The passed in ModulePtr, or the newly constructed one if the default param is used /// \returns The passed in Module
template<typename T> template<typename T>
ModulePtr copy_constructor(const std::string &type, ModulePtr m = std::make_shared<Module>()) void copy_constructor(const std::string &type, Module& m)
{ {
m->add(constructor<T (const T &)>(), type); m.add(constructor<T (const T &)>(), type);
return m;
} }
/// \brief Add all comparison operators for the templated type. Used during bootstrap, also available to users. /// \brief Add all comparison operators for the templated type. Used during bootstrap, also available to users.
/// \tparam T Type to create comparison operators for /// \tparam T Type to create comparison operators for
/// \param[in,out] m module to add comparison operators to /// \param[in,out] m module to add comparison operators to
/// \returns the passed in ModulePtr or the newly constructed one if the default params are used. /// \returns the passed in Module.
template<typename T> template<typename T>
ModulePtr opers_comparison(ModulePtr m = std::make_shared<Module>()) void opers_comparison(Module& m)
{ {
operators::equal<T>(m); operators::equal<T>(m);
operators::greater_than<T>(m); operators::greater_than<T>(m);
@ -113,7 +109,6 @@ namespace chaiscript
operators::less_than<T>(m); operators::less_than<T>(m);
operators::less_than_equal<T>(m); operators::less_than_equal<T>(m);
operators::not_equal<T>(m); operators::not_equal<T>(m);
return m;
} }
@ -122,15 +117,14 @@ namespace chaiscript
/// \param[in] type The name of the type to add the constructors for. /// \param[in] type The name of the type to add the constructors for.
/// \param[in,out] m The Module to add the basic constructors to /// \param[in,out] m The Module to add the basic constructors to
/// \tparam T Type to generate basic constructors for /// \tparam T Type to generate basic constructors for
/// \returns The passed in ModulePtr, or the newly constructed one if the default param is used /// \returns The passed in Module
/// \sa copy_constructor /// \sa copy_constructor
/// \sa constructor /// \sa constructor
template<typename T> template<typename T>
ModulePtr basic_constructors(const std::string &type, ModulePtr m = std::make_shared<Module>()) void basic_constructors(const std::string &type, Module& m)
{ {
m->add(constructor<T ()>(), type); m.add(constructor<T ()>(), type);
copy_constructor<T>(type, m); copy_constructor<T>(type, m);
return m;
} }
/// \brief Adds a constructor for a POD type /// \brief Adds a constructor for a POD type
@ -138,10 +132,9 @@ namespace chaiscript
/// \param[in] type The name of the type /// \param[in] type The name of the type
/// \param[in,out] m The Module to add the constructor to /// \param[in,out] m The Module to add the constructor to
template<typename T> template<typename T>
ModulePtr construct_pod(const std::string &type, ModulePtr m = std::make_shared<Module>()) void construct_pod(const std::string &type, Module& m)
{ {
m->add(fun(&detail::construct_pod<T>), type); m.add(fun(&detail::construct_pod<T>), type);
return m;
} }
@ -185,14 +178,13 @@ namespace chaiscript
/// Add all common functions for a POD type. All operators, and /// Add all common functions for a POD type. All operators, and
/// common conversions /// common conversions
template<typename T> template<typename T>
ModulePtr bootstrap_pod_type(const std::string &name, ModulePtr m = std::make_shared<Module>()) void bootstrap_pod_type(const std::string &name, Module& m)
{ {
m->add(user_type<T>(), name); m.add(user_type<T>(), name);
m->add(constructor<T()>(), name); m.add(constructor<T()>(), name);
construct_pod<T>(name, m); construct_pod<T>(name, m);
m->add(fun(&parse_string<T>), "to_" + name); m.add(fun(&parse_string<T>), "to_" + name);
return m;
} }
@ -261,41 +253,41 @@ namespace chaiscript
/// Add all arithmetic operators for PODs /// Add all arithmetic operators for PODs
static void opers_arithmetic_pod(ModulePtr m = std::make_shared<Module>()) static void opers_arithmetic_pod(Module& m)
{ {
m->add(fun(&Boxed_Number::equals), "=="); m.add(fun(&Boxed_Number::equals), "==");
m->add(fun(&Boxed_Number::less_than), "<"); m.add(fun(&Boxed_Number::less_than), "<");
m->add(fun(&Boxed_Number::greater_than), ">"); m.add(fun(&Boxed_Number::greater_than), ">");
m->add(fun(&Boxed_Number::greater_than_equal), ">="); m.add(fun(&Boxed_Number::greater_than_equal), ">=");
m->add(fun(&Boxed_Number::less_than_equal), "<="); m.add(fun(&Boxed_Number::less_than_equal), "<=");
m->add(fun(&Boxed_Number::not_equal), "!="); m.add(fun(&Boxed_Number::not_equal), "!=");
m->add(fun(&Boxed_Number::pre_decrement), "--"); m.add(fun(&Boxed_Number::pre_decrement), "--");
m->add(fun(&Boxed_Number::pre_increment), "++"); m.add(fun(&Boxed_Number::pre_increment), "++");
m->add(fun(&Boxed_Number::sum), "+"); m.add(fun(&Boxed_Number::sum), "+");
m->add(fun(&Boxed_Number::unary_plus), "+"); m.add(fun(&Boxed_Number::unary_plus), "+");
m->add(fun(&Boxed_Number::unary_minus), "-"); m.add(fun(&Boxed_Number::unary_minus), "-");
m->add(fun(&Boxed_Number::difference), "-"); m.add(fun(&Boxed_Number::difference), "-");
m->add(fun(&Boxed_Number::assign_bitwise_and), "&="); m.add(fun(&Boxed_Number::assign_bitwise_and), "&=");
m->add(fun(&Boxed_Number::assign), "="); m.add(fun(&Boxed_Number::assign), "=");
m->add(fun(&Boxed_Number::assign_bitwise_or), "|="); m.add(fun(&Boxed_Number::assign_bitwise_or), "|=");
m->add(fun(&Boxed_Number::assign_bitwise_xor), "^="); m.add(fun(&Boxed_Number::assign_bitwise_xor), "^=");
m->add(fun(&Boxed_Number::assign_remainder), "%="); m.add(fun(&Boxed_Number::assign_remainder), "%=");
m->add(fun(&Boxed_Number::assign_shift_left), "<<="); m.add(fun(&Boxed_Number::assign_shift_left), "<<=");
m->add(fun(&Boxed_Number::assign_shift_right), ">>="); m.add(fun(&Boxed_Number::assign_shift_right), ">>=");
m->add(fun(&Boxed_Number::bitwise_and), "&"); m.add(fun(&Boxed_Number::bitwise_and), "&");
m->add(fun(&Boxed_Number::bitwise_complement), "~"); m.add(fun(&Boxed_Number::bitwise_complement), "~");
m->add(fun(&Boxed_Number::bitwise_xor), "^"); m.add(fun(&Boxed_Number::bitwise_xor), "^");
m->add(fun(&Boxed_Number::bitwise_or), "|"); m.add(fun(&Boxed_Number::bitwise_or), "|");
m->add(fun(&Boxed_Number::assign_product), "*="); m.add(fun(&Boxed_Number::assign_product), "*=");
m->add(fun(&Boxed_Number::assign_quotient), "/="); m.add(fun(&Boxed_Number::assign_quotient), "/=");
m->add(fun(&Boxed_Number::assign_sum), "+="); m.add(fun(&Boxed_Number::assign_sum), "+=");
m->add(fun(&Boxed_Number::assign_difference), "-="); m.add(fun(&Boxed_Number::assign_difference), "-=");
m->add(fun(&Boxed_Number::quotient), "/"); m.add(fun(&Boxed_Number::quotient), "/");
m->add(fun(&Boxed_Number::shift_left), "<<"); m.add(fun(&Boxed_Number::shift_left), "<<");
m->add(fun(&Boxed_Number::product), "*"); m.add(fun(&Boxed_Number::product), "*");
m->add(fun(&Boxed_Number::remainder), "%"); m.add(fun(&Boxed_Number::remainder), "%");
m->add(fun(&Boxed_Number::shift_right), ">>"); m.add(fun(&Boxed_Number::shift_right), ">>");
} }
@ -403,57 +395,57 @@ namespace chaiscript
public: public:
/// \brief perform all common bootstrap functions for std::string, void and POD types /// \brief perform all common bootstrap functions for std::string, void and POD types
/// \param[in,out] m Module to add bootstrapped functions to /// \param[in,out] m Module to add bootstrapped functions to
/// \returns passed in ModulePtr, or newly created one if default argument is used /// \returns passed in Module
static ModulePtr bootstrap(ModulePtr m = std::make_shared<Module>()) static void bootstrap(Module& m)
{ {
m->add(user_type<void>(), "void"); m.add(user_type<void>(), "void");
m->add(user_type<bool>(), "bool"); m.add(user_type<bool>(), "bool");
m->add(user_type<Boxed_Value>(), "Object"); m.add(user_type<Boxed_Value>(), "Object");
m->add(user_type<Boxed_Number>(), "Number"); m.add(user_type<Boxed_Number>(), "Number");
m->add(user_type<Proxy_Function>(), "Function"); m.add(user_type<Proxy_Function>(), "Function");
m->add(user_type<dispatch::Assignable_Proxy_Function>(), "Assignable_Function"); m.add(user_type<dispatch::Assignable_Proxy_Function>(), "Assignable_Function");
m->add(user_type<std::exception>(), "exception"); m.add(user_type<std::exception>(), "exception");
m->add(fun(&dispatch::Proxy_Function_Base::get_arity), "get_arity"); m.add(fun(&dispatch::Proxy_Function_Base::get_arity), "get_arity");
m->add(fun(&dispatch::Proxy_Function_Base::annotation), "get_annotation"); m.add(fun(&dispatch::Proxy_Function_Base::annotation), "get_annotation");
m->add(fun(&dispatch::Proxy_Function_Base::operator==), "=="); m.add(fun(&dispatch::Proxy_Function_Base::operator==), "==");
m->add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_param_types)), "get_param_types"); m.add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_param_types)), "get_param_types");
m->add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_contained_functions)), "get_contained_functions"); m.add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_contained_functions)), "get_contained_functions");
m->add(user_type<std::out_of_range>(), "out_of_range"); m.add(user_type<std::out_of_range>(), "out_of_range");
m->add(user_type<std::logic_error>(), "logic_error"); m.add(user_type<std::logic_error>(), "logic_error");
m->add(chaiscript::base_class<std::exception, std::logic_error>()); m.add(chaiscript::base_class<std::exception, std::logic_error>());
m->add(chaiscript::base_class<std::logic_error, std::out_of_range>()); m.add(chaiscript::base_class<std::logic_error, std::out_of_range>());
m->add(chaiscript::base_class<std::exception, std::out_of_range>()); m.add(chaiscript::base_class<std::exception, std::out_of_range>());
m->add(user_type<std::runtime_error>(), "runtime_error"); m.add(user_type<std::runtime_error>(), "runtime_error");
m->add(chaiscript::base_class<std::exception, std::runtime_error>()); m.add(chaiscript::base_class<std::exception, std::runtime_error>());
m->add(constructor<std::runtime_error (const std::string &)>(), "runtime_error"); m.add(constructor<std::runtime_error (const std::string &)>(), "runtime_error");
m->add(fun(std::function<std::string (const std::runtime_error &)>(&what)), "what"); m.add(fun(std::function<std::string (const std::runtime_error &)>(&what)), "what");
m->add(user_type<dispatch::Dynamic_Object>(), "Dynamic_Object"); m.add(user_type<dispatch::Dynamic_Object>(), "Dynamic_Object");
m->add(constructor<dispatch::Dynamic_Object (const std::string &)>(), "Dynamic_Object"); m.add(constructor<dispatch::Dynamic_Object (const std::string &)>(), "Dynamic_Object");
m->add(constructor<dispatch::Dynamic_Object ()>(), "Dynamic_Object"); m.add(constructor<dispatch::Dynamic_Object ()>(), "Dynamic_Object");
m->add(fun(&dispatch::Dynamic_Object::get_type_name), "get_type_name"); m.add(fun(&dispatch::Dynamic_Object::get_type_name), "get_type_name");
m->add(fun(&dispatch::Dynamic_Object::get_attrs), "get_attrs"); m.add(fun(&dispatch::Dynamic_Object::get_attrs), "get_attrs");
m->add(fun(&dispatch::Dynamic_Object::set_explicit), "set_explicit"); m.add(fun(&dispatch::Dynamic_Object::set_explicit), "set_explicit");
m->add(fun(&dispatch::Dynamic_Object::is_explicit), "is_explicit"); m.add(fun(&dispatch::Dynamic_Object::is_explicit), "is_explicit");
m->add(fun(&dispatch::Dynamic_Object::has_attr), "has_attr"); m.add(fun(&dispatch::Dynamic_Object::has_attr), "has_attr");
m->add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::get_attr)), "get_attr"); m.add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::get_attr)), "get_attr");
m->add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::get_attr)), "get_attr"); m.add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::get_attr)), "get_attr");
m->add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::method_missing)), "method_missing"); m.add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::method_missing)), "method_missing");
m->add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::method_missing)), "method_missing"); m.add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::method_missing)), "method_missing");
m->add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::get_attr)), "[]"); m.add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::get_attr)), "[]");
m->add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::get_attr)), "[]"); m.add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::get_attr)), "[]");
m->eval(R"chaiscript( m.eval(R"chaiscript(
def Dynamic_Object::clone() { def Dynamic_Object::clone() {
auto &new_o = Dynamic_Object(this.get_type_name()); auto &new_o = Dynamic_Object(this.get_type_name());
for_each(this.get_attrs(), fun[new_o](x) { new_o.get_attr(x.first) = x.second; } ); for_each(this.get_attrs(), fun[new_o](x) { new_o.get_attr(x.first) = x.second; } );
@ -490,37 +482,37 @@ namespace chaiscript
} }
)chaiscript"); )chaiscript");
m->add(fun(&has_guard), "has_guard"); m.add(fun(&has_guard), "has_guard");
m->add(fun(&get_guard), "get_guard"); m.add(fun(&get_guard), "get_guard");
m->add(fun(&Boxed_Value::is_undef), "is_var_undef"); m.add(fun(&Boxed_Value::is_undef), "is_var_undef");
m->add(fun(&Boxed_Value::is_null), "is_var_null"); m.add(fun(&Boxed_Value::is_null), "is_var_null");
m->add(fun(&Boxed_Value::is_const), "is_var_const"); m.add(fun(&Boxed_Value::is_const), "is_var_const");
m->add(fun(&Boxed_Value::is_ref), "is_var_reference"); m.add(fun(&Boxed_Value::is_ref), "is_var_reference");
m->add(fun(&Boxed_Value::is_pointer), "is_var_pointer"); m.add(fun(&Boxed_Value::is_pointer), "is_var_pointer");
m->add(fun(&Boxed_Value::is_return_value), "is_var_return_value"); m.add(fun(&Boxed_Value::is_return_value), "is_var_return_value");
m->add(fun(&Boxed_Value::reset_return_value), "reset_var_return_value"); m.add(fun(&Boxed_Value::reset_return_value), "reset_var_return_value");
m->add(fun(&Boxed_Value::is_type), "is_type"); m.add(fun(&Boxed_Value::is_type), "is_type");
m->add(fun(&Boxed_Value::get_attr), "get_var_attr"); m.add(fun(&Boxed_Value::get_attr), "get_var_attr");
m->add(fun(&Boxed_Value::copy_attrs), "copy_var_attrs"); m.add(fun(&Boxed_Value::copy_attrs), "copy_var_attrs");
m->add(fun(&Boxed_Value::clone_attrs), "clone_var_attrs"); m.add(fun(&Boxed_Value::clone_attrs), "clone_var_attrs");
m->add(fun(&Boxed_Value::get_type_info), "get_type_info"); m.add(fun(&Boxed_Value::get_type_info), "get_type_info");
m->add(user_type<Type_Info>(), "Type_Info"); m.add(user_type<Type_Info>(), "Type_Info");
m->add(constructor<Type_Info (const Type_Info &)>(), "Type_Info"); m.add(constructor<Type_Info (const Type_Info &)>(), "Type_Info");
operators::equal<Type_Info>(m); operators::equal<Type_Info>(m);
m->add(fun(&Type_Info::is_const), "is_type_const"); m.add(fun(&Type_Info::is_const), "is_type_const");
m->add(fun(&Type_Info::is_reference), "is_type_reference"); m.add(fun(&Type_Info::is_reference), "is_type_reference");
m->add(fun(&Type_Info::is_void), "is_type_void"); m.add(fun(&Type_Info::is_void), "is_type_void");
m->add(fun(&Type_Info::is_undef), "is_type_undef"); m.add(fun(&Type_Info::is_undef), "is_type_undef");
m->add(fun(&Type_Info::is_pointer), "is_type_pointer"); m.add(fun(&Type_Info::is_pointer), "is_type_pointer");
m->add(fun(&Type_Info::is_arithmetic), "is_type_arithmetic"); m.add(fun(&Type_Info::is_arithmetic), "is_type_arithmetic");
m->add(fun(&Type_Info::name), "cpp_name"); m.add(fun(&Type_Info::name), "cpp_name");
m->add(fun(&Type_Info::bare_name), "cpp_bare_name"); m.add(fun(&Type_Info::bare_name), "cpp_bare_name");
m->add(fun(&Type_Info::bare_equal), "bare_equal"); m.add(fun(&Type_Info::bare_equal), "bare_equal");
basic_constructors<bool>("bool", m); basic_constructors<bool>("bool", m);
@ -528,14 +520,14 @@ namespace chaiscript
operators::equal<bool>(m); operators::equal<bool>(m);
operators::not_equal<bool>(m); operators::not_equal<bool>(m);
m->add(fun([](const std::string &s) -> std::string { return s; }), "to_string"); m.add(fun([](const std::string &s) -> std::string { return s; }), "to_string");
m->add(fun(&Bootstrap::bool_to_string), "to_string"); m.add(fun(&Bootstrap::bool_to_string), "to_string");
m->add(fun(&unknown_assign), "="); m.add(fun(&unknown_assign), "=");
m->add(fun(&throw_exception), "throw"); m.add(fun(&throw_exception), "throw");
m->add(fun(&what), "what"); m.add(fun(&what), "what");
m->add(fun(&to_string<char>), "to_string"); m.add(fun(&to_string<char>), "to_string");
m->add(fun(&Boxed_Number::to_string), "to_string"); m.add(fun(&Boxed_Number::to_string), "to_string");
bootstrap_pod_type<double>("double", m); bootstrap_pod_type<double>("double", m);
bootstrap_pod_type<long double>("long_double", m); bootstrap_pod_type<long double>("long_double", m);
@ -565,38 +557,38 @@ namespace chaiscript
opers_arithmetic_pod(m); opers_arithmetic_pod(m);
m->add(fun(&print), "print_string"); m.add(fun(&print), "print_string");
m->add(fun(&println), "println_string"); m.add(fun(&println), "println_string");
m->add(dispatch::make_dynamic_proxy_function(&bind_function), "bind"); m.add(dispatch::make_dynamic_proxy_function(&bind_function), "bind");
m->add(fun(&shared_ptr_unconst_clone<dispatch::Proxy_Function_Base>), "clone"); m.add(fun(&shared_ptr_unconst_clone<dispatch::Proxy_Function_Base>), "clone");
m->add(fun(&ptr_assign<std::remove_const<dispatch::Proxy_Function_Base>::type>), "="); m.add(fun(&ptr_assign<std::remove_const<dispatch::Proxy_Function_Base>::type>), "=");
m->add(fun(&ptr_assign<std::add_const<dispatch::Proxy_Function_Base>::type>), "="); m.add(fun(&ptr_assign<std::add_const<dispatch::Proxy_Function_Base>::type>), "=");
m->add(chaiscript::base_class<dispatch::Proxy_Function_Base, dispatch::Assignable_Proxy_Function>()); m.add(chaiscript::base_class<dispatch::Proxy_Function_Base, dispatch::Assignable_Proxy_Function>());
m->add(fun( m.add(fun(
[](dispatch::Assignable_Proxy_Function &t_lhs, const std::shared_ptr<const dispatch::Proxy_Function_Base> &t_rhs) { [](dispatch::Assignable_Proxy_Function &t_lhs, const std::shared_ptr<const dispatch::Proxy_Function_Base> &t_rhs) {
t_lhs.assign(t_rhs); t_lhs.assign(t_rhs);
} }
), "=" ), "="
); );
m->add(fun(&Boxed_Value::type_match), "type_match"); m.add(fun(&Boxed_Value::type_match), "type_match");
m->add(chaiscript::fun(&has_parse_tree), "has_parse_tree"); m.add(chaiscript::fun(&has_parse_tree), "has_parse_tree");
m->add(chaiscript::fun(&get_parse_tree), "get_parse_tree"); m.add(chaiscript::fun(&get_parse_tree), "get_parse_tree");
m->add(chaiscript::base_class<std::runtime_error, chaiscript::exception::eval_error>()); m.add(chaiscript::base_class<std::runtime_error, chaiscript::exception::eval_error>());
m->add(chaiscript::user_type<chaiscript::exception::arithmetic_error>(), "arithmetic_error"); m.add(chaiscript::user_type<chaiscript::exception::arithmetic_error>(), "arithmetic_error");
m->add(chaiscript::base_class<std::runtime_error, chaiscript::exception::arithmetic_error>()); m.add(chaiscript::base_class<std::runtime_error, chaiscript::exception::arithmetic_error>());
// chaiscript::bootstrap::standard_library::vector_type<std::vector<std::shared_ptr<chaiscript::AST_Node> > >("AST_NodeVector", m); // chaiscript::bootstrap::standard_library::vector_type<std::vector<std::shared_ptr<chaiscript::AST_Node> > >("AST_NodeVector", m);
chaiscript::utility::add_class<chaiscript::exception::eval_error>(*m, chaiscript::utility::add_class<chaiscript::exception::eval_error>(m,
"eval_error", "eval_error",
{ }, { },
{ {fun(&chaiscript::exception::eval_error::reason), "reason"}, { {fun(&chaiscript::exception::eval_error::reason), "reason"},
@ -611,7 +603,7 @@ namespace chaiscript
); );
chaiscript::utility::add_class<chaiscript::File_Position>(*m, chaiscript::utility::add_class<chaiscript::File_Position>(m,
"File_Position", "File_Position",
{ constructor<File_Position()>(), { constructor<File_Position()>(),
constructor<File_Position(int, int)>() }, constructor<File_Position(int, int)>() },
@ -620,7 +612,7 @@ namespace chaiscript
); );
chaiscript::utility::add_class<AST_Node>(*m, chaiscript::utility::add_class<AST_Node>(m,
"AST_Node", "AST_Node",
{ }, { },
{ {fun(&AST_Node::text), "text"}, { {fun(&AST_Node::text), "text"},
@ -641,16 +633,12 @@ namespace chaiscript
); );
chaiscript::utility::add_class<parser::ChaiScript_Parser>(*m, chaiscript::utility::add_class<parser::ChaiScript_Parser>(m,
"ChaiScript_Parser", "ChaiScript_Parser",
{ constructor<parser::ChaiScript_Parser ()>() }, { constructor<parser::ChaiScript_Parser ()>() },
{ {fun(&parser::ChaiScript_Parser::parse), "parse"}, { {fun(&parser::ChaiScript_Parser::parse), "parse"},
{fun(&parser::ChaiScript_Parser::ast), "ast"} } {fun(&parser::ChaiScript_Parser::ast), "ast"} }
); );
return m;
} }
}; };
} }

View File

@ -177,22 +177,20 @@ namespace chaiscript
/// Add Bidir_Range support for the given ContainerType /// Add Bidir_Range support for the given ContainerType
template<typename Bidir_Type> template<typename Bidir_Type>
ModulePtr input_range_type_impl(const std::string &type, ModulePtr m = std::make_shared<Module>()) void input_range_type_impl(const std::string &type, Module& m)
{ {
m->add(user_type<Bidir_Type>(), type + "_Range"); m.add(user_type<Bidir_Type>(), type + "_Range");
copy_constructor<Bidir_Type>(type + "_Range", m); copy_constructor<Bidir_Type>(type + "_Range", m);
m->add(constructor<Bidir_Type (typename Bidir_Type::container_type &)>(), "range_internal"); m.add(constructor<Bidir_Type (typename Bidir_Type::container_type &)>(), "range_internal");
m->add(fun(&Bidir_Type::empty), "empty"); m.add(fun(&Bidir_Type::empty), "empty");
m->add(fun(&Bidir_Type::pop_front), "pop_front"); m.add(fun(&Bidir_Type::pop_front), "pop_front");
m->add(fun(&Bidir_Type::front), "front"); m.add(fun(&Bidir_Type::front), "front");
m->add(fun(&Bidir_Type::pop_back), "pop_back"); m.add(fun(&Bidir_Type::pop_back), "pop_back");
m->add(fun(&Bidir_Type::back), "back"); m.add(fun(&Bidir_Type::back), "back");
}
return m;
}
/// Algorithm for inserting at a specific position into a container /// Algorithm for inserting at a specific position into a container
@ -230,10 +228,16 @@ namespace chaiscript
} }
template<typename ContainerType> template<typename ContainerType>
ModulePtr input_range_type(const std::string &type, ModulePtr m = std::make_shared<Module>()) void input_range_type(const std::string &type, Module& m)
{ {
detail::input_range_type_impl<Bidir_Range<ContainerType> >(type,m); detail::input_range_type_impl<Bidir_Range<ContainerType> >(type,m);
detail::input_range_type_impl<Const_Bidir_Range<ContainerType> >("Const_" + type, m); detail::input_range_type_impl<Const_Bidir_Range<ContainerType> >("Const_" + type, m);
}
template<typename ContainerType>
ModulePtr input_range_type(const std::string &type)
{
auto m = std::make_shared<Module>();
input_range_type<ContainerType>(type, *m);
return m; return m;
} }
@ -241,11 +245,11 @@ namespace chaiscript
/// Add random_access_container concept to the given ContainerType /// Add random_access_container concept to the given ContainerType
/// http://www.sgi.com/tech/stl/RandomAccessContainer.html /// http://www.sgi.com/tech/stl/RandomAccessContainer.html
template<typename ContainerType> template<typename ContainerType>
ModulePtr random_access_container_type(const std::string &/*type*/, ModulePtr m = std::make_shared<Module>()) void random_access_container_type(const std::string &/*type*/, Module& m)
{ {
//In the interest of runtime safety for the m, we prefer the at() method for [] access, //In the interest of runtime safety for the m, 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.
m->add( m.add(
fun( fun(
[](ContainerType &c, int index) -> typename ContainerType::reference { [](ContainerType &c, int index) -> typename ContainerType::reference {
/// \todo we are prefering to keep the key as 'int' to avoid runtime conversions /// \todo we are prefering to keep the key as 'int' to avoid runtime conversions
@ -253,25 +257,37 @@ namespace chaiscript
return c.at(static_cast<typename ContainerType::size_type>(index)); return c.at(static_cast<typename ContainerType::size_type>(index));
}), "[]"); }), "[]");
m->add( m.add(
fun( fun(
[](const ContainerType &c, int index) -> typename ContainerType::const_reference { [](const ContainerType &c, int index) -> typename ContainerType::const_reference {
/// \todo we are prefering to keep the key as 'int' to avoid runtime conversions /// \todo we are prefering to keep the key as 'int' to avoid runtime conversions
/// during dispatch. reevaluate /// during dispatch. reevaluate
return c.at(static_cast<typename ContainerType::size_type>(index)); return c.at(static_cast<typename ContainerType::size_type>(index));
}), "[]"); }), "[]");
}
template<typename ContainerType>
ModulePtr random_access_container_type(const std::string &type)
{
auto m = std::make_shared<Module>();
random_access_container_type<ContainerType>(type, *m);
return m; return m;
} }
/// Add assignable concept to the given ContainerType /// Add assignable concept to the given ContainerType
/// http://www.sgi.com/tech/stl/Assignable.html /// http://www.sgi.com/tech/stl/Assignable.html
template<typename ContainerType> template<typename ContainerType>
ModulePtr assignable_type(const std::string &type, ModulePtr m = std::make_shared<Module>()) void assignable_type(const std::string &type, Module& m)
{ {
copy_constructor<ContainerType>(type, m); copy_constructor<ContainerType>(type, m);
operators::assign<ContainerType>(m); operators::assign<ContainerType>(m);
}
template<typename ContainerType>
ModulePtr assignable_type(const std::string &type)
{
auto m = std::make_shared<Module>();
assignable_type<ContainerType>(type, *m);
return m; return m;
} }
@ -279,33 +295,44 @@ namespace chaiscript
/// Add container concept to the given ContainerType /// Add container concept to the given ContainerType
/// http://www.sgi.com/tech/stl/Container.html /// http://www.sgi.com/tech/stl/Container.html
template<typename ContainerType> template<typename ContainerType>
ModulePtr container_type(const std::string &/*type*/, ModulePtr m = std::make_shared<Module>()) void container_type(const std::string &/*type*/, Module& m)
{ {
m->add(fun([](const ContainerType *a) { return a->size(); } ), "size"); m.add(fun([](const ContainerType *a) { return a->size(); } ), "size");
m->add(fun([](const ContainerType *a) { return a->empty(); } ), "empty"); m.add(fun([](const ContainerType *a) { return a->empty(); } ), "empty");
m->add(fun([](ContainerType *a) { a->clear(); } ), "clear"); m.add(fun([](ContainerType *a) { a->clear(); } ), "clear");
return m;
} }
template <typename ContainerType>
ModulePtr container_type(const std::string& type)
{
auto m = std::make_shared<Module>();
container_type<ContainerType>(type, *m);
return m;
}
/// Add default constructable concept to the given Type /// Add default constructable concept to the given Type
/// http://www.sgi.com/tech/stl/DefaultConstructible.html /// http://www.sgi.com/tech/stl/DefaultConstructible.html
template<typename Type> template<typename Type>
ModulePtr default_constructible_type(const std::string &type, ModulePtr m = std::make_shared<Module>()) void default_constructible_type(const std::string &type, Module& m)
{ {
m->add(constructor<Type ()>(), type); m.add(constructor<Type ()>(), type);
}
template <typename Type>
ModulePtr default_constructible_type(const std::string& type)
{
auto m = std::make_shared<Module>();
default_constructible_type<Type>(type, *m);
return m; return m;
} }
/// Add sequence concept to the given ContainerType /// Add sequence concept to the given ContainerType
/// http://www.sgi.com/tech/stl/Sequence.html /// http://www.sgi.com/tech/stl/Sequence.html
template<typename ContainerType> template<typename ContainerType>
ModulePtr sequence_type(const std::string &/*type*/, ModulePtr m = std::make_shared<Module>()) void sequence_type(const std::string &/*type*/, Module& m)
{ {
m->add(fun(&detail::insert_at<ContainerType>), m.add(fun(&detail::insert_at<ContainerType>),
[]()->std::string{ []()->std::string{
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) { if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
return "insert_ref_at"; return "insert_ref_at";
@ -314,27 +341,31 @@ namespace chaiscript
} }
}()); }());
m->add(fun(&detail::erase_at<ContainerType>), "erase_at"); m.add(fun(&detail::erase_at<ContainerType>), "erase_at");
}
template <typename ContainerType>
ModulePtr sequence_type(const std::string &type)
{
auto m = std::make_shared<Module>();
sequence_type<ContainerType>(type, *m);
return m; return m;
} }
/// Add back insertion sequence concept to the given ContainerType /// Add back insertion sequence concept to the given ContainerType
/// http://www.sgi.com/tech/stl/BackInsertionSequence.html /// http://www.sgi.com/tech/stl/BackInsertionSequence.html
template<typename ContainerType> template<typename ContainerType>
ModulePtr back_insertion_sequence_type(const std::string &type, ModulePtr m = std::make_shared<Module>()) void back_insertion_sequence_type(const std::string &type, Module& m)
{ {
typedef typename ContainerType::reference (ContainerType::*backptr)(); typedef typename ContainerType::reference (ContainerType::*backptr)();
m->add(fun(static_cast<backptr>(&ContainerType::back)), "back"); m.add(fun(static_cast<backptr>(&ContainerType::back)), "back");
typedef void (ContainerType::*push_back)(const typename ContainerType::value_type &); typedef void (ContainerType::*push_back)(const typename ContainerType::value_type &);
m->add(fun(static_cast<push_back>(&ContainerType::push_back)), m.add(fun(static_cast<push_back>(&ContainerType::push_back)),
[&]()->std::string{ [&]()->std::string{
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) { if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
m->eval( m.eval(
"# Pushes the second value onto the container while making a clone of the value\n" "# Pushes the second value onto the container while making a clone of the value\n"
"def push_back(" + type + " container, x)\n" "def push_back(" + type + " container, x)\n"
"{ \n" "{ \n"
@ -353,7 +384,13 @@ namespace chaiscript
} }
}()); }());
m->add(fun(&ContainerType::pop_back), "pop_back"); m.add(fun(&ContainerType::pop_back), "pop_back");
}
template<typename ContainerType>
ModulePtr back_insertion_sequence_type(const std::string &type)
{
auto m = std::make_shared<Module>();
back_insertion_sequence_type<ContainerType>(type, *m);
return m; return m;
} }
@ -362,20 +399,20 @@ namespace chaiscript
/// Front insertion sequence /// Front insertion sequence
/// http://www.sgi.com/tech/stl/FrontInsertionSequence.html /// http://www.sgi.com/tech/stl/FrontInsertionSequence.html
template<typename ContainerType> template<typename ContainerType>
ModulePtr front_insertion_sequence_type(const std::string &type, ModulePtr m = std::make_shared<Module>()) void front_insertion_sequence_type(const std::string &type, Module& m)
{ {
typedef typename ContainerType::reference (ContainerType::*front_ptr)(); typedef typename ContainerType::reference (ContainerType::*front_ptr)();
typedef typename ContainerType::const_reference (ContainerType::*const_front_ptr)() const; typedef typename ContainerType::const_reference (ContainerType::*const_front_ptr)() const;
typedef void (ContainerType::*push_ptr)(typename ContainerType::const_reference); typedef void (ContainerType::*push_ptr)(typename ContainerType::const_reference);
typedef void (ContainerType::*pop_ptr)(); typedef void (ContainerType::*pop_ptr)();
m->add(fun(static_cast<front_ptr>(&ContainerType::front)), "front"); m.add(fun(static_cast<front_ptr>(&ContainerType::front)), "front");
m->add(fun(static_cast<const_front_ptr>(&ContainerType::front)), "front"); m.add(fun(static_cast<const_front_ptr>(&ContainerType::front)), "front");
m->add(fun(static_cast<push_ptr>(&ContainerType::push_front)), m.add(fun(static_cast<push_ptr>(&ContainerType::push_front)),
[&]()->std::string{ [&]()->std::string{
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) { if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
m->eval( m.eval(
"# Pushes the second value onto the front of container while making a clone of the value\n" "# Pushes the second value onto the front of container while making a clone of the value\n"
"def push_front(" + type + " container, x)\n" "def push_front(" + type + " container, x)\n"
"{ \n" "{ \n"
@ -393,7 +430,13 @@ namespace chaiscript
} }
}()); }());
m->add(fun(static_cast<pop_ptr>(&ContainerType::pop_front)), "pop_front"); m.add(fun(static_cast<pop_ptr>(&ContainerType::pop_front)), "pop_front");
}
template<typename ContainerType>
ModulePtr front_insertion_sequence_type(const std::string &type)
{
auto m = std::make_shared<Module>();
front_insertion_sequence_type<ContainerType>(type, *m);
return m; return m;
} }
@ -401,20 +444,25 @@ namespace chaiscript
/// bootstrap a given PairType /// bootstrap a given PairType
/// http://www.sgi.com/tech/stl/pair.html /// http://www.sgi.com/tech/stl/pair.html
template<typename PairType> template<typename PairType>
ModulePtr pair_type(const std::string &type, ModulePtr m = std::make_shared<Module>()) void pair_type(const std::string &type, Module& m)
{ {
m->add(user_type<PairType>(), type); m.add(user_type<PairType>(), type);
typename PairType::first_type PairType::* f = &PairType::first; typename PairType::first_type PairType::* f = &PairType::first;
typename PairType::second_type PairType::* s = &PairType::second; typename PairType::second_type PairType::* s = &PairType::second;
m->add(fun(f), "first"); m.add(fun(f), "first");
m->add(fun(s), "second"); m.add(fun(s), "second");
basic_constructors<PairType>(type, m); basic_constructors<PairType>(type, m);
m->add(constructor<PairType (const typename PairType::first_type &, const typename PairType::second_type &)>(), type); m.add(constructor<PairType (const typename PairType::first_type &, const typename PairType::second_type &)>(), type);
}
template<typename PairType>
ModulePtr pair_type(const std::string &type)
{
auto m = std::make_shared<Module>();
pair_type<PairType>(type, *m);
return m; return m;
} }
@ -424,10 +472,15 @@ namespace chaiscript
/// http://www.sgi.com/tech/stl/PairAssociativeContainer.html /// http://www.sgi.com/tech/stl/PairAssociativeContainer.html
template<typename ContainerType> template<typename ContainerType>
ModulePtr pair_associative_container_type(const std::string &type, ModulePtr m = std::make_shared<Module>()) void pair_associative_container_type(const std::string &type, Module& m)
{ {
pair_type<typename ContainerType::value_type>(type + "_Pair", m); pair_type<typename ContainerType::value_type>(type + "_Pair", m);
}
template<typename ContainerType>
ModulePtr pair_associative_container_type(const std::string &type)
{
auto m = std::make_shared<Module>();
pair_associative_container_type<ContainerType>(type, *m);
return m; return m;
} }
@ -435,17 +488,17 @@ namespace chaiscript
/// Add unique associative container concept to the given ContainerType /// Add unique associative container concept to the given ContainerType
/// http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html /// http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html
template<typename ContainerType> template<typename ContainerType>
ModulePtr unique_associative_container_type(const std::string &/*type*/, ModulePtr m = std::make_shared<Module>()) void unique_associative_container_type(const std::string &/*type*/, Module& m)
{ {
m->add(fun(detail::count<ContainerType>), "count"); m.add(fun(detail::count<ContainerType>), "count");
typedef size_t (ContainerType::*erase_ptr)(const typename ContainerType::key_type &); typedef size_t (ContainerType::*erase_ptr)(const typename ContainerType::key_type &);
m->add(fun(static_cast<erase_ptr>(&ContainerType::erase)), "erase"); m.add(fun(static_cast<erase_ptr>(&ContainerType::erase)), "erase");
m->add(fun(&detail::insert<ContainerType>), "insert"); m.add(fun(&detail::insert<ContainerType>), "insert");
m->add(fun(&detail::insert_ref<ContainerType>), m.add(fun(&detail::insert_ref<ContainerType>),
[]()->std::string{ []()->std::string{
if (typeid(typename ContainerType::mapped_type) == typeid(Boxed_Value)) { if (typeid(typename ContainerType::mapped_type) == typeid(Boxed_Value)) {
return "insert_ref"; return "insert_ref";
@ -453,8 +506,12 @@ namespace chaiscript
return "insert"; return "insert";
} }
}()); }());
}
template<typename ContainerType>
ModulePtr unique_associative_container_type(const std::string &type)
{
auto m = std::make_shared<Module>();
unique_associative_container_type<ContainerType>(type, *m);
return m; return m;
} }
@ -462,21 +519,21 @@ namespace chaiscript
/// Add a MapType container /// Add a MapType container
/// http://www.sgi.com/tech/stl/Map.html /// http://www.sgi.com/tech/stl/Map.html
template<typename MapType> template<typename MapType>
ModulePtr map_type(const std::string &type, ModulePtr m = std::make_shared<Module>()) void map_type(const std::string &type, Module& m)
{ {
m->add(user_type<MapType>(), type); m.add(user_type<MapType>(), type);
typedef typename MapType::mapped_type &(MapType::*elem_access)(const typename MapType::key_type &); typedef typename MapType::mapped_type &(MapType::*elem_access)(const typename MapType::key_type &);
typedef const typename MapType::mapped_type &(MapType::*const_elem_access)(const typename MapType::key_type &) const; typedef const typename MapType::mapped_type &(MapType::*const_elem_access)(const typename MapType::key_type &) const;
m->add(fun(static_cast<elem_access>(&MapType::operator[])), "[]"); m.add(fun(static_cast<elem_access>(&MapType::operator[])), "[]");
m->add(fun(static_cast<elem_access>(&MapType::at)), "at"); m.add(fun(static_cast<elem_access>(&MapType::at)), "at");
m->add(fun(static_cast<const_elem_access>(&MapType::at)), "at"); m.add(fun(static_cast<const_elem_access>(&MapType::at)), "at");
if (typeid(MapType) == typeid(std::map<std::string, Boxed_Value>)) if (typeid(MapType) == typeid(std::map<std::string, Boxed_Value>))
{ {
m->eval(R"( m.eval(R"(
def Map::`==`(Map rhs) { def Map::`==`(Map rhs) {
if ( rhs.size() != this.size() ) { if ( rhs.size() != this.size() ) {
return false; return false;
@ -504,7 +561,12 @@ namespace chaiscript
unique_associative_container_type<MapType>(type, m); unique_associative_container_type<MapType>(type, m);
pair_associative_container_type<MapType>(type, m); pair_associative_container_type<MapType>(type, m);
input_range_type<MapType>(type, m); input_range_type<MapType>(type, m);
}
template<typename MapType>
ModulePtr map_type(const std::string &type)
{
auto m = std::make_shared<Module>();
map_type<MapType>(type, *m);
return m; return m;
} }
@ -512,9 +574,9 @@ namespace chaiscript
/// hopefully working List type /// hopefully working List type
/// http://www.sgi.com/tech/stl/List.html /// http://www.sgi.com/tech/stl/List.html
template<typename ListType> template<typename ListType>
ModulePtr list_type(const std::string &type, ModulePtr m = std::make_shared<Module>()) void list_type(const std::string &type, Module& m)
{ {
m->add(user_type<ListType>(), type); m.add(user_type<ListType>(), type);
front_insertion_sequence_type<ListType>(type, m); front_insertion_sequence_type<ListType>(type, m);
back_insertion_sequence_type<ListType>(type, m); back_insertion_sequence_type<ListType>(type, m);
@ -523,7 +585,12 @@ namespace chaiscript
default_constructible_type<ListType>(type, m); default_constructible_type<ListType>(type, m);
assignable_type<ListType>(type, m); assignable_type<ListType>(type, m);
input_range_type<ListType>(type, m); input_range_type<ListType>(type, m);
}
template<typename ListType>
ModulePtr list_type(const std::string &type)
{
auto m = std::make_shared<Module>();
list_type<ListType>(type, m);
return m; return m;
} }
@ -531,15 +598,15 @@ namespace chaiscript
/// Create a vector type with associated concepts /// Create a vector type with associated concepts
/// http://www.sgi.com/tech/stl/Vector.html /// http://www.sgi.com/tech/stl/Vector.html
template<typename VectorType> template<typename VectorType>
ModulePtr vector_type(const std::string &type, ModulePtr m = std::make_shared<Module>()) void vector_type(const std::string &type, Module& m)
{ {
m->add(user_type<VectorType>(), type); m.add(user_type<VectorType>(), type);
typedef typename VectorType::reference (VectorType::*frontptr)(); typedef typename VectorType::reference (VectorType::*frontptr)();
typedef typename VectorType::const_reference (VectorType::*constfrontptr)() const; typedef typename VectorType::const_reference (VectorType::*constfrontptr)() const;
m->add(fun(static_cast<frontptr>(&VectorType::front)), "front"); m.add(fun(static_cast<frontptr>(&VectorType::front)), "front");
m->add(fun(static_cast<constfrontptr>(&VectorType::front)), "front"); m.add(fun(static_cast<constfrontptr>(&VectorType::front)), "front");
back_insertion_sequence_type<VectorType>(type, m); back_insertion_sequence_type<VectorType>(type, m);
@ -552,7 +619,7 @@ namespace chaiscript
if (typeid(VectorType) == typeid(std::vector<Boxed_Value>)) if (typeid(VectorType) == typeid(std::vector<Boxed_Value>))
{ {
m->eval(R"( m.eval(R"(
def Vector::`==`(Vector rhs) { def Vector::`==`(Vector rhs) {
if ( rhs.size() != this.size() ) { if ( rhs.size() != this.size() ) {
return false; return false;
@ -573,16 +640,21 @@ namespace chaiscript
} )" } )"
); );
} }
}
template<typename VectorType>
ModulePtr vector_type(const std::string &type)
{
auto m = std::make_shared<Module>();
vector_type<VectorType>(type, *m);
return m; return m;
} }
/// Add a String container /// Add a String container
/// http://www.sgi.com/tech/stl/basic_string.html /// http://www.sgi.com/tech/stl/basic_string.html
template<typename String> template<typename String>
ModulePtr string_type(const std::string &type, ModulePtr m = std::make_shared<Module>()) void string_type(const std::string &type, Module& m)
{ {
m->add(user_type<String>(), type); m.add(user_type<String>(), type);
operators::addition<String>(m); operators::addition<String>(m);
operators::assign_sum<String>(m); operators::assign_sum<String>(m);
opers_comparison<String>(m); opers_comparison<String>(m);
@ -594,7 +666,7 @@ namespace chaiscript
input_range_type<String>(type, m); input_range_type<String>(type, m);
//Special case: add push_back to string (which doesn't support other back_insertion operations //Special case: add push_back to string (which doesn't support other back_insertion operations
m->add(fun(&String::push_back), m.add(fun(&String::push_back),
[]()->std::string{ []()->std::string{
if (typeid(typename String::value_type) == typeid(Boxed_Value)) { if (typeid(typename String::value_type) == typeid(Boxed_Value)) {
return "push_back_ref"; return "push_back_ref";
@ -604,21 +676,26 @@ namespace chaiscript
}()); }());
m->add(fun([](const String *s, const String &f, size_t pos) { return s->find(f, pos); } ), "find"); m.add(fun([](const String *s, const String &f, size_t pos) { return s->find(f, pos); } ), "find");
m->add(fun([](const String *s, const String &f, size_t pos) { return s->rfind(f, pos); } ), "rfind"); m.add(fun([](const String *s, const String &f, size_t pos) { return s->rfind(f, pos); } ), "rfind");
m->add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_of(f, pos); } ), "find_first_of"); m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_of(f, pos); } ), "find_first_of");
m->add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_of(f, pos); } ), "find_last_of"); m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_of(f, pos); } ), "find_last_of");
m->add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_not_of(f, pos); } ), "find_last_not_of"); m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_not_of(f, pos); } ), "find_last_not_of");
m->add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_not_of(f, pos); } ), "find_first_not_of"); m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_not_of(f, pos); } ), "find_first_not_of");
m->add(fun([](String *s) { s->clear(); } ), "clear"); m.add(fun([](String *s) { s->clear(); } ), "clear");
m->add(fun([](const String *s) { return s->empty(); } ), "empty"); m.add(fun([](const String *s) { return s->empty(); } ), "empty");
m->add(fun([](const String *s) { return s->size(); } ), "size"); m.add(fun([](const String *s) { return s->size(); } ), "size");
m->add(fun([](const String *s) { return s->c_str(); } ), "c_str");
m->add(fun([](const String *s) { return s->data(); } ), "data");
m->add(fun([](const String *s, size_t pos, size_t len) { return s->substr(pos, len); } ), "substr");
m.add(fun([](const String *s) { return s->c_str(); } ), "c_str");
m.add(fun([](const String *s) { return s->data(); } ), "data");
m.add(fun([](const String *s, size_t pos, size_t len) { return s->substr(pos, len); } ), "substr");
}
template<typename String>
ModulePtr string_type(const std::string &type)
{
auto m = std::make_shared<Module>();
string_type<String>(type, *m);
return m; return m;
} }
@ -627,14 +704,19 @@ namespace chaiscript
/// Add a MapType container /// Add a MapType container
/// http://www.sgi.com/tech/stl/Map.html /// http://www.sgi.com/tech/stl/Map.html
template<typename FutureType> template<typename FutureType>
ModulePtr future_type(const std::string &type, ModulePtr m = std::make_shared<Module>()) void future_type(const std::string &type, Module& m)
{ {
m->add(user_type<FutureType>(), type); m.add(user_type<FutureType>(), type);
m->add(fun([](const FutureType &t) { return t.valid(); }), "valid");
m->add(fun(&FutureType::get), "get");
m->add(fun(&FutureType::wait), "wait");
m.add(fun([](const FutureType &t) { return t.valid(); }), "valid");
m.add(fun(&FutureType::get), "get");
m.add(fun(&FutureType::wait), "wait");
}
template<typename FutureType>
ModulePtr future_type(const std::string &type)
{
auto m = std::make_shared<Module>();
future_type<FutureType>(type, *m);
return m; return m;
} }
} }

View File

@ -178,12 +178,6 @@ namespace chaiscript
return *this; return *this;
} }
Module &add(const std::shared_ptr<Module> &m)
{
m->apply(*this, *this);
return *m;
}
template<typename Eval, typename Engine> template<typename Eval, typename Engine>
void apply(Eval &t_eval, Engine &t_engine) const void apply(Eval &t_eval, Engine &t_engine) const
{ {

View File

@ -229,234 +229,201 @@ namespace chaiscript
template<typename T> template<typename T>
ModulePtr assign(ModulePtr m = std::make_shared<Module>()) void assign(Module& m)
{ {
m->add(chaiscript::fun(&detail::assign<T &, const T&>), "="); m.add(chaiscript::fun(&detail::assign<T &, const T&>), "=");
return m;
} }
template<typename T> template<typename T>
ModulePtr assign_bitwise_and(ModulePtr m = std::make_shared<Module>()) void assign_bitwise_and(Module& m)
{ {
m->add(chaiscript::fun(&detail::assign_bitwise_and<T &, const T&>), "&="); m.add(chaiscript::fun(&detail::assign_bitwise_and<T &, const T&>), "&=");
return m;
} }
template<typename T> template<typename T>
ModulePtr assign_xor(ModulePtr m = std::make_shared<Module>()) void assign_xor(Module& m)
{ {
m->add(chaiscript::fun(&detail::assign_xor<T &, const T&>), "^="); m.add(chaiscript::fun(&detail::assign_xor<T &, const T&>), "^=");
return m;
} }
template<typename T> template<typename T>
ModulePtr assign_bitwise_or(ModulePtr m = std::make_shared<Module>()) void assign_bitwise_or(Module& m)
{ {
m->add(chaiscript::fun(&detail::assign_bitwise_or<T &, const T&>), "|="); m.add(chaiscript::fun(&detail::assign_bitwise_or<T &, const T&>), "|=");
return m;
} }
template<typename T> template<typename T>
ModulePtr assign_difference(ModulePtr m = std::make_shared<Module>()) void assign_difference(Module& m)
{ {
m->add(chaiscript::fun(&detail::assign_difference<T &, const T&>), "-="); m.add(chaiscript::fun(&detail::assign_difference<T &, const T&>), "-=");
return m;
} }
template<typename T> template<typename T>
ModulePtr assign_left_shift(ModulePtr m = std::make_shared<Module>()) void assign_left_shift(Module& m)
{ {
m->add(chaiscript::fun(&detail::assign_left_shift<T &, const T&>), "<<="); m.add(chaiscript::fun(&detail::assign_left_shift<T &, const T&>), "<<=");
return m;
} }
template<typename T> template<typename T>
ModulePtr assign_product(ModulePtr m = std::make_shared<Module>()) void assign_product(Module& m)
{ {
m->add(chaiscript::fun(&detail::assign_product<T &, const T&>), "*="); m.add(chaiscript::fun(&detail::assign_product<T &, const T&>), "*=");
return m;
} }
template<typename T> template<typename T>
ModulePtr assign_quotient(ModulePtr m = std::make_shared<Module>()) void assign_quotient(Module& m)
{ {
m->add(chaiscript::fun(&detail::assign_quotient<T &, const T&>), "/="); m.add(chaiscript::fun(&detail::assign_quotient<T &, const T&>), "/=");
return m;
} }
template<typename T> template<typename T>
ModulePtr assign_remainder(ModulePtr m = std::make_shared<Module>()) void assign_remainder(Module& m)
{ {
m->add(chaiscript::fun(&detail::assign_remainder<T &, const T&>), "%="); m.add(chaiscript::fun(&detail::assign_remainder<T &, const T&>), "%=");
return m;
} }
template<typename T> template<typename T>
ModulePtr assign_right_shift(ModulePtr m = std::make_shared<Module>()) void assign_right_shift(Module& m)
{ {
m->add(chaiscript::fun(&detail::assign_right_shift<T &, const T&>), ">>="); m.add(chaiscript::fun(&detail::assign_right_shift<T &, const T&>), ">>=");
return m;
} }
template<typename T> template<typename T>
ModulePtr assign_sum(ModulePtr m = std::make_shared<Module>()) void assign_sum(Module& m)
{ {
m->add(chaiscript::fun(&detail::assign_sum<T &, const T&>), "+="); m.add(chaiscript::fun(&detail::assign_sum<T &, const T&>), "+=");
return m;
} }
template<typename T> template<typename T>
ModulePtr prefix_decrement(ModulePtr m = std::make_shared<Module>()) void prefix_decrement(Module& m)
{ {
m->add(chaiscript::fun(&detail::prefix_decrement<T &>), "--"); m.add(chaiscript::fun(&detail::prefix_decrement<T &>), "--");
return m;
} }
template<typename T> template<typename T>
ModulePtr prefix_increment(ModulePtr m = std::make_shared<Module>()) void prefix_increment(Module& m)
{ {
m->add(chaiscript::fun(&detail::prefix_increment<T &>), "++"); m.add(chaiscript::fun(&detail::prefix_increment<T &>), "++");
return m;
} }
template<typename T> template<typename T>
ModulePtr equal(ModulePtr m = std::make_shared<Module>()) void equal(Module& m)
{ {
m->add(chaiscript::fun(&detail::equal<const T&, const T&>), "=="); m.add(chaiscript::fun(&detail::equal<const T&, const T&>), "==");
return m;
} }
template<typename T> template<typename T>
ModulePtr greater_than(ModulePtr m = std::make_shared<Module>()) void greater_than(Module& m)
{ {
m->add(chaiscript::fun(&detail::greater_than<const T&, const T&>), ">"); m.add(chaiscript::fun(&detail::greater_than<const T&, const T&>), ">");
return m;
} }
template<typename T> template<typename T>
ModulePtr greater_than_equal(ModulePtr m = std::make_shared<Module>()) void greater_than_equal(Module& m)
{ {
m->add(chaiscript::fun(&detail::greater_than_equal<const T&, const T&>), ">="); m.add(chaiscript::fun(&detail::greater_than_equal<const T&, const T&>), ">=");
return m;
} }
template<typename T> template<typename T>
ModulePtr less_than(ModulePtr m = std::make_shared<Module>()) void less_than(Module& m)
{ {
m->add(chaiscript::fun(&detail::less_than<const T&, const T&>), "<"); m.add(chaiscript::fun(&detail::less_than<const T&, const T&>), "<");
return m;
} }
template<typename T> template<typename T>
ModulePtr less_than_equal(ModulePtr m = std::make_shared<Module>()) void less_than_equal(Module& m)
{ {
m->add(chaiscript::fun(&detail::less_than_equal<const T&, const T&>), "<="); m.add(chaiscript::fun(&detail::less_than_equal<const T&, const T&>), "<=");
return m;
} }
template<typename T> template<typename T>
ModulePtr logical_compliment(ModulePtr m = std::make_shared<Module>()) void logical_compliment(Module& m)
{ {
m->add(chaiscript::fun(&detail::logical_compliment<const T &>), "!"); m.add(chaiscript::fun(&detail::logical_compliment<const T &>), "!");
return m;
} }
template<typename T> template<typename T>
ModulePtr not_equal(ModulePtr m = std::make_shared<Module>()) void not_equal(Module& m)
{ {
m->add(chaiscript::fun(&detail::not_equal<const T &, const T &>), "!="); m.add(chaiscript::fun(&detail::not_equal<const T &, const T &>), "!=");
return m;
} }
template<typename T> template<typename T>
ModulePtr addition(ModulePtr m = std::make_shared<Module>()) void addition(Module& m)
{ {
m->add(chaiscript::fun(&detail::addition<const T &, const T &>), "+"); m.add(chaiscript::fun(&detail::addition<const T &, const T &>), "+");
return m;
} }
template<typename T> template<typename T>
ModulePtr unary_plus(ModulePtr m = std::make_shared<Module>()) void unary_plus(Module& m)
{ {
m->add(chaiscript::fun(&detail::unary_plus<const T &>), "+"); m.add(chaiscript::fun(&detail::unary_plus<const T &>), "+");
return m;
} }
template<typename T> template<typename T>
ModulePtr subtraction(ModulePtr m = std::make_shared<Module>()) void subtraction(Module& m)
{ {
m->add(chaiscript::fun(&detail::subtraction<const T &, const T &>), "-"); m.add(chaiscript::fun(&detail::subtraction<const T &, const T &>), "-");
return m;
} }
template<typename T> template<typename T>
ModulePtr unary_minus(ModulePtr m = std::make_shared<Module>()) void unary_minus(Module& m)
{ {
m->add(chaiscript::fun(&detail::unary_minus<const T &>), "-"); m.add(chaiscript::fun(&detail::unary_minus<const T &>), "-");
return m;
} }
template<typename T> template<typename T>
ModulePtr bitwise_and(ModulePtr m = std::make_shared<Module>()) void bitwise_and(Module& m)
{ {
m->add(chaiscript::fun(&detail::bitwise_and<const T &, const T &>), "&"); m.add(chaiscript::fun(&detail::bitwise_and<const T &, const T &>), "&");
return m;
} }
template<typename T> template<typename T>
ModulePtr bitwise_compliment(ModulePtr m = std::make_shared<Module>()) void bitwise_compliment(Module& m)
{ {
m->add(chaiscript::fun(&detail::bitwise_compliment<const T &>), "~"); m.add(chaiscript::fun(&detail::bitwise_compliment<const T &>), "~");
return m;
} }
template<typename T> template<typename T>
ModulePtr bitwise_xor(ModulePtr m = std::make_shared<Module>()) void bitwise_xor(Module& m)
{ {
m->add(chaiscript::fun(&detail::bitwise_xor<const T &, const T &>), "^"); m.add(chaiscript::fun(&detail::bitwise_xor<const T &, const T &>), "^");
return m;
} }
template<typename T> template<typename T>
ModulePtr bitwise_or(ModulePtr m = std::make_shared<Module>()) void bitwise_or(Module& m)
{ {
m->add(chaiscript::fun(&detail::bitwise_or<const T &, const T &>), "|"); m.add(chaiscript::fun(&detail::bitwise_or<const T &, const T &>), "|");
return m;
} }
template<typename T> template<typename T>
ModulePtr division(ModulePtr m = std::make_shared<Module>()) void division(Module& m)
{ {
m->add(chaiscript::fun(&detail::division<const T &, const T &>), "/"); m.add(chaiscript::fun(&detail::division<const T &, const T &>), "/");
return m;
} }
template<typename T> template<typename T>
ModulePtr left_shift(ModulePtr m = std::make_shared<Module>()) void left_shift(Module& m)
{ {
m->add(chaiscript::fun(&detail::left_shift<const T &, const T &>), "<<"); m.add(chaiscript::fun(&detail::left_shift<const T &, const T &>), "<<");
return m;
} }
template<typename T> template<typename T>
ModulePtr multiplication(ModulePtr m = std::make_shared<Module>()) void multiplication(Module& m)
{ {
m->add(chaiscript::fun(&detail::multiplication<const T &, const T &>), "*"); m.add(chaiscript::fun(&detail::multiplication<const T &, const T &>), "*");
return m;
} }
template<typename T> template<typename T>
ModulePtr remainder(ModulePtr m = std::make_shared<Module>()) void remainder(Module& m)
{ {
m->add(chaiscript::fun(&detail::remainder<const T &, const T &>), "%"); m.add(chaiscript::fun(&detail::remainder<const T &, const T &>), "%");
return m;
} }
template<typename T> template<typename T>
ModulePtr right_shift(ModulePtr m = std::make_shared<Module>()) void right_shift(Module& m)
{ {
m->add(chaiscript::fun(&detail::right_shift<const T &, const T &>), ">>"); m.add(chaiscript::fun(&detail::right_shift<const T &, const T &>), ">>");
return m;
} }
} }
} }

View File

@ -9,11 +9,11 @@ namespace chaiscript
{ {
public: public:
static ModulePtr library(ModulePtr m = std::make_shared<Module>()) static Module& library(Module& m)
{ {
m->add(chaiscript::fun([](const std::string &t_str) { return from_json(t_str); }), "from_json"); m.add(chaiscript::fun([](const std::string &t_str) { return from_json(t_str); }), "from_json");
m->add(chaiscript::fun(&json_wrap::to_json), "to_json"); m.add(chaiscript::fun(&json_wrap::to_json), "to_json");
return m; return m;

View File

@ -78,10 +78,9 @@ namespace chaiscript
t_module.add(chaiscript::constructor<Enum (const Enum &)>(), t_class_name); t_module.add(chaiscript::constructor<Enum (const Enum &)>(), t_class_name);
using namespace chaiscript::bootstrap::operators; using namespace chaiscript::bootstrap::operators;
t_module.add([](){ equal<Enum>(t_module);
// add some comparison and assignment operators not_equal<Enum>(t_module);
return assign<Enum>(not_equal<Enum>(equal<Enum>())); assign<Enum>(t_module);
}());
t_module.add(chaiscript::fun([](const Enum &e, const int &i) { return e == i; }), "=="); t_module.add(chaiscript::fun([](const Enum &e, const int &i) { return e == i; }), "==");
t_module.add(chaiscript::fun([](const int &i, const Enum &e) { return i == e; }), "=="); t_module.add(chaiscript::fun([](const int &i, const Enum &e) { return i == e; }), "==");

View File

@ -23,9 +23,9 @@
CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_stl_extra() CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_stl_extra()
{ {
auto module = std::make_shared<chaiscript::Module>();
auto module = chaiscript::bootstrap::standard_library::list_type<std::list<chaiscript::Boxed_Value> >("List"); chaiscript::bootstrap::standard_library::list_type<std::list<chaiscript::Boxed_Value> >("List", *module);
module->add(chaiscript::bootstrap::standard_library::vector_type<std::vector<uint16_t> >("u16vector")); chaiscript::bootstrap::standard_library::vector_type<std::vector<uint16_t> >("u16vector", *module);
module->add(chaiscript::vector_conversion<std::vector<uint16_t>>()); module->add(chaiscript::vector_conversion<std::vector<uint16_t>>());
return module; return module;
} }

View File

@ -189,9 +189,9 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo
m->add(chaiscript::fun(&TestBaseType::set_string_val), "set_string_val"); m->add(chaiscript::fun(&TestBaseType::set_string_val), "set_string_val");
m->add(chaiscript::fun(&TestBaseType::mdarray), "mdarray"); m->add(chaiscript::fun(&TestBaseType::mdarray), "mdarray");
m->add(chaiscript::bootstrap::array<int[2][3][5]>("IntArray_2_3_5")); chaiscript::bootstrap::array<int[2][3][5]>("IntArray_2_3_5", *m);
m->add(chaiscript::bootstrap::array<int[3][5]>("IntArray_3_5")); chaiscript::bootstrap::array<int[3][5]>("IntArray_3_5", *m);
m->add(chaiscript::bootstrap::array<int[5]>("IntArray_5")); chaiscript::bootstrap::array<int[5]>("IntArray_5", *m);
// member that is a function // member that is a function
m->add(chaiscript::fun(&TestBaseType::func_member), "func_member"); m->add(chaiscript::fun(&TestBaseType::func_member), "func_member");