Remove guards from catch blocks

This commit is contained in:
Jason Turner 2017-11-18 19:08:14 -07:00
parent d59350d356
commit 92ae85c3e8
5 changed files with 2 additions and 126 deletions

View File

@ -1280,7 +1280,7 @@ namespace chaiscript
auto &catch_block = *this->children[i];
if (catch_block.children.size() == 1) {
//No variable capture, no guards
//No variable capture
retval = catch_block.children[0]->eval(t_ss);
break;
} else if (catch_block.children.size() == 2 || catch_block.children.size() == 3) {
@ -1293,27 +1293,10 @@ namespace chaiscript
t_ss.add_object(name, t_except);
if (catch_block.children.size() == 2) {
//Variable capture, no guards
//Variable capture
retval = catch_block.children[1]->eval(t_ss);
break;
}
else if (catch_block.children.size() == 3) {
//Variable capture, guards
bool guard = false;
try {
guard = boxed_cast<bool>(catch_block.children[1]->eval(t_ss));
} catch (const exception::bad_boxed_cast &) {
if (this->children.back()->identifier == AST_Node_Type::Finally) {
this->children.back()->children[0]->eval(t_ss);
}
throw exception::eval_error("Guard condition not boolean");
}
if (guard) {
retval = catch_block.children[2]->eval(t_ss);
break;
}
}
}
}
else {

View File

@ -1715,11 +1715,6 @@ namespace chaiscript
if (!(Arg() && Char(')'))) {
throw exception::eval_error("Incomplete 'catch' expression", File_Position(m_position.line, m_position.col), *m_filename);
}
if (Char(':')) {
if (!Operator()) {
throw exception::eval_error("Missing guard expression for catch", File_Position(m_position.line, m_position.col), *m_filename);
}
}
}
while (Eol()) {}

View File

@ -1,34 +0,0 @@
var results = [];
for (var i = 2; i < 6; ++i) {
try {
throw(i)
}
catch(e) : e < 2 {
results.push_back("c1: " + e.to_string());
}
catch(e) : e < 4 {
results.push_back("c2: " + e.to_string());
}
catch(e) {
results.push_back("c3: " + e.to_string());
}
catch {
// Should never get called
assert_equal(false, true)
}
}
try {
throw(3)
}
catch(e) : e < 3
{
// Should never get called
assert_equal(false, true);
}
catch {
results.push_back("defaultcatch");
}
assert_equal(["c2: 2", "c2: 3", "c3: 4", "c3: 5", "defaultcatch"], results);

View File

@ -1,34 +0,0 @@
auto results = [];
for (auto i = 2; i < 6; ++i) {
try {
throw(i)
}
catch(e) : e < 2 {
results.push_back("c1: " + e.to_string());
}
catch(e) : e < 4 {
results.push_back("c2: " + e.to_string());
}
catch(e) {
results.push_back("c3: " + e.to_string());
}
catch {
// Should never get called
assert_equal(false, true)
}
}
try {
throw(3)
}
catch(e) : e < 3
{
// Should never get called
assert_equal(false, true);
}
catch {
results.push_back("defaultcatch");
}
assert_equal(["c2: 2", "c2: 3", "c3: 4", "c3: 5", "defaultcatch"], results);

View File

@ -1,34 +0,0 @@
auto results = [];
for (auto i = 2; i < 6; ++i) {
try {
throw(i)
}
catch(int e) : e < 2 {
results.push_back("c1: " + e.to_string());
}
catch(int e) : e < 4 {
results.push_back("c2: " + e.to_string());
}
catch(e) {
results.push_back("c3: " + e.to_string());
}
catch {
// Should never get called
assert_equal(false, true)
}
}
try {
throw(3)
}
catch(int e) : e < 3
{
// Should never get called
assert_equal(false, true);
}
catch {
results.push_back("defaultcatch");
}
assert_equal(["c2: 2", "c2: 3", "c3: 4", "c3: 5", "defaultcatch"], results);