From 90aa53bdc657a80cf480caaf8887262814406541 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 8 Jul 2009 04:22:47 +0000 Subject: [PATCH] dump_system() looks nice now, lots of little updates. Annotations are displayed as well, but they do not work for the prelude, I don't know why. Any attempt to add them invalidates the function definition in question. sensors.chai does work, however --- chaiscript/chaiscript_prelude.hpp | 4 +-- dispatchkit/bootstrap.hpp | 4 ++- dispatchkit/bootstrap_stl.hpp | 12 +++++++- dispatchkit/dispatchkit.hpp | 50 +++++++++++++++++++++++-------- dispatchkit/proxy_functions.hpp | 34 ++++++++++++++++++++- samples/sensors.chai | 2 ++ 6 files changed, 89 insertions(+), 17 deletions(-) diff --git a/chaiscript/chaiscript_prelude.hpp b/chaiscript/chaiscript_prelude.hpp index c3fbc894..d2cfeb47 100644 --- a/chaiscript/chaiscript_prelude.hpp +++ b/chaiscript/chaiscript_prelude.hpp @@ -9,10 +9,10 @@ //by C++, so CODE_STRING, takes two expressions and adds in the missing comma #define CODE_STRING(x, y) #x ", " #y -#define chaiscript_prelude CODE_STRING(\ +#define chaiscript_prelude CODE_STRING( \n\n \ def to_string(x) : call_exists(first, x) && call_exists(second, x) { \ "<" + x.first.to_string() + ", " + x.second.to_string() + ">"; \ -} \ +} \n \n \ def to_string(x) : call_exists(range, x) && !x.is_type("string"){ \ "[" + x.join(", ") + "]"; \ } \ diff --git a/dispatchkit/bootstrap.hpp b/dispatchkit/bootstrap.hpp index 2be438a4..96f230cf 100644 --- a/dispatchkit/bootstrap.hpp +++ b/dispatchkit/bootstrap.hpp @@ -532,6 +532,8 @@ namespace dispatchkit { s.register_type("void"); s.register_type("bool"); + s.register_type("Object"); + s.register_type("PODObject"); s.register_type("function"); add_basic_constructors(s, "bool"); @@ -556,7 +558,7 @@ namespace dispatchkit register_function(s, &println, "println_string"); s.register_function(boost::function(boost::bind(&dump_system, boost::ref(s))), "dump_system"); - s.register_function(boost::function(boost::bind(&dump_object, _1)), "dump_object"); + s.register_function(boost::function(boost::bind(&dump_object, _1, boost::ref(s))), "dump_object"); s.register_function(boost::function(boost::bind(&is_type, boost::ref(s), _2, _1)), "is_type"); diff --git a/dispatchkit/bootstrap_stl.hpp b/dispatchkit/bootstrap_stl.hpp index 6b2dcfb4..efa0c6cd 100644 --- a/dispatchkit/bootstrap_stl.hpp +++ b/dispatchkit/bootstrap_stl.hpp @@ -55,11 +55,19 @@ namespace dispatchkit template void bootstrap_input_range(Dispatch_Engine &system, const std::string &type) { + system.register_type >(type+"_Range"); + system.register_type(type+"_Iterator"); + system.register_function(build_constructor, ContainerType &>(), "range"); system.register_function(build_constructor, typename ContainerType::iterator>(), "range"); + + typedef std::pair ItrPair; + system.register_function(build_constructor, - const std::pair &>(), "range"); + const ItrPair &>(), "range"); + system.register_type(type+"_Iterator_Pair"); + register_function(system, &Input_Range::empty, "empty"); @@ -158,6 +166,8 @@ namespace dispatchkit template void bootstrap_pair(Dispatch_Engine &system, const std::string &type) { + system.register_type(type); + register_member(system, &PairType::first, "first"); register_member(system, &PairType::second, "second"); diff --git a/dispatchkit/dispatchkit.hpp b/dispatchkit/dispatchkit.hpp index a83efac2..2b6fbbb3 100644 --- a/dispatchkit/dispatchkit.hpp +++ b/dispatchkit/dispatchkit.hpp @@ -63,6 +63,11 @@ namespace dispatchkit return false; } + virtual std::string annotation() const + { + return ""; + } + private: std::vector > > m_funcs; }; @@ -186,6 +191,21 @@ namespace dispatchkit } } + std::string get_type_name(const Type_Info &ti) const + { + for (Type_Name_Map::const_iterator itr = m_types.begin(); + itr != m_types.end(); + ++itr) + { + if (itr->second.m_bare_type_info == ti.m_bare_type_info) + { + return itr->first; + } + } + + return ti.m_bare_type_info->name(); + } + std::vector get_types() const { return std::vector(m_types.begin(), m_types.end()); @@ -253,32 +273,38 @@ namespace dispatchkit Boxed_Value m_place_holder; }; - void dump_object(Boxed_Value o) + void dump_object(Boxed_Value o, const Dispatch_Engine &e) { - std::cout << o.get_type_info().m_type_info->name() << std::endl; + std::cout << e.get_type_name(o.get_type_info()) << std::endl; } - void dump_type(const Type_Info &type) + void dump_type(const Type_Info &type, const Dispatch_Engine &e) { - std::cout << type.m_bare_type_info->name(); + std::cout << e.get_type_name(type); } - void dump_function(const Dispatch_Engine::Function_Map::value_type &f) + void dump_function(const Dispatch_Engine::Function_Map::value_type &f, const Dispatch_Engine &e) { std::vector params = f.second->get_param_types(); - dump_type(params.front()); + dump_type(params.front(), e); std::cout << " " << f.first << "("; for (std::vector::const_iterator itr = params.begin() + 1; itr != params.end(); - ++itr) + ) { - dump_type(*itr); - std::cout << ", "; + dump_type(*itr, e); + ++itr; + + if (itr != params.end()) + { + std::cout << ", "; + } + } - std::cout << ")" << std::endl; + std::cout << ") " << f.second->annotation() << std::endl; } void dump_system(const Dispatch_Engine &s) @@ -290,7 +316,7 @@ namespace dispatchkit ++itr) { std::cout << itr->first << ": "; - dump_type(itr->second); + std::cout << itr->second.m_bare_type_info->name(); std::cout << std::endl; } @@ -302,7 +328,7 @@ namespace dispatchkit itr != funcs.end(); ++itr) { - dump_function(*itr); + dump_function(*itr, s); } std::cout << std::endl; } diff --git a/dispatchkit/proxy_functions.hpp b/dispatchkit/proxy_functions.hpp index 8a166c89..cce4b4e7 100644 --- a/dispatchkit/proxy_functions.hpp +++ b/dispatchkit/proxy_functions.hpp @@ -122,6 +122,7 @@ namespace dispatchkit virtual std::vector get_param_types() const = 0; virtual bool operator==(const Proxy_Function &) const = 0; virtual bool types_match(const std::vector &types) const = 0; + virtual std::string annotation() const = 0; }; class guard_error : public std::runtime_error @@ -179,7 +180,26 @@ namespace dispatchkit virtual std::vector get_param_types() const { - return build_param_type_list(m_f); + std::vector types; + + types.push_back(Get_Type_Info::get()); + + if (m_arity >= 0) + { + for (int i = 0; i < m_arity; ++i) + { + types.push_back(Get_Type_Info::get()); + } + } else { + types.push_back(Get_Type_Info >::get()); + } + + return types; + } + + virtual std::string annotation() const + { + return m_description; } private: @@ -280,6 +300,12 @@ namespace dispatchkit return std::vector(); } + virtual std::string annotation() const + { + return ""; + } + + private: boost::shared_ptr m_f; std::vector m_args; @@ -321,6 +347,12 @@ namespace dispatchkit return compare_types(m_f, types); } + virtual std::string annotation() const + { + return ""; + } + + private: Func m_f; }; diff --git a/samples/sensors.chai b/samples/sensors.chai index 2db6d7a7..be9e4b98 100644 --- a/samples/sensors.chai +++ b/samples/sensors.chai @@ -56,6 +56,8 @@ def update_state(state) update_cpu_state(state, file, "cpu1"); } +dump_system() + var global_state = Map() initialize_cpu_sensor(global_state, "cpu", sensor_manager);