Simplify redundant bool condition checking
This commit is contained in:
@@ -152,7 +152,7 @@ if(MSVC)
|
|||||||
# how to workaround or fix the error. So I'm disabling it globally.
|
# how to workaround or fix the error. So I'm disabling it globally.
|
||||||
add_definitions(/wd4503)
|
add_definitions(/wd4503)
|
||||||
else()
|
else()
|
||||||
add_definitions(-Wall -Wextra -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Wcast-qual -Woverloaded-virtual -pedantic ${CPP11_FLAG})
|
add_definitions(-Wall -Wextra -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Wcast-qual -Wunused -Woverloaded-virtual -pedantic ${CPP11_FLAG})
|
||||||
|
|
||||||
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
|
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
|
||||||
add_definitions(-Weverything -Wno-c++98-compat -Wno-documentation -Wno-switch-enum -Wno-weak-vtables -Wno-sign-conversion -Wno-missing-prototypes -Wno-padded -Wno-missing-noreturn)
|
add_definitions(-Weverything -Wno-c++98-compat -Wno-documentation -Wno-switch-enum -Wno-weak-vtables -Wno-sign-conversion -Wno-missing-prototypes -Wno-padded -Wno-missing-noreturn)
|
||||||
|
@@ -451,6 +451,15 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool get_bool_condition(const Boxed_Value &t_bv) {
|
||||||
|
try {
|
||||||
|
return boxed_cast<bool>(t_bv);
|
||||||
|
}
|
||||||
|
catch (const exception::bad_boxed_cast &) {
|
||||||
|
throw exception::eval_error("Condition not boolean");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void replace_child(const AST_NodePtr &t_child, const AST_NodePtr &t_new_child)
|
void replace_child(const AST_NodePtr &t_child, const AST_NodePtr &t_new_child)
|
||||||
{
|
{
|
||||||
|
@@ -826,7 +826,7 @@ namespace chaiscript
|
|||||||
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
|
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while (boxed_cast<bool>(this->children[0]->eval(t_ss))) {
|
while (get_bool_condition(this->children[0]->eval(t_ss))) {
|
||||||
try {
|
try {
|
||||||
this->children[1]->eval(t_ss);
|
this->children[1]->eval(t_ss);
|
||||||
} catch (detail::Continue_Loop &) {
|
} catch (detail::Continue_Loop &) {
|
||||||
@@ -835,8 +835,6 @@ namespace chaiscript
|
|||||||
// the next condition test
|
// the next condition test
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (const exception::bad_boxed_cast &) {
|
|
||||||
throw exception::eval_error("While condition not boolean");
|
|
||||||
} catch (detail::Break_Loop &) {
|
} catch (detail::Break_Loop &) {
|
||||||
// loop was broken intentionally
|
// loop was broken intentionally
|
||||||
}
|
}
|
||||||
@@ -868,21 +866,15 @@ namespace chaiscript
|
|||||||
AST_Node(t_ast_node_text, AST_Node_Type::If, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
AST_Node(t_ast_node_text, AST_Node_Type::If, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
||||||
virtual ~Ternary_Cond_AST_Node() {}
|
virtual ~Ternary_Cond_AST_Node() {}
|
||||||
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE{
|
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE{
|
||||||
bool cond = false;
|
|
||||||
try {
|
|
||||||
cond = boxed_cast<bool>(this->children[0]->eval(t_ss));
|
|
||||||
}
|
|
||||||
catch (const exception::bad_boxed_cast &) {
|
|
||||||
throw exception::eval_error("Ternary if condition not boolean");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cond) {
|
if (get_bool_condition(this->children[0]->eval(t_ss))) {
|
||||||
return this->children[1]->eval(t_ss);
|
return this->children[1]->eval(t_ss);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return this->children[2]->eval(t_ss);
|
return this->children[2]->eval(t_ss);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct If_AST_Node : public AST_Node {
|
struct If_AST_Node : public AST_Node {
|
||||||
@@ -891,31 +883,20 @@ namespace chaiscript
|
|||||||
AST_Node(t_ast_node_text, AST_Node_Type::If, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
AST_Node(t_ast_node_text, AST_Node_Type::If, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
||||||
virtual ~If_AST_Node() {}
|
virtual ~If_AST_Node() {}
|
||||||
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE{
|
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE{
|
||||||
bool cond = false;
|
|
||||||
try {
|
|
||||||
cond = boxed_cast<bool>(this->children[0]->eval(t_ss));
|
|
||||||
}
|
|
||||||
catch (const exception::bad_boxed_cast &) {
|
|
||||||
throw exception::eval_error("If condition not boolean");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cond) {
|
if (get_bool_condition(this->children[0]->eval(t_ss))) {
|
||||||
return this->children[1]->eval(t_ss);
|
return this->children[1]->eval(t_ss);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (this->children.size() > 2) {
|
if (this->children.size() > 2) {
|
||||||
size_t i = 2;
|
size_t i = 2;
|
||||||
|
bool cond = false;
|
||||||
while ((!cond) && (i < this->children.size())) {
|
while ((!cond) && (i < this->children.size())) {
|
||||||
if (this->children[i]->text == "else") {
|
if (this->children[i]->text == "else") {
|
||||||
return this->children[i+1]->eval(t_ss);
|
return this->children[i+1]->eval(t_ss);
|
||||||
}
|
}
|
||||||
else if (this->children[i]->text == "else if") {
|
else if (this->children[i]->text == "else if") {
|
||||||
try {
|
cond = get_bool_condition(this->children[i+1]->eval(t_ss));
|
||||||
cond = boxed_cast<bool>(this->children[i+1]->eval(t_ss));
|
|
||||||
}
|
|
||||||
catch (const exception::bad_boxed_cast &) {
|
|
||||||
throw exception::eval_error("'else if' condition not boolean");
|
|
||||||
}
|
|
||||||
if (cond) {
|
if (cond) {
|
||||||
return this->children[i+2]->eval(t_ss);
|
return this->children[i+2]->eval(t_ss);
|
||||||
}
|
}
|
||||||
@@ -944,7 +925,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// while condition evals to true
|
// while condition evals to true
|
||||||
while (boxed_cast<bool>(this->children[1]->eval(t_ss))) {
|
while (get_bool_condition(this->children[1]->eval(t_ss))) {
|
||||||
try {
|
try {
|
||||||
// Body of Loop
|
// Body of Loop
|
||||||
this->children[3]->eval(t_ss);
|
this->children[3]->eval(t_ss);
|
||||||
@@ -958,9 +939,6 @@ namespace chaiscript
|
|||||||
this->children[2]->eval(t_ss);
|
this->children[2]->eval(t_ss);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (const exception::bad_boxed_cast &) {
|
|
||||||
throw exception::eval_error("For condition not boolean");
|
|
||||||
}
|
|
||||||
catch (detail::Break_Loop &) {
|
catch (detail::Break_Loop &) {
|
||||||
// loop broken
|
// loop broken
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user