blueloveTH пре 1 година
родитељ
комит
6bcff00990
2 измењених фајлова са 28 додато и 3 уклоњено
  1. 25 0
      src/public/py_list.c
  2. 3 3
      src/public/py_str.c

+ 25 - 0
src/public/py_list.c

@@ -127,6 +127,30 @@ static bool _py_list__new__(int argc, py_Ref argv) {
     return TypeError("list() takes at most 1 argument");
     return TypeError("list() takes at most 1 argument");
 }
 }
 
 
+static bool _py_list__getitem__(int argc, py_Ref argv) {
+    PY_CHECK_ARGC(2);
+    List* self = py_touserdata(py_arg(0));
+    py_Ref _1 = py_arg(1);
+    if(_1->type == tp_int) {
+        int index = py_toint(py_arg(1));
+        if(!pk__normalize_index(&index, self->count)) return false;
+        *py_retval() = c11__getitem(py_TValue, self, index);
+        return true;
+    } else if(_1->type == tp_slice) {
+        int start, stop, step;
+        bool ok = pk__parse_int_slice(_1, self->count, &start, &stop, &step);
+        if(!ok) return false;
+        py_newlist(py_retval());
+        List* list = py_touserdata(py_retval());
+        PK_SLICE_LOOP(i, start, stop, step) {
+            c11_vector__push(py_TValue, list, c11__getitem(py_TValue, self, i));
+        }
+        return true;
+    } else {
+        return TypeError("list indices must be integers");
+    }
+}
+
 py_Type pk_list__register() {
 py_Type pk_list__register() {
     pk_VM* vm = pk_current_vm;
     pk_VM* vm = pk_current_vm;
     py_Type type = pk_VM__new_type(vm, "list", tp_object, NULL, false);
     py_Type type = pk_VM__new_type(vm, "list", tp_object, NULL, false);
@@ -137,5 +161,6 @@ py_Type pk_list__register() {
     py_bindmagic(type, __eq__, _py_list__eq__);
     py_bindmagic(type, __eq__, _py_list__eq__);
     py_bindmagic(type, __ne__, _py_list__ne__);
     py_bindmagic(type, __ne__, _py_list__ne__);
     py_bindmagic(type, __new__, _py_list__new__);
     py_bindmagic(type, __new__, _py_list__new__);
+    py_bindmagic(type, __getitem__, _py_list__getitem__);
     return type;
     return type;
 }
 }

+ 3 - 3
src/public/py_str.c

@@ -167,9 +167,10 @@ static bool _py_str__getitem__(int argc, py_Ref argv) {
     py_Ref _1 = py_arg(1);
     py_Ref _1 = py_arg(1);
     if(_1->type == tp_int) {
     if(_1->type == tp_int) {
         int index = py_toint(py_arg(1));
         int index = py_toint(py_arg(1));
-        pk__normalize_index(&index, self.size);
+        if(!pk__normalize_index(&index, self.size)) return false;
         c11_sv res = c11_sv__u8_getitem(self, index);
         c11_sv res = c11_sv__u8_getitem(self, index);
         py_newstrn(py_retval(), res.data, res.size);
         py_newstrn(py_retval(), res.data, res.size);
+        return true;
     } else if(_1->type == tp_slice) {
     } else if(_1->type == tp_slice) {
         int start, stop, step;
         int start, stop, step;
         bool ok = pk__parse_int_slice(_1, c11_sv__u8_length(self), &start, &stop, &step);
         bool ok = pk__parse_int_slice(_1, c11_sv__u8_length(self), &start, &stop, &step);
@@ -179,9 +180,8 @@ static bool _py_str__getitem__(int argc, py_Ref argv) {
         c11_string__delete(res);
         c11_string__delete(res);
         return true;
         return true;
     } else {
     } else {
-        return TypeError("str indices must be integers");
+        return TypeError("string indices must be integers");
     }
     }
-    return true;
 }
 }
 
 
 #define DEF_STR_CMP_OP(op, __f, __cond)                                                            \
 #define DEF_STR_CMP_OP(op, __f, __cond)                                                            \