diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 89613e86..edca3e1f 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -361,6 +361,8 @@ public: void TypeError(const Str& msg){ _error("TypeError", msg); } void IndexError(const Str& msg){ _error("IndexError", msg); } void ValueError(const Str& msg){ _error("ValueError", msg); } + void ZeroDivisionError(const Str& msg){ _error("ZeroDivisionError", msg); } + void ZeroDivisionError(){ _error("ZeroDivisionError", "division by zero"); } void NameError(StrName name){ _error("NameError", fmt("name ", name.escape() + " is not defined")); } void UnboundLocalError(StrName name){ _error("UnboundLocalError", fmt("local variable ", name.escape() + " referenced before assignment")); } void KeyError(PyObject* obj){ _error("KeyError", PK_OBJ_GET(Str, py_repr(obj))); } diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 4510655d..6fa99362 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -383,7 +383,11 @@ void init_builtins(VM* _vm) { i64 lhs, rhs; if(try_cast_int(lhs_, &lhs) && try_cast_int(rhs_, &rhs)){ bool flag = false; - if(rhs < 0) {flag = true; rhs = -rhs;} + if(rhs < 0) { + if(lhs == 0) vm->ZeroDivisionError("0.0 cannot be raised to a negative power"); + flag = true; + rhs = -rhs; + } i64 ret = 1; while(rhs){ if(rhs & 1) ret *= lhs;