From 25b15a3449c39589855eb10a134e664082600902 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Fri, 9 Jan 2015 20:06:04 -0700 Subject: [PATCH] Only apply divide by zero protection to integers Also allow arithmetic error to bubble up to the caller. --- include/chaiscript/dispatchkit/bootstrap.hpp | 10 +++++++--- include/chaiscript/dispatchkit/boxed_number.hpp | 4 +++- include/chaiscript/language/chaiscript_eval.hpp | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/chaiscript/dispatchkit/bootstrap.hpp b/include/chaiscript/dispatchkit/bootstrap.hpp index 3e88daf..579b6ee 100644 --- a/include/chaiscript/dispatchkit/bootstrap.hpp +++ b/include/chaiscript/dispatchkit/bootstrap.hpp @@ -396,10 +396,9 @@ namespace chaiscript m->add(user_type(), "runtime_error"); m->add(chaiscript::base_class()); - m->add(constructor(), "runtime_error"); - m->add(fun(std::function(&what)), "what"); + m->add(fun(std::function(&what)), "what"); m->add(user_type(), "Dynamic_Object"); m->add(constructor(), "Dynamic_Object"); @@ -487,7 +486,12 @@ namespace chaiscript m->add(chaiscript::fun(&has_parse_tree), "has_parse_tree"); m->add(chaiscript::fun(&get_parse_tree), "get_parse_tree"); - m->add(chaiscript::base_class()); + m->add(chaiscript::user_type("eval_error")); + m->add(chaiscript::base_class()); + + m->add(chaiscript::user_type("arithmetic_error")); + m->add(chaiscript::base_class()); + // chaiscript::bootstrap::standard_library::vector_type > >("AST_NodeVector", m); diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index 69cfefa..150e76c 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -53,7 +53,9 @@ namespace chaiscript template static void check_divide_by_zero(T t) { - if(std::is_arithmetic::value) if(t==0) throw chaiscript::exception::arithmetic_error("divide by zero"); + if(std::is_integral::value && std::is_arithmetic::value && t==0) { + throw chaiscript::exception::arithmetic_error("divide by zero"); + } } #else template diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index be9aabc..eafb84b 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -93,6 +93,8 @@ namespace chaiscript // If it's an arithmetic operation we want to short circuit dispatch try{ return Boxed_Number::do_oper(t_oper, t_lhs, t_rhs); + } catch (const chaiscript::exception::arithmetic_error &) { + throw; } catch (...) { throw exception::eval_error("Error with numeric operator calling: " + t_oper_string); }