Fix default cmake build to use readline and gdb. Add simple try/catch/throw exceptions
This commit is contained in:
@@ -579,6 +579,40 @@ namespace chaiscript
|
||||
return retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates an if/elseif/else block
|
||||
*/
|
||||
template <typename Eval_System>
|
||||
Boxed_Value eval_try(Eval_System &ss, const TokenPtr &node) {
|
||||
Boxed_Value retval;
|
||||
retval = Boxed_Value();
|
||||
|
||||
ss.new_scope();
|
||||
try {
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
}
|
||||
catch (std::exception &) {
|
||||
// nothing
|
||||
std::cout << "DEBUG: std::exception caught" << std::endl;
|
||||
}
|
||||
catch (Boxed_Value &bv) {
|
||||
if (node->children.size() > 2) {
|
||||
if (node->children[1]->text == "catch") {
|
||||
if (node->children.size() > 3) {
|
||||
ss.add_object(node->children[2]->text, bv);
|
||||
retval = eval_token(ss, node->children[3]);
|
||||
}
|
||||
else {
|
||||
retval = eval_token(ss, node->children[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ss.pop_scope();
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates an if/elseif/else block
|
||||
*/
|
||||
@@ -924,6 +958,10 @@ namespace chaiscript
|
||||
return eval_dot_access(ss, node);
|
||||
break;
|
||||
|
||||
case(Token_Type::Try) :
|
||||
return eval_try(ss, node);
|
||||
break;
|
||||
|
||||
case(Token_Type::If) :
|
||||
return eval_if(ss, node);
|
||||
break;
|
||||
|
@@ -916,6 +916,60 @@ namespace chaiscript
|
||||
return retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a function definition from input
|
||||
*/
|
||||
bool Try() {
|
||||
bool retval = false;
|
||||
bool is_annotated = false;
|
||||
|
||||
TokenPtr annotation;
|
||||
|
||||
if (Annotation()) {
|
||||
while (Eol_());
|
||||
annotation = match_stack.back();
|
||||
match_stack.pop_back();
|
||||
is_annotated = true;
|
||||
}
|
||||
|
||||
int prev_stack_top = match_stack.size();
|
||||
|
||||
if (Keyword("try")) {
|
||||
retval = true;
|
||||
|
||||
while (Eol());
|
||||
|
||||
if (!Block()) {
|
||||
throw Eval_Error("Incomplete 'try' block", File_Position(line, col), filename);
|
||||
}
|
||||
|
||||
bool has_matches = true;
|
||||
while (has_matches) {
|
||||
while (Eol());
|
||||
has_matches = false;
|
||||
if (Keyword("catch", true)) {
|
||||
if (Char('(')) {
|
||||
if (!(Id(true) && Char(')'))) {
|
||||
throw Eval_Error("Incomplete 'catch' expression", File_Position(line, col), filename);
|
||||
}
|
||||
}
|
||||
|
||||
while (Eol());
|
||||
|
||||
if (!Block()) {
|
||||
throw Eval_Error("Incomplete 'catch' block", File_Position(line, col), filename);
|
||||
}
|
||||
|
||||
has_matches = true;
|
||||
}
|
||||
}
|
||||
|
||||
build_match(Token_Type::Try, prev_stack_top);
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an if/elseif/else block from input
|
||||
*/
|
||||
@@ -1550,6 +1604,14 @@ namespace chaiscript
|
||||
retval = true;
|
||||
saw_eol = true;
|
||||
}
|
||||
else if (Try()) {
|
||||
if (!saw_eol) {
|
||||
throw Eval_Error("Two function definitions missing line separator", match_stack.back());
|
||||
}
|
||||
has_more = true;
|
||||
retval = true;
|
||||
saw_eol = true;
|
||||
}
|
||||
else if (If()) {
|
||||
if (!saw_eol) {
|
||||
throw Eval_Error("Two function definitions missing line separator", match_stack.back());
|
||||
|
Reference in New Issue
Block a user