mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2026-02-06 17:59:56 +08:00
Remove guards from catch blocks
This commit is contained in:
parent
d59350d356
commit
92ae85c3e8
@ -1280,7 +1280,7 @@ namespace chaiscript
|
|||||||
auto &catch_block = *this->children[i];
|
auto &catch_block = *this->children[i];
|
||||||
|
|
||||||
if (catch_block.children.size() == 1) {
|
if (catch_block.children.size() == 1) {
|
||||||
//No variable capture, no guards
|
//No variable capture
|
||||||
retval = catch_block.children[0]->eval(t_ss);
|
retval = catch_block.children[0]->eval(t_ss);
|
||||||
break;
|
break;
|
||||||
} else if (catch_block.children.size() == 2 || catch_block.children.size() == 3) {
|
} 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);
|
t_ss.add_object(name, t_except);
|
||||||
|
|
||||||
if (catch_block.children.size() == 2) {
|
if (catch_block.children.size() == 2) {
|
||||||
//Variable capture, no guards
|
//Variable capture
|
||||||
retval = catch_block.children[1]->eval(t_ss);
|
retval = catch_block.children[1]->eval(t_ss);
|
||||||
break;
|
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 {
|
else {
|
||||||
|
|||||||
@ -1715,11 +1715,6 @@ namespace chaiscript
|
|||||||
if (!(Arg() && Char(')'))) {
|
if (!(Arg() && Char(')'))) {
|
||||||
throw exception::eval_error("Incomplete 'catch' expression", File_Position(m_position.line, m_position.col), *m_filename);
|
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()) {}
|
while (Eol()) {}
|
||||||
|
|||||||
@ -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);
|
|
||||||
@ -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);
|
|
||||||
@ -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);
|
|
||||||
Loading…
x
Reference in New Issue
Block a user