From 76ac7c36fec81b164768a75bfb9446efd6faca4f Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sat, 31 Jan 2015 07:28:37 -0700 Subject: [PATCH] Simplify redundant bool condition checking --- CMakeLists.txt | 2 +- .../chaiscript/language/chaiscript_common.hpp | 9 +++++ .../chaiscript/language/chaiscript_eval.hpp | 36 ++++--------------- 3 files changed, 17 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 36b5698..f942eb0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,7 +152,7 @@ if(MSVC) # how to workaround or fix the error. So I'm disabling it globally. add_definitions(/wd4503) 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") 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) diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index e383d16..1a920bd 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -451,6 +451,15 @@ namespace chaiscript } } + static bool get_bool_condition(const Boxed_Value &t_bv) { + try { + return boxed_cast(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) { diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 33477d8..21bb77a 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -826,7 +826,7 @@ namespace chaiscript chaiscript::eval::detail::Scope_Push_Pop spp(t_ss); try { - while (boxed_cast(this->children[0]->eval(t_ss))) { + while (get_bool_condition(this->children[0]->eval(t_ss))) { try { this->children[1]->eval(t_ss); } catch (detail::Continue_Loop &) { @@ -835,8 +835,6 @@ namespace chaiscript // the next condition test } } - } catch (const exception::bad_boxed_cast &) { - throw exception::eval_error("While condition not boolean"); } catch (detail::Break_Loop &) { // 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) { } virtual ~Ternary_Cond_AST_Node() {} virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE{ - bool cond = false; - try { - cond = boxed_cast(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); } else { return this->children[2]->eval(t_ss); } } + }; 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) { } virtual ~If_AST_Node() {} virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE{ - bool cond = false; - try { - cond = boxed_cast(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); } else { if (this->children.size() > 2) { size_t i = 2; + bool cond = false; while ((!cond) && (i < this->children.size())) { if (this->children[i]->text == "else") { return this->children[i+1]->eval(t_ss); } else if (this->children[i]->text == "else if") { - try { - cond = boxed_cast(this->children[i+1]->eval(t_ss)); - } - catch (const exception::bad_boxed_cast &) { - throw exception::eval_error("'else if' condition not boolean"); - } + cond = get_bool_condition(this->children[i+1]->eval(t_ss)); if (cond) { return this->children[i+2]->eval(t_ss); } @@ -944,7 +925,7 @@ namespace chaiscript try { // while condition evals to true - while (boxed_cast(this->children[1]->eval(t_ss))) { + while (get_bool_condition(this->children[1]->eval(t_ss))) { try { // Body of Loop this->children[3]->eval(t_ss); @@ -958,9 +939,6 @@ namespace chaiscript this->children[2]->eval(t_ss); } } - catch (const exception::bad_boxed_cast &) { - throw exception::eval_error("For condition not boolean"); - } catch (detail::Break_Loop &) { // loop broken }