blueloveTH %!s(int64=2) %!d(string=hai) anos
pai
achega
bb96279e9b
Modificáronse 2 ficheiros con 7 adicións e 1 borrados
  1. 2 0
      include/pocketpy/vm.h
  2. 5 1
      src/pocketpy.cpp

+ 2 - 0
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))); }

+ 5 - 1
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;