Check for division by zero in interpreter
This commit is contained in:
parent
9d7f81d7fc
commit
4b3f87718d
1 changed files with 10 additions and 0 deletions
|
|
@ -178,12 +178,22 @@ namespace pslang::interpreter
|
|||
case ast::binary_operation_type::division:
|
||||
if constexpr (!std::is_same_v<T, bool>)
|
||||
{
|
||||
if constexpr (std::is_integral_v<T>)
|
||||
{
|
||||
if (arg2.value == static_cast<T>(0))
|
||||
throw std::runtime_error("Division by zero");
|
||||
}
|
||||
return primitive_value(primitive_value_base<T>{static_cast<T>(arg1.value / arg2.value)});
|
||||
}
|
||||
break;
|
||||
case ast::binary_operation_type::remainder:
|
||||
if constexpr (!std::is_same_v<T, bool> && std::is_integral_v<T>)
|
||||
{
|
||||
if constexpr (std::is_integral_v<T>)
|
||||
{
|
||||
if (arg2.value == static_cast<T>(0))
|
||||
throw std::runtime_error("Division by zero");
|
||||
}
|
||||
return primitive_value(primitive_value_base<T>{static_cast<T>(arg1.value % arg2.value)});
|
||||
}
|
||||
break;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue