Add support for annotations to dispatchkit, and reflect that support in chaiscript.

Also, some documentation cleanups.
This commit is contained in:
Jason Turner 2009-07-08 02:03:25 +00:00
parent bea05c6ea0
commit a8d74cea6f
4 changed files with 138 additions and 124 deletions

View File

@ -488,63 +488,65 @@ namespace chaiscript
break;
case (Token_Type::Def) : {
std::vector<std::string> param_names;
std::string annotation = node->annotation?node->annotation->text:"";
boost::shared_ptr<dispatchkit::Dynamic_Proxy_Function> 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<dispatchkit::Dynamic_Proxy_Function> guard = boost::shared_ptr<dispatchkit::Dynamic_Proxy_Function>
(new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children[2], param_names, _1), node->children[1]->children.size()));
ss.register_function(boost::shared_ptr<dispatchkit::Proxy_Function>(
new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, 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<dispatchkit::Proxy_Function>(
new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, 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<dispatchkit::Dynamic_Proxy_Function> guard = boost::shared_ptr<dispatchkit::Dynamic_Proxy_Function>
(new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children[1], param_names, _1), 0));
ss.register_function(boost::shared_ptr<dispatchkit::Proxy_Function>(
new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), 0,
guard)), node->children[0]->text);
}
else {
ss.register_function(boost::shared_ptr<dispatchkit::Proxy_Function>(
new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), 0)), node->children[0]->text);
guardnode = node->children[1];
}
}
if (guardnode) {
guard = boost::shared_ptr<dispatchkit::Dynamic_Proxy_Function>
(new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>,
boost::ref(ss), guardnode,
param_names, _1), numparams));
}
ss.register_function(boost::shared_ptr<dispatchkit::Proxy_Function>
(new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>,
boost::ref(ss), node->children.back(),
param_names, _1), numparams,
annotation, guard)), function_name);
}
break;
case (Token_Type::Lambda) : {
std::vector<std::string> 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<dispatchkit::Proxy_Function>(new dispatchkit::Proxy_Function_Impl<boost::function<void (const std::string &)> >(&test));
retval = dispatchkit::Boxed_Value(boost::shared_ptr<dispatchkit::Proxy_Function>(
new dispatchkit::Dynamic_Proxy_Function(
boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), node->children[0]->children.size())));
}
else {
//no parameters
retval = dispatchkit::Boxed_Value(boost::shared_ptr<dispatchkit::Proxy_Function>(
new dispatchkit::Dynamic_Proxy_Function(
boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), 0)));
numparams = 0;
}
retval = dispatchkit::Boxed_Value(boost::shared_ptr<dispatchkit::Proxy_Function>(
new dispatchkit::Dynamic_Proxy_Function(
boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), numparams)));
}
break;

View File

@ -6,6 +6,9 @@
namespace dispatchkit
{
/**
* Set of helper functions for common operators
*/
template<typename Ret, typename P1, typename P2>
Ret add(P1 p1, P2 p2)
{
@ -40,46 +43,12 @@ namespace dispatchkit
return p1 % p2;
}
template<typename P1, typename P2>
bool bool_and(P1 p1, P2 p2)
{
return p1 && p2;
}
template<typename P1, typename P2>
bool bool_or(P1 p1, P2 p2)
{
return p1 || p2;
}
template<typename P1, typename P2>
P1 &assign(P1 &p1, const P2 &p2)
{
return (p1 = p2);
}
template<typename P1>
P1 &assign_pod(P1 &p1, Boxed_POD_Value v)
{
if (v.m_isfloat)
{
return (p1 = v.d);
} else {
return (p1 = v.i);
}
}
template<typename P1>
P1 construct_pod(Boxed_POD_Value v)
{
if (v.m_isfloat)
{
return (v.d);
} else {
return (v.i);
}
}
template<typename P1, typename P2>
@ -127,35 +96,12 @@ namespace dispatchkit
return (p1 *= p2);
}
template<typename P1>
P1 &timesequal_pod(P1 &p1, Boxed_POD_Value r)
{
if (r.m_isfloat)
{
return (p1 *= r.d);
} else {
return (p1 *= r.i);
}
}
template<typename P1, typename P2>
P1 &dividesequal(P1 &p1, const P2 &p2)
{
return (p1 /= p2);
}
template<typename P1>
P1 &dividesequal_pod(P1 &p1, Boxed_POD_Value r)
{
if (r.m_isfloat)
{
return (p1 /= r.d);
} else {
return (p1 /= r.i);
}
}
template<typename P1, typename P2>
P1 &addsequal(P1 &p1, const P2 &p2)
@ -163,33 +109,13 @@ namespace dispatchkit
return (p1 += p2);
}
template<typename P1>
P1 &addsequal_pod(P1 &p1, Boxed_POD_Value r)
{
if (r.m_isfloat)
{
return (p1 += r.d);
} else {
return (p1 += r.i);
}
}
template<typename P1, typename P2>
P1 &subtractsequal(P1 &p1, const P2 &p2)
{
return (p1 -= p2);
}
template<typename P1>
P1 &subtractsequal_pod(P1 &p1, Boxed_POD_Value r)
{
if (r.m_isfloat)
{
return (p1 -= r.d);
} else {
return (p1 -= r.i);
}
}
template<typename P1>
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<typename P1>
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<typename P1>
P1 construct_pod(Boxed_POD_Value v)
{
if (v.m_isfloat)
{
return (v.d);
} else {
return (v.i);
}
}
template<typename P1>
P1 &timesequal_pod(P1 &p1, Boxed_POD_Value r)
{
if (r.m_isfloat)
{
return (p1 *= r.d);
} else {
return (p1 *= r.i);
}
}
template<typename P1>
P1 &dividesequal_pod(P1 &p1, Boxed_POD_Value r)
{
if (r.m_isfloat)
{
return (p1 /= r.d);
} else {
return (p1 /= r.i);
}
}
template<typename P1>
P1 &addsequal_pod(P1 &p1, Boxed_POD_Value r)
{
if (r.m_isfloat)
{
return (p1 += r.d);
} else {
return (p1 += r.i);
}
}
template<typename P1>
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<typename T>
void add_oper_equals(Dispatch_Engine &s)
{
register_function(s, &equals<const T&, const T&>, "=");
}
/**
* Add canonical form of "+" for type T
*/
template<typename T>
void add_oper_add(Dispatch_Engine &s)
{
register_function(s, &add<T, const T&, const T&>, "+");
}
/**
* Add canonical form of "+=" for type T
*/
template<typename T>
void add_oper_add_equals(Dispatch_Engine &s)
{
register_function(s, &addsequal<T, T>, "+=");
}
/**
* Add canonical form of "-" for type T
*/
template<typename T>
void add_oper_subtract(Dispatch_Engine &s)
{
register_function(s, &subtract<T, const T&, const T&>, "-");
}
/**
* Add canonical form of "/" for type T
*/
template<typename T>
void add_oper_divide(Dispatch_Engine &s)
{
register_function(s, &divide<T, const T&, const T&>, "-");
register_function(s, &divide<T, const T&, const T&>, "/");
}
template<typename T>

View File

@ -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<Boxed_Value (const std::vector<Boxed_Value> &)> &t_f,
int t_arity=-1,
const boost::shared_ptr<Proxy_Function> &t_guard = boost::shared_ptr<Proxy_Function>())
: m_f(t_f), m_arity(t_arity), m_guard(t_guard)
Dynamic_Proxy_Function(
const boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> &t_f,
int t_arity=-1,
const std::string &t_description = "",
const boost::shared_ptr<Proxy_Function> &t_guard = boost::shared_ptr<Proxy_Function>())
: m_f(t_f), m_arity(t_arity), m_description(t_description), m_guard(t_guard)
{
}
@ -203,6 +201,7 @@ namespace dispatchkit
boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f;
int m_arity;
std::string m_description;
boost::shared_ptr<Proxy_Function> m_guard;
};

View File

@ -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"]; } )