From d558019bb320bbe486073bb365ed8af06c7a9af3 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sat, 31 Jan 2015 13:41:29 -0700 Subject: [PATCH] Simplify logical && || operator eval --- .../chaiscript/language/chaiscript_eval.hpp | 41 ++----------------- unittests/logical_short_circuiting.chai | 21 ++++++++++ 2 files changed, 25 insertions(+), 37 deletions(-) create mode 100644 unittests/logical_short_circuiting.chai diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 21bb77a..c433e27 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -1483,26 +1483,8 @@ namespace chaiscript AST_Node(t_ast_node_text, AST_Node_Type::Logical_And, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } virtual ~Logical_And_AST_Node() {} virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE{ - Boxed_Value retval = this->children[0]->eval(t_ss); - - if (this->children.size() > 1) { - for (size_t i = 1; i < this->children.size(); i += 2) { - bool lhs; - try { - lhs = boxed_cast(retval); - } - catch (const exception::bad_boxed_cast &) { - throw exception::eval_error("Condition not boolean"); - } - if (lhs) { - retval = this->children[i+1]->eval(t_ss); - } - else { - retval = Boxed_Value(false); - } - } - } - return retval; + return const_var(get_bool_condition(this->children[0]->eval(t_ss)) + && get_bool_condition(this->children[2]->eval(t_ss))); } virtual std::string pretty_print() const CHAISCRIPT_OVERRIDE @@ -1517,23 +1499,8 @@ namespace chaiscript AST_Node(t_ast_node_text, AST_Node_Type::Logical_Or, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } virtual ~Logical_Or_AST_Node() {} virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE{ - Boxed_Value retval; - - retval = this->children[0]->eval(t_ss); - - if (this->children.size() > 1) { - for (size_t i = 1; i < this->children.size(); i += 2) { - bool lhs = boxed_cast(retval); - - if (lhs) { - retval = Boxed_Value(true); - } - else { - retval = this->children[i+1]->eval(t_ss); - } - } - } - return retval; + return const_var(get_bool_condition(this->children[0]->eval(t_ss)) + || get_bool_condition(this->children[2]->eval(t_ss))); } virtual std::string pretty_print() const CHAISCRIPT_OVERRIDE diff --git a/unittests/logical_short_circuiting.chai b/unittests/logical_short_circuiting.chai new file mode 100644 index 0000000..55cae32 --- /dev/null +++ b/unittests/logical_short_circuiting.chai @@ -0,0 +1,21 @@ + +def shouldnt_execute() +{ + assert(false) +} + + + +if (false && shouldnt_execute()) { +} + +if (true || shouldnt_execute()) { +} + + +if (false || false || false || true || shouldnt_execute()) { +} + + +if (true && true && true && false && shouldnt_execute()) { +}