// This file is distributed under the BSD License. // See "license.txt" for details. // Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com #ifndef CHAISCRIPT_BOOTSTRAP_HPP_ #define CHAISCRIPT_BOOTSTRAP_HPP_ #include "dispatchkit.hpp" #include "dynamic_object.hpp" #include "register_function.hpp" #include "operators.hpp" #include "boxed_number.hpp" #include #include namespace chaiscript { /// \brief Classes and functions useful for bootstrapping of ChaiScript and adding of new types namespace bootstrap { namespace detail { /// \brief Constructs a new POD value object from a Boxed_Number /// \param[in] v Boxed_Number to copy into the new object /// \returns The newly created object. template std::shared_ptr construct_pod(Boxed_Number v) { std::shared_ptr p(new P1()); Boxed_Value bv(p); Boxed_Number nb(bv); nb = v; return p; } } /// \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,out] m The Module to add the copy constructor to /// \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 template ModulePtr copy_constructor(const std::string &type, ModulePtr m = ModulePtr(new Module())) { m->add(constructor(), type); return m; } /// \brief Add all comparison operators for the templated type. Used during bootstrap, also available to users. /// \tparam T Type to create comparison operators for /// \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. template ModulePtr opers_comparison(ModulePtr m = ModulePtr(new Module())) { operators::equal(m); operators::greater_than(m); operators::greater_than_equal(m); operators::less_than(m); operators::less_than_equal(m); operators::not_equal(m); return m; } /// \brief Adds default and copy constructors for the given type /// \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 /// \tparam T Type to generate basic constructors for /// \returns The passed in ModulePtr, or the newly constructed one if the default param is used /// \sa copy_constructor /// \sa constructor template ModulePtr basic_constructors(const std::string &type, ModulePtr m = ModulePtr(new Module())) { m->add(constructor(), type); copy_constructor(type, m); return m; } /// \brief Adds a constructor for a POD type /// \tparam T The type to add the constructor for /// \param[in] type The name of the type /// \param[in,out] m The Module to add the constructor to template ModulePtr construct_pod(const std::string &type, ModulePtr m = ModulePtr(new Module())) { m->add(fun(&detail::construct_pod), type); return m; } /** * to_string function for internal use. Uses ostream operator<< */ template std::string to_string(Input i) { std::stringstream ss; ss << i; return ss.str(); } /** * Internal function for converting from a string to a value * uses ostream operator >> to perform the conversion */ template Input parse_string(const std::string &i) { std::stringstream ss(i); Input t; ss >> t; return t; } /** * Add all common functions for a POD type. All operators, and * common conversions */ template ModulePtr bootstrap_pod_type(const std::string &name, ModulePtr m = ModulePtr(new Module())) { m->add(user_type(), name); m->add(constructor(), name); construct_pod(name, m); m->add(fun(&to_string), "to_string"); m->add(fun(&parse_string), "to_" + name); return m; } /** * "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 * but want to instead maintain the shared_ptr. It is needed internally * for handling of Proxy_Function object (that is, * function variables. */ template std::shared_ptr shared_ptr_clone(const std::shared_ptr &p) { return p; } /** * Specific version of shared_ptr_clone just for Proxy_Functions */ template std::shared_ptr::type> shared_ptr_unconst_clone(const std::shared_ptr::type> &p) { return std::const_pointer_cast::type>(p); } /** * Assignment function for shared_ptr objects, does not perform a copy of the * object pointed to, instead maintains the shared_ptr concept. * Similar to shared_ptr_clone. Used for Proxy_Function. */ template Boxed_Value ptr_assign(Boxed_Value lhs, const std::shared_ptr &rhs) { if (lhs.is_undef() || (!lhs.get_type_info().is_const() && lhs.get_type_info().bare_equal(chaiscript::detail::Get_Type_Info::get()))) { lhs.assign(Boxed_Value(rhs)); return lhs; } else { throw exception::bad_boxed_cast("type mismatch in pointer assignment"); } } /** * Class consisting of only static functions. All default bootstrapping occurs * from this class. */ class Bootstrap { private: /** * Function allowing for assignment of an unknown type to any other value */ static Boxed_Value unknown_assign(Boxed_Value lhs, Boxed_Value rhs) { if (lhs.is_undef()) { return (lhs.assign(rhs)); } else { throw exception::bad_boxed_cast("boxed_value has a set type already"); } } static void print(const std::string &s) { std::cout << s; } static void println(const std::string &s) { std::cout << s << std::endl; } /** * Add all arithmetic operators for PODs */ static void opers_arithmetic_pod(ModulePtr m = ModulePtr(new Module())) { m->add(fun(&Boxed_Number::equals), "=="); m->add(fun(&Boxed_Number::less_than), "<"); m->add(fun(&Boxed_Number::greater_than), ">"); m->add(fun(&Boxed_Number::greater_than_equal), ">="); m->add(fun(&Boxed_Number::less_than_equal), "<="); m->add(fun(&Boxed_Number::not_equal), "!="); m->add(fun(&Boxed_Number::pre_decrement), "--"); m->add(fun(&Boxed_Number::pre_increment), "++"); m->add(fun(&Boxed_Number::sum), "+"); m->add(fun(&Boxed_Number::unary_plus), "+"); m->add(fun(&Boxed_Number::unary_minus), "-"); m->add(fun(&Boxed_Number::difference), "-"); m->add(fun(&Boxed_Number::assign_bitwise_and), "&="); m->add(fun(&Boxed_Number::assign), "="); m->add(fun(&Boxed_Number::assign_bitwise_or), "|="); m->add(fun(&Boxed_Number::assign_bitwise_xor), "^="); m->add(fun(&Boxed_Number::assign_remainder), "%="); m->add(fun(&Boxed_Number::assign_shift_left), "<<="); m->add(fun(&Boxed_Number::assign_shift_right), ">>="); m->add(fun(&Boxed_Number::bitwise_and), "&"); m->add(fun(&Boxed_Number::bitwise_complement), "~"); m->add(fun(&Boxed_Number::bitwise_xor), "^"); m->add(fun(&Boxed_Number::bitwise_or), "|"); m->add(fun(&Boxed_Number::assign_product), "*="); m->add(fun(&Boxed_Number::assign_quotient), "/="); m->add(fun(&Boxed_Number::assign_sum), "+="); m->add(fun(&Boxed_Number::assign_difference), "-="); m->add(fun(&Boxed_Number::quotient), "/"); m->add(fun(&Boxed_Number::shift_left), "<<"); m->add(fun(&Boxed_Number::product), "*"); m->add(fun(&Boxed_Number::remainder), "%"); m->add(fun(&Boxed_Number::shift_right), ">>"); } /** * Create a bound function object. The first param is the function to bind * the remaining parameters are the args to bind into the * result */ static Boxed_Value bind_function(const std::vector ¶ms) { if (params.size() < 2) { throw exception::arity_error(static_cast(params.size()), 2); } Const_Proxy_Function f = boxed_cast(params[0]); return Boxed_Value(Const_Proxy_Function(new dispatch::Bound_Function(f, std::vector(params.begin() + 1, params.end())))); } /** * Returns true if a call can be made that consists of the first parameter * (the function) with the remaining parameters as its arguments. */ static Boxed_Value call_exists(const std::vector ¶ms) { if (params.size() < 1) { throw exception::arity_error(static_cast(params.size()), 1); } Const_Proxy_Function f = boxed_cast(params[0]); return Boxed_Value(f->call_match(std::vector(params.begin() + 1, params.end()))); } static bool has_guard(const Const_Proxy_Function &t_pf) { auto pf = std::dynamic_pointer_cast(t_pf); if (pf) { return bool(pf->get_guard()); } else { return false; } } static Const_Proxy_Function get_guard(const Const_Proxy_Function &t_pf) { auto pf = std::dynamic_pointer_cast(t_pf); if (pf) { if (pf->get_guard()) { return pf->get_guard(); } else { throw std::runtime_error("Function does not have a guard"); } } else { throw std::runtime_error("Function does not have a guard"); } } static void throw_exception(const Boxed_Value &bv) { throw bv; } static std::shared_ptr bootstrap2( std::shared_ptr e = std::shared_ptr (new chaiscript::detail::Dispatch_Engine())) { e->add(user_type(), "void"); return e; } static std::string what(const std::exception &e) { return e.what(); } /** * Boolean specialization of internal to_string function */ static std::string bool_to_string(bool b) { if (b) { return "true"; } else { return "false"; } } template static std::vector do_return_boxed_value_vector(FunctionType f, const dispatch::Proxy_Function_Base *b) { auto v = (b->*f)(); std::vector vbv; for (const auto &o: v) { vbv.push_back(const_var(o)); } return vbv; } template static std::function (const dispatch::Proxy_Function_Base*)> return_boxed_value_vector(const Function &f) { return std::bind(&do_return_boxed_value_vector, f, std::placeholders::_1); } public: /// \brief perform all common bootstrap functions for std::string, void and POD types /// \param[in,out] m Module to add bootstrapped functions to /// \returns passed in ModulePtr, or newly created one if default argument is used static ModulePtr bootstrap(ModulePtr m = ModulePtr(new Module())) { m->add(user_type(), "void"); m->add(user_type(), "bool"); m->add(user_type(), "Object"); m->add(user_type(), "Number"); m->add(user_type(), "Function"); m->add(user_type(), "exception"); 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::operator()), "call"); 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_contained_functions)), "get_contained_functions"); m->add(user_type(), "runtime_error"); m->add(chaiscript::base_class()); m->add(constructor(), "runtime_error"); m->add(fun(std::function(&what)), "what"); m->add(user_type(), "Dynamic_Object"); m->add(constructor(), "Dynamic_Object"); 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_attr), "get_attr"); m->eval("def Dynamic_Object::clone() { auto &new_o = Dynamic_Object(this.get_type_name()); for_each(this.get_attrs(), bind(fun(new_o, x) { new_o.get_attr(x.first) = x.second; }, new_o, _) ); return new_o; }"); m->add(fun(&has_guard), "has_guard"); m->add(fun(&get_guard), "get_guard"); 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_const), "is_var_const"); 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_type), "is_type"); m->add(fun(&Boxed_Value::get_type_info), "get_type_info"); m->add(user_type(), "Type_Info"); operators::equal(m); 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_void), "is_type_void"); 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::name), "cpp_name"); m->add(fun(&Type_Info::bare_name), "cpp_bare_name"); m->add(fun(&Type_Info::bare_equal), "bare_equal"); typedef bool (Type_Info::*typeinfocompare)(const Type_Info &) const; m->add(fun(typeinfocompare(&Type_Info::operator==)), "=="); m->add(fun(&Type_Info::bare_equal), "bare_equal"); basic_constructors("bool", m); operators::assign(m); operators::equal(m); m->add(fun(&to_string), "internal_to_string"); m->add(fun(&Bootstrap::bool_to_string), "internal_to_string"); m->add(fun(&unknown_assign), "="); m->add(fun(&throw_exception), "throw"); m->add(fun(&what), "what"); bootstrap_pod_type("double", m); bootstrap_pod_type("long_double", m); bootstrap_pod_type("float", m); bootstrap_pod_type("int", m); bootstrap_pod_type("unsigned_int", m); bootstrap_pod_type("unsigned_long", m); bootstrap_pod_type("size_t", m); bootstrap_pod_type("char", m); bootstrap_pod_type("int8_t", m); bootstrap_pod_type("int16_t", m); bootstrap_pod_type("int32_t", m); bootstrap_pod_type("int64_t", m); bootstrap_pod_type("uint8_t", m); bootstrap_pod_type("uint16_t", m); bootstrap_pod_type("uint32_t", m); bootstrap_pod_type("uint64_t", m); operators::logical_compliment(m); opers_arithmetic_pod(m); m->add(fun(&print), "print_string"); m->add(fun(&println), "println_string"); m->add(Proxy_Function(new dispatch::Dynamic_Proxy_Function(std::bind(&bind_function, std::placeholders::_1))), "bind"); m->add(fun(&shared_ptr_unconst_clone), "clone"); m->add(fun(&ptr_assign::type>), "="); m->add(fun(&ptr_assign::type>), "="); m->add(Proxy_Function(new dispatch::Dynamic_Proxy_Function(std::bind(&call_exists, std::placeholders::_1))), "call_exists"); m->add(fun(&Boxed_Value::type_match), "type_match"); return m; } }; } } #endif