Added function guards

This commit is contained in:
Jonathan Turner
2009-07-02 17:22:12 +00:00
parent f6342ef2a8
commit 6d4f2146b8
2 changed files with 38 additions and 9 deletions

View File

@@ -315,7 +315,7 @@ namespace chaiscript
} }
catch(const dispatchkit::dispatch_error &e){ catch(const dispatchkit::dispatch_error &e){
ss.set_stack(prev_stack); ss.set_stack(prev_stack);
throw EvalError("Engine error: " + std::string(e.what()) + " on '" + node->children[0]->text + "'", node->children[0]); throw EvalError("Engine error: " + std::string(e.what()) + " with function '" + node->children[0]->text + "'", node->children[0]);
} }
catch(ReturnValue &rv) { catch(ReturnValue &rv) {
ss.set_stack(prev_stack); ss.set_stack(prev_stack);
@@ -485,19 +485,41 @@ namespace chaiscript
case (Token_Type::Def) : { case (Token_Type::Def) : {
std::vector<std::string> param_names; std::vector<std::string> param_names;
if ((node->children.size() > 1) && (node->children[1]->identifier == Token_Type::Arg_List)) { if ((node->children.size() > 2) && (node->children[1]->identifier == Token_Type::Arg_List)) {
for (i = 0; i < node->children[1]->children.size(); ++i) { for (i = 0; i < node->children[1]->children.size(); ++i) {
param_names.push_back(node->children[1]->children[i]->text); param_names.push_back(node->children[1]->children[i]->text);
} }
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); 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);
}
} }
else { else {
//no parameters //no parameters
ss.register_function(boost::shared_ptr<dispatchkit::Proxy_Function>(
//no parameters
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);
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);
}
} }
} }
break; break;

View File

@@ -682,6 +682,13 @@ namespace chaiscript
while (Eol()); while (Eol());
if (Char(':')) {
if (!Expression()) {
throw Parse_Error("Missing guard expression for function", File_Position(line, col), filename);
}
}
while (Eol());
if (!Block()) { if (!Block()) {
throw Parse_Error("Incomplete function definition", File_Position(line, col), filename); throw Parse_Error("Incomplete function definition", File_Position(line, col), filename);
} }
@@ -870,7 +877,7 @@ namespace chaiscript
std::string::iterator prev_pos = input_pos; std::string::iterator prev_pos = input_pos;
unsigned int prev_stack_top = match_stack.size(); unsigned int prev_stack_top = match_stack.size();
if (Id(true)) { if (Id(true) || Id_Literal()) {
retval = true; retval = true;
bool has_more = true; bool has_more = true;
@@ -1021,7 +1028,7 @@ namespace chaiscript
bool Value() { bool Value() {
if (Var_Decl() || Lambda() || Id_Fun_Array() || Num(true) || Prefix() || Quoted_String(true) || Single_Quoted_String(true) || if (Var_Decl() || Lambda() || Id_Fun_Array() || Num(true) || Prefix() || Quoted_String(true) || Single_Quoted_String(true) ||
Paren_Expression() || Inline_Container() || Id_Literal()) { Paren_Expression() || Inline_Container()) {
return true; return true;
} }
else { else {