blueloveTH 2 лет назад
Родитель
Сommit
1e3849319e
2 измененных файлов с 16 добавлено и 0 удалено
  1. 15 0
      src/pocketpy.h
  2. 1 0
      src/vm.h

+ 15 - 0
src/pocketpy.h

@@ -637,6 +637,21 @@ inline void init_builtins(VM* _vm) {
         const Bytes& other = CAST(Bytes&, args[1]);
         return VAR(self != other);
     });
+
+    /************ slice ************/
+    _vm->bind_static_method<3>("slice", "__new__", [](VM* vm, ArgsView args) {
+        return VAR(Slice(args[0], args[1], args[2]));
+    });
+
+    _vm->bind_method<0>("slice", "__repr__", [](VM* vm, ArgsView args) {
+        const Slice& self = CAST(Slice&, args[0]);
+        std::stringstream ss;
+        ss << "slice(";
+        ss << CAST(Str, vm->asRepr(self.start)) << ", ";
+        ss << CAST(Str, vm->asRepr(self.stop)) << ", ";
+        ss << CAST(Str, vm->asRepr(self.step)) << ")";
+        return VAR(ss.str());
+    });
 }
 
 #ifdef _WIN32

+ 1 - 0
src/vm.h

@@ -806,6 +806,7 @@ inline void VM::init_builtin_types(){
     builtins->attr().set("range", _t(tp_range));
     builtins->attr().set("bytes", _t(tp_bytes));
     builtins->attr().set("StopIteration", StopIteration);
+    builtins->attr().set("slice", _t(tp_slice));
 
     post_init();
     for(int i=0; i<_all_types.size(); i++){