Просмотр исходного кода

fix bool eq/ne

Update ceval.c

Update array2d.c
blueloveTH 11 часов назад
Родитель
Сommit
d6e0d07b72
4 измененных файлов с 45 добавлено и 38 удалено
  1. 18 26
      src/bindings/py_number.c
  2. 2 1
      src/interpreter/ceval.c
  3. 17 11
      src/modules/array2d.c
  4. 8 0
      tests/030_bool.py

+ 18 - 26
src/bindings/py_number.c

@@ -520,11 +520,23 @@ static bool bool__repr__(int argc, py_Ref argv) {
     return true;
 }
 
+static bool bool_try_cast_i64(py_Ref arg, py_i64* out) {
+    if (arg->type == tp_int) {
+        *out = py_toint(arg);
+        return true;
+    } else if (arg->type == tp_bool) {
+        *out = py_tobool(arg);
+        return true;
+    } else {
+        return false;
+    }
+}
+
 static bool bool__eq__(int argc, py_Ref argv) {
     PY_CHECK_ARGC(2);
-    bool lhs = py_tobool(&argv[0]);
-    if(argv[1].type == tp_bool) {
-        bool rhs = py_tobool(&argv[1]);
+    py_i64 lhs = (py_i64)py_tobool(&argv[0]);
+    py_i64 rhs;
+    if(bool_try_cast_i64(py_arg(1), &rhs)) {
         py_newbool(py_retval(), lhs == rhs);
     } else {
         py_newnotimplemented(py_retval());
@@ -534,9 +546,9 @@ static bool bool__eq__(int argc, py_Ref argv) {
 
 static bool bool__ne__(int argc, py_Ref argv) {
     PY_CHECK_ARGC(2);
-    bool lhs = py_tobool(&argv[0]);
-    if(argv[1].type == tp_bool) {
-        bool rhs = py_tobool(&argv[1]);
+    py_i64 lhs = (py_i64)py_tobool(&argv[0]);
+    py_i64 rhs;
+    if(bool_try_cast_i64(py_arg(1), &rhs)) {
         py_newbool(py_retval(), lhs != rhs);
     } else {
         py_newnotimplemented(py_retval());
@@ -561,25 +573,6 @@ DEF_BOOL_BITWISE(__and__, &&)
 DEF_BOOL_BITWISE(__or__, ||)
 DEF_BOOL_BITWISE(__xor__, !=)
 
-static bool bool__invert__(int argc, py_Ref argv) {
-    PY_CHECK_ARGC(1);
-    bool val = py_tobool(&argv[0]);
-    py_newbool(py_retval(), !val);
-    return true;
-}
-
-static bool bool_try_cast_i64(py_Ref arg, py_i64* out) {
-    if (arg->type == tp_int) {
-        *out = py_toint(arg);
-        return true;
-    } else if (arg->type == tp_bool) {
-        *out = py_tobool(arg);
-        return true;
-    } else {
-        return false;
-    }
-}
-
 static bool bool__add__(int argc, py_Ref argv) {
     PY_CHECK_ARGC(2);
     py_i64 lhs = py_tobool(py_arg(0));
@@ -711,7 +704,6 @@ void pk_number__register() {
     py_bindmagic(tp_bool, __and__, bool__and__);
     py_bindmagic(tp_bool, __or__, bool__or__);
     py_bindmagic(tp_bool, __xor__, bool__xor__);
-    py_bindmagic(tp_bool, __invert__, bool__invert__);
     py_bindmagic(tp_bool, __add__, bool__add__);
     py_bindmagic(tp_bool, __sub__, bool__sub__);
     py_bindmagic(tp_bool, __mul__, bool__mul__);

+ 2 - 1
src/interpreter/ceval.c

@@ -803,7 +803,8 @@ __NEXT_STEP:
         }
         /*****************************************/
         case OP_CALL: {
-            if(self->heap.gc_enabled) ManagedHeap__collect_hint(&self->heap);
+            if(self->heap.gc_enabled && self->heap.gc_counter >= self->heap.gc_threshold)
+                ManagedHeap__collect_hint(&self->heap);
             vectorcall_opcall(byte.arg & 0xFF, byte.arg >> 8);
             DISPATCH();
         }

+ 17 - 11
src/modules/array2d.c

@@ -387,8 +387,13 @@ static bool array2d_like__invert__(int argc, py_Ref argv) {
     for(int j = 0; j < self->n_rows; j++) {
         for(int i = 0; i < self->n_cols; i++) {
             py_Ref item = self->f_get(self, i, j);
-            if(!pk_callmagic(__invert__, 1, item)) return false;
-            c11_array2d__set(res, i, j, py_retval());
+            if(item->type == tp_bool) {
+                py_Ref p_out = c11_array2d__get(res, i, j);
+                py_newbool(p_out, !py_tobool(item));
+            } else {
+                if(!pk_callmagic(__invert__, 1, item)) return false;
+                c11_array2d__set(res, i, j, py_retval());
+            }
         }
     }
     py_assign(py_retval(), py_peek(-1));
@@ -1007,7 +1012,8 @@ static void register_array2d_view(py_Ref mod) {
 #include "pocketpy/xmacros/smallmap.h"
 #undef SMALLMAP_T__SOURCE
 
-static py_TValue* c11_chunked_array2d__new_chunk(c11_chunked_array2d* self, c11_vec2i pos, py_Ref context) {
+static py_TValue*
+    c11_chunked_array2d__new_chunk(c11_chunked_array2d* self, c11_vec2i pos, py_Ref context) {
     bool exists = c11_chunked_array2d_chunks__contains(&self->chunks, pos);
     if(exists) {
         ValueError("chunk already exists at pos (%d, %d)", pos.x, pos.y);
@@ -1044,15 +1050,15 @@ static void c11_chunked_array2d__world_to_chunk(c11_chunked_array2d* self,
                                                 c11_vec2i* restrict chunk_pos,
                                                 c11_vec2i* restrict local_pos) {
     cpy312__divmod_int_uint(col,
-                           self->chunk_size_log2,
-                           self->chunk_size_mask,
-                           &chunk_pos->x,
-                           &local_pos->x);
+                            self->chunk_size_log2,
+                            self->chunk_size_mask,
+                            &chunk_pos->x,
+                            &local_pos->x);
     cpy312__divmod_int_uint(row,
-                           self->chunk_size_log2,
-                           self->chunk_size_mask,
-                           &chunk_pos->y,
-                           &local_pos->y);
+                            self->chunk_size_log2,
+                            self->chunk_size_mask,
+                            &chunk_pos->y,
+                            &local_pos->y);
 }
 
 static py_TValue* c11_chunked_array2d__parse_col_row(c11_chunked_array2d* self,

+ 8 - 0
tests/030_bool.py

@@ -3,6 +3,14 @@ assert True == True
 assert True != False
 assert False == False
 assert False != True
+assert True == 1
+assert False == 0
+assert True != 0
+assert False != 1
+assert 1 == True
+assert 0 == False
+assert 1 != False
+assert 0 != True
 
 # test and/or/not
 assert True and True