diff --git a/include/chaiscript/chaiscript.hpp b/include/chaiscript/chaiscript.hpp index eb9fe84..767218e 100644 --- a/include/chaiscript/chaiscript.hpp +++ b/include/chaiscript/chaiscript.hpp @@ -43,16 +43,19 @@ namespace chaiscript Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Map_Pair, Value_Range, Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl }; }; - /** - * Helper lookup to get the name of each node type - */ - const char *token_type_to_string(int tokentype) { + namespace + { + /** + * Helper lookup to get the name of each node type + */ + const char *token_type_to_string(int tokentype) { const char *token_types[] = { "Internal Parser Error", "Int", "Float", "Id", "Char", "Str", "Eol", "Fun_Call", "Inplace_Fun_Call", "Arg_List", "Variable", "Equation", "Var_Decl", - "Expression", "Comparison", "Additive", "Multiplicative", "Negate", "Not", "Array_Call", "Dot_Access", "Quoted_String", "Single_Quoted_String", - "Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Map_Pair", "Value_Range", - "Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl"}; + "Expression", "Comparison", "Additive", "Multiplicative", "Negate", "Not", "Array_Call", "Dot_Access", "Quoted_String", "Single_Quoted_String", + "Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Map_Pair", "Value_Range", + "Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl"}; return token_types[tokentype]; + } } /** diff --git a/include/chaiscript/dispatchkit/bootstrap.hpp b/include/chaiscript/dispatchkit/bootstrap.hpp index d73015b..8dd7679 100644 --- a/include/chaiscript/dispatchkit/bootstrap.hpp +++ b/include/chaiscript/dispatchkit/bootstrap.hpp @@ -483,18 +483,6 @@ namespace chaiscript return boost::lexical_cast(i); } - /** - * Boolean specialization of internal to_string function - */ - template<> std::string to_string(bool b) - { - if (b) - { - return "true"; - } else { - return "false"; - } - } /** * Internal function for converting from a string to a value @@ -540,12 +528,12 @@ namespace chaiscript /** * Specific version of shared_ptr_clone just for Proxy_Functions - * probably not necessary, probably could just use the version above, - * but here we are. */ - Proxy_Function proxy_function_clone(const Const_Proxy_Function &f) + template + boost::shared_ptr::type> + shared_ptr_unconst_clone(const boost::shared_ptr::type> &p) { - return boost::const_pointer_cast(f); + return boost::const_pointer_cast::type>(p); } @@ -671,6 +659,19 @@ namespace chaiscript 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"; + } + } + public: /** * perform all common bootstrap functions for std::string, void and POD types @@ -701,7 +702,7 @@ namespace chaiscript oper_assign(m); m->add(fun(&to_string), "internal_to_string"); - 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"); @@ -723,7 +724,7 @@ namespace chaiscript m->add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))), "bind"); - m->add(fun(&proxy_function_clone), "clone"); + m->add(fun(&shared_ptr_unconst_clone), "clone"); m->add(fun(&ptr_assign), "="); m->add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&call_exists, _1))), diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index 1011939..20cbaa1 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -495,6 +495,100 @@ namespace chaiscript return dispatch(range.first, range.second, params); } + /** + * Dump object info to stdout + */ + void dump_object(Boxed_Value o) const + { + Type_Info ti = o.get_type_info(); + std::cout << (ti.is_const()?"const ":"") << get_type_name(ti) << std::endl; + } + + /** + * Dump type info to stdout + */ + void dump_type(const Type_Info &type) const + { + std::cout << (type.is_const()?"const ":"") << get_type_name(type); + } + + /** + * Dump function to stdout + */ + void dump_function(const std::pair &f) const + { + std::vector params = f.second->get_param_types(); + std::string annotation = f.second->annotation(); + + if (annotation.size() > 0) { + std::cout << annotation; + } + dump_type(params.front()); + std::cout << " " << f.first << "("; + + for (std::vector::const_iterator itr = params.begin() + 1; + itr != params.end(); + ) + { + dump_type(*itr); + ++itr; + + if (itr != params.end()) + { + std::cout << ", "; + } + } + + std::cout << ") " << std::endl; + } + + /** + * Dump all system info to stdout + */ + void dump_system() const + { + std::cout << "Registered Types: " << std::endl; + std::vector > types = get_types(); + for (std::vector >::const_iterator itr = types.begin(); + itr != types.end(); + ++itr) + { + std::cout << itr->first << ": "; + std::cout << itr->second.bare_name(); + std::cout << std::endl; + } + + std::cout << std::endl; + std::vector > funcs = get_functions(); + + std::cout << "Functions: " << std::endl; + for (std::vector >::const_iterator itr = funcs.begin(); + itr != funcs.end(); + ++itr) + { + dump_function(*itr); + } + std::cout << std::endl; + } + + /** + * return true if the Boxed_Value matches the registered type by name + */ + bool is_type(const std::string &user_typename, Boxed_Value r) const + { + try { + return get_type(user_typename) == r.get_type_info(); + } catch (const std::range_error &) { + return false; + } + } + + std::string type_name(Boxed_Value obj) const + { + return get_type_name(obj.get_type_info()); + } + + private: /** * Returns the current stack @@ -585,99 +679,6 @@ namespace chaiscript std::set m_reserved_words; }; - /** - * Dump object info to stdout - */ - void dump_object(Boxed_Value o, const Dispatch_Engine &e) - { - Type_Info ti = o.get_type_info(); - std::cout << (ti.is_const()?"const ":"") << e.get_type_name(ti) << std::endl; - } - - /** - * Dump type info to stdout - */ - void dump_type(const Type_Info &type, const Dispatch_Engine &e) - { - std::cout << (type.is_const()?"const ":"") << e.get_type_name(type); - } - - /** - * Dump function to stdout - */ - void dump_function(const std::pair &f, const Dispatch_Engine &e) - { - std::vector params = f.second->get_param_types(); - std::string annotation = f.second->annotation(); - - if (annotation.size() > 0) { - std::cout << annotation; - } - dump_type(params.front(), e); - std::cout << " " << f.first << "("; - - for (std::vector::const_iterator itr = params.begin() + 1; - itr != params.end(); - ) - { - dump_type(*itr, e); - ++itr; - - if (itr != params.end()) - { - std::cout << ", "; - } - } - - std::cout << ") " << std::endl; - } - - /** - * Dump all system info to stdout - */ - void dump_system(const Dispatch_Engine &s) - { - std::cout << "Registered Types: " << std::endl; - std::vector > types = s.get_types(); - for (std::vector >::const_iterator itr = types.begin(); - itr != types.end(); - ++itr) - { - std::cout << itr->first << ": "; - std::cout << itr->second.bare_name(); - std::cout << std::endl; - } - - std::cout << std::endl; - std::vector > funcs = s.get_functions(); - - std::cout << "Functions: " << std::endl; - for (std::vector >::const_iterator itr = funcs.begin(); - itr != funcs.end(); - ++itr) - { - dump_function(*itr, s); - } - std::cout << std::endl; - } - - /** - * return true if the Boxed_Value matches the registered type by name - */ - bool is_type(const Dispatch_Engine &e, const std::string &user_typename, Boxed_Value r) - { - try { - return e.get_type(user_typename) == r.get_type_info(); - } catch (const std::range_error &) { - return false; - } - } - - std::string type_name(const Dispatch_Engine &e, Boxed_Value obj) - { - return e.get_type_name(obj.get_type_info()); - } - } #endif diff --git a/include/chaiscript/dispatchkit/dynamic_object.hpp b/include/chaiscript/dispatchkit/dynamic_object.hpp index ad1e21a..c617113 100644 --- a/include/chaiscript/dispatchkit/dynamic_object.hpp +++ b/include/chaiscript/dispatchkit/dynamic_object.hpp @@ -34,37 +34,19 @@ namespace chaiscript std::map m_attrs; }; - Boxed_Value dynamic_object_attribute(const std::string &t_type_name, const std::string &t_attr_name, - Dynamic_Object &t_do) + struct Dynamic_Object_Attribute { - if (t_do.get_type_name() != t_type_name) + static Boxed_Value func(const std::string &t_type_name, const std::string &t_attr_name, + Dynamic_Object &t_do) { - throw bad_boxed_cast("Dynamic object type mismatch"); - } - - return t_do.get_attr(t_attr_name); - } - - bool dynamic_object_typename_match(const std::vector &bvs, const std::string &name, - const boost::optional &ti) - { - if (bvs.size() > 0) - { - try { - const Dynamic_Object &d = boxed_cast(bvs[0]); - return name == "Dynamic_Object" || d.get_type_name() == name; - } catch (const std::bad_cast &) { - if (ti) - { - return bvs[0].get_type_info().bare_equal(*ti); - } else { - return false; - } + if (t_do.get_type_name() != t_type_name) + { + throw bad_boxed_cast("Dynamic object type mismatch"); } - } else { - return false; + + return t_do.get_attr(t_attr_name); } - } + }; /** * A Proxy_Function implementation designed for calling a function @@ -129,6 +111,27 @@ namespace chaiscript } private: + static bool dynamic_object_typename_match(const std::vector &bvs, const std::string &name, + const boost::optional &ti) + { + if (bvs.size() > 0) + { + try { + const Dynamic_Object &d = boxed_cast(bvs[0]); + return name == "Dynamic_Object" || d.get_type_name() == name; + } catch (const std::bad_cast &) { + if (ti) + { + return bvs[0].get_type_info().bare_equal(*ti); + } else { + return false; + } + } + } else { + return false; + } + } + std::string m_type_name; Proxy_Function m_func; boost::optional m_ti; diff --git a/include/chaiscript/dispatchkit/proxy_functions.hpp b/include/chaiscript/dispatchkit/proxy_functions.hpp index 521f851..36f51d6 100644 --- a/include/chaiscript/dispatchkit/proxy_functions.hpp +++ b/include/chaiscript/dispatchkit/proxy_functions.hpp @@ -458,7 +458,8 @@ namespace chaiscript * 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 */ - Boxed_Value dispatch(const std::vector > &funcs, + template + Boxed_Value dispatch(const Funcs &funcs, const std::vector &plist) { return dispatch(funcs.begin(), funcs.end(), plist); diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 7483cd8..78ddfdb 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -450,12 +450,12 @@ namespace chaiscript add(Bootstrap::bootstrap()); - engine.add(fun(boost::function(boost::bind(&dump_system, boost::ref(engine)))), "dump_system"); - engine.add(fun(boost::function(boost::bind(&dump_object, _1, boost::ref(engine)))), "dump_object"); - engine.add(fun(boost::function(boost::bind(&is_type, boost::ref(engine), _2, _1))), + engine.add(fun(boost::function(boost::bind(&Eval_Engine::dump_system, boost::ref(engine)))), "dump_system"); + engine.add(fun(boost::function(boost::bind(&Eval_Engine::dump_object, boost::ref(engine), _1))), "dump_object"); + engine.add(fun(boost::function(boost::bind(&Eval_Engine::is_type, boost::ref(engine), _2, _1))), "is_type"); - engine.add(fun(boost::function(boost::bind(&chaiscript::type_name, boost::ref(engine), _1))), + engine.add(fun(boost::function(boost::bind(&Eval_Engine::type_name, boost::ref(engine), _1))), "type_name"); engine.add(fun(boost::function(boost::bind(&Eval_Engine::function_exists, boost::ref(engine), _1))), "function_exists"); diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index daf53c1..d845a3a 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -246,7 +246,7 @@ namespace chaiscript template Boxed_Value eval_attr_decl(Eval_System &ss, const TokenPtr &node) { try { - ss.add(fun(boost::function(boost::bind(&dynamic_object_attribute, node->children[0]->text, + ss.add(fun(boost::function(boost::bind(&Dynamic_Object_Attribute::func, node->children[0]->text, node->children[1]->text, _1))), node->children[1]->text); }