diff --git a/chaiscript/chaiscript_eval.hpp b/chaiscript/chaiscript_eval.hpp index 623b7c8..eca4051 100644 --- a/chaiscript/chaiscript_eval.hpp +++ b/chaiscript/chaiscript_eval.hpp @@ -488,63 +488,65 @@ namespace chaiscript break; case (Token_Type::Def) : { std::vector param_names; + std::string annotation = node->annotation?node->annotation->text:""; + boost::shared_ptr guard; + size_t numparams = 0; + std::string function_name = node->children[0]->text; + TokenPtr guardnode; if ((node->children.size() > 2) && (node->children[1]->identifier == Token_Type::Arg_List)) { - for (i = 0; i < node->children[1]->children.size(); ++i) { + numparams = node->children[1]->children.size(); + for (i = 0; i < numparams; ++i) { param_names.push_back(node->children[1]->children[i]->text); } if (node->children.size() > 3) { - //Guarded function - boost::shared_ptr guard = boost::shared_ptr - (new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function, boost::ref(ss), node->children[2], param_names, _1), node->children[1]->children.size())); - - ss.register_function(boost::shared_ptr( - new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function, boost::ref(ss), node->children.back(), param_names, _1), node->children[1]->children.size(), - guard)), node->children[0]->text); - - } - else { - ss.register_function(boost::shared_ptr( - new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function, boost::ref(ss), node->children.back(), param_names, _1), node->children[1]->children.size())), node->children[0]->text); + guardnode = node->children[2]; } } else { //no parameters + numparams = 0; if (node->children.size() > 2) { - boost::shared_ptr guard = boost::shared_ptr - (new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function, boost::ref(ss), node->children[1], param_names, _1), 0)); - - ss.register_function(boost::shared_ptr( - new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function, boost::ref(ss), node->children.back(), param_names, _1), 0, - guard)), node->children[0]->text); - } - else { - ss.register_function(boost::shared_ptr( - new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function, boost::ref(ss), node->children.back(), param_names, _1), 0)), node->children[0]->text); + guardnode = node->children[1]; } } + + if (guardnode) { + guard = boost::shared_ptr + (new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function, + boost::ref(ss), guardnode, + param_names, _1), numparams)); + } + + ss.register_function(boost::shared_ptr + (new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function, + boost::ref(ss), node->children.back(), + param_names, _1), numparams, + annotation, guard)), function_name); + } break; case (Token_Type::Lambda) : { std::vector param_names; + size_t numparams = 0; if ((node->children.size() > 0) && (node->children[0]->identifier == Token_Type::Arg_List)) { - for (i = 0; i < node->children[0]->children.size(); ++i) { + numparams = node->children[0]->children.size(); + for (i = 0; i < numparams; ++i) { param_names.push_back(node->children[0]->children[i]->text); } - //retval = boost::shared_ptr(new dispatchkit::Proxy_Function_Impl >(&test)); - retval = dispatchkit::Boxed_Value(boost::shared_ptr( - new dispatchkit::Dynamic_Proxy_Function( - boost::bind(&eval_function, boost::ref(ss), node->children.back(), param_names, _1), node->children[0]->children.size()))); + } else { //no parameters - retval = dispatchkit::Boxed_Value(boost::shared_ptr( - new dispatchkit::Dynamic_Proxy_Function( - boost::bind(&eval_function, boost::ref(ss), node->children.back(), param_names, _1), 0))); + numparams = 0; } + + retval = dispatchkit::Boxed_Value(boost::shared_ptr( + new dispatchkit::Dynamic_Proxy_Function( + boost::bind(&eval_function, boost::ref(ss), node->children.back(), param_names, _1), numparams))); } break; diff --git a/dispatchkit/bootstrap.hpp b/dispatchkit/bootstrap.hpp index 9f8f636..2be438a 100644 --- a/dispatchkit/bootstrap.hpp +++ b/dispatchkit/bootstrap.hpp @@ -6,6 +6,9 @@ namespace dispatchkit { + /** + * Set of helper functions for common operators + */ template Ret add(P1 p1, P2 p2) { @@ -40,46 +43,12 @@ namespace dispatchkit return p1 % p2; } - - template - bool bool_and(P1 p1, P2 p2) - { - return p1 && p2; - } - - template - bool bool_or(P1 p1, P2 p2) - { - return p1 || p2; - } - template P1 &assign(P1 &p1, const P2 &p2) { return (p1 = p2); } - template - P1 &assign_pod(P1 &p1, Boxed_POD_Value v) - { - if (v.m_isfloat) - { - return (p1 = v.d); - } else { - return (p1 = v.i); - } - } - - template - P1 construct_pod(Boxed_POD_Value v) - { - if (v.m_isfloat) - { - return (v.d); - } else { - return (v.i); - } - } template @@ -127,35 +96,12 @@ namespace dispatchkit return (p1 *= p2); } - template - P1 ×equal_pod(P1 &p1, Boxed_POD_Value r) - { - if (r.m_isfloat) - { - return (p1 *= r.d); - } else { - return (p1 *= r.i); - } - } - - template P1 ÷sequal(P1 &p1, const P2 &p2) { return (p1 /= p2); } - template - P1 ÷sequal_pod(P1 &p1, Boxed_POD_Value r) - { - if (r.m_isfloat) - { - return (p1 /= r.d); - } else { - return (p1 /= r.i); - } - } - template P1 &addsequal(P1 &p1, const P2 &p2) @@ -163,33 +109,13 @@ namespace dispatchkit return (p1 += p2); } - template - P1 &addsequal_pod(P1 &p1, Boxed_POD_Value r) - { - if (r.m_isfloat) - { - return (p1 += r.d); - } else { - return (p1 += r.i); - } - } - template P1 &subtractsequal(P1 &p1, const P2 &p2) { return (p1 -= p2); } - template - P1 &subtractsequal_pod(P1 &p1, Boxed_POD_Value r) - { - if (r.m_isfloat) - { - return (p1 -= r.d); - } else { - return (p1 -= r.i); - } - } + template P1 &prefixincrement(P1 &p1) @@ -215,36 +141,122 @@ namespace dispatchkit return (p1); } + /* Special helpers for generating generic "POD" type operators + * The POD operators are needed for general support of C++ POD + * types without iterating out all possible combinations of operators + * (<, >, +, +=, *=, \=, -, <=, >=, ==) and types + * (char, uint8_t, int8_t, uint16_t, int16_t...) + */ + template + P1 &assign_pod(P1 &p1, Boxed_POD_Value v) + { + if (v.m_isfloat) + { + return (p1 = v.d); + } else { + return (p1 = v.i); + } + } - //Add canonical forms of operators + template + P1 construct_pod(Boxed_POD_Value v) + { + if (v.m_isfloat) + { + return (v.d); + } else { + return (v.i); + } + } + + template + P1 ×equal_pod(P1 &p1, Boxed_POD_Value r) + { + if (r.m_isfloat) + { + return (p1 *= r.d); + } else { + return (p1 *= r.i); + } + } + + template + P1 ÷sequal_pod(P1 &p1, Boxed_POD_Value r) + { + if (r.m_isfloat) + { + return (p1 /= r.d); + } else { + return (p1 /= r.i); + } + } + + template + P1 &addsequal_pod(P1 &p1, Boxed_POD_Value r) + { + if (r.m_isfloat) + { + return (p1 += r.d); + } else { + return (p1 += r.i); + } + } + + template + P1 &subtractsequal_pod(P1 &p1, Boxed_POD_Value r) + { + if (r.m_isfloat) + { + return (p1 -= r.d); + } else { + return (p1 -= r.i); + } + } + + + /** + * Add canonical form of "=" for type T + */ template void add_oper_equals(Dispatch_Engine &s) { register_function(s, &equals, "="); } + /** + * Add canonical form of "+" for type T + */ template void add_oper_add(Dispatch_Engine &s) { register_function(s, &add, "+"); } + /** + * Add canonical form of "+=" for type T + */ template void add_oper_add_equals(Dispatch_Engine &s) { register_function(s, &addsequal, "+="); } + /** + * Add canonical form of "-" for type T + */ template void add_oper_subtract(Dispatch_Engine &s) { register_function(s, &subtract, "-"); } + /** + * Add canonical form of "/" for type T + */ template void add_oper_divide(Dispatch_Engine &s) { - register_function(s, ÷, "-"); + register_function(s, ÷, "/"); } template diff --git a/dispatchkit/proxy_functions.hpp b/dispatchkit/proxy_functions.hpp index 06b1303..8a166c8 100644 --- a/dispatchkit/proxy_functions.hpp +++ b/dispatchkit/proxy_functions.hpp @@ -114,8 +114,6 @@ namespace dispatchkit namespace dispatchkit { - - class Proxy_Function { public: @@ -131,21 +129,21 @@ namespace dispatchkit public: guard_error() throw() : std::runtime_error("Guard evaluation failed") - { - } + { } virtual ~guard_error() throw() - { - } + { } }; class Dynamic_Proxy_Function : public Proxy_Function { public: - Dynamic_Proxy_Function(const boost::function &)> &t_f, - int t_arity=-1, - const boost::shared_ptr &t_guard = boost::shared_ptr()) - : m_f(t_f), m_arity(t_arity), m_guard(t_guard) + Dynamic_Proxy_Function( + const boost::function &)> &t_f, + int t_arity=-1, + const std::string &t_description = "", + const boost::shared_ptr &t_guard = boost::shared_ptr()) + : m_f(t_f), m_arity(t_arity), m_description(t_description), m_guard(t_guard) { } @@ -203,6 +201,7 @@ namespace dispatchkit boost::function &)> m_f; int m_arity; + std::string m_description; boost::shared_ptr m_guard; }; diff --git a/samples/sensors.chai b/samples/sensors.chai index e83db1b..2db6d7a 100644 --- a/samples/sensors.chai +++ b/samples/sensors.chai @@ -46,6 +46,7 @@ def update_cpu_state(state, statfile, cpuname) state[cpuname + ".softirq"] = softirq; } +# annotation def update_state(state) { var file = load_text_file("/proc/stat"); @@ -61,7 +62,7 @@ initialize_cpu_sensor(global_state, "cpu", sensor_manager); initialize_cpu_sensor(global_state, "cpu0", sensor_manager); initialize_cpu_sensor(global_state, "cpu1", sensor_manager); -sensor_manager.add_sensor("cpu", 500, global_state, function(state) { update_state(state); state["cpu"]; } ) -sensor_manager.add_sensor("cpu0", 500, global_state, function(state) { state["cpu0"]; } ) -sensor_manager.add_sensor("cpu1", 500, global_state, function(state) { state["cpu1"]; } ) +sensor_manager.add_sensor("cpu", 500, global_state, fun(state) { update_state(state); state["cpu"]; } ) +sensor_manager.add_sensor("cpu0", 500, global_state, fun(state) { state["cpu0"]; } ) +sensor_manager.add_sensor("cpu1", 500, global_state, fun(state) { state["cpu1"]; } )