From 6d4f2146b89d9d43eedb5326d0411a698b9e4ae9 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 2 Jul 2009 17:22:12 +0000 Subject: [PATCH] Added function guards --- chaiscript/chaiscript_eval.hpp | 36 +++++++++++++++++++++++++------- chaiscript/chaiscript_parser.hpp | 11 ++++++++-- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/chaiscript/chaiscript_eval.hpp b/chaiscript/chaiscript_eval.hpp index 755bb80..8d374ce 100644 --- a/chaiscript/chaiscript_eval.hpp +++ b/chaiscript/chaiscript_eval.hpp @@ -315,7 +315,7 @@ namespace chaiscript } catch(const dispatchkit::dispatch_error &e){ 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) { ss.set_stack(prev_stack); @@ -485,19 +485,41 @@ namespace chaiscript case (Token_Type::Def) : { std::vector 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) { param_names.push_back(node->children[1]->children[i]->text); } - 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); + + 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); + } } else { //no parameters - ss.register_function(boost::shared_ptr( - //no parameters - new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function, boost::ref(ss), node->children.back(), param_names, _1), 0)), node->children[0]->text); + 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); + } } } break; diff --git a/chaiscript/chaiscript_parser.hpp b/chaiscript/chaiscript_parser.hpp index e79642b..5d3c1e2 100644 --- a/chaiscript/chaiscript_parser.hpp +++ b/chaiscript/chaiscript_parser.hpp @@ -682,6 +682,13 @@ namespace chaiscript while (Eol()); + if (Char(':')) { + if (!Expression()) { + throw Parse_Error("Missing guard expression for function", File_Position(line, col), filename); + } + } + + while (Eol()); if (!Block()) { throw Parse_Error("Incomplete function definition", File_Position(line, col), filename); } @@ -870,7 +877,7 @@ namespace chaiscript std::string::iterator prev_pos = input_pos; unsigned int prev_stack_top = match_stack.size(); - if (Id(true)) { + if (Id(true) || Id_Literal()) { retval = true; bool has_more = true; @@ -1021,7 +1028,7 @@ namespace chaiscript bool Value() { 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; } else {