diff --git a/src/pocketpy.h b/src/pocketpy.h index 944f2571..654b8e66 100644 --- a/src/pocketpy.h +++ b/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 diff --git a/src/vm.h b/src/vm.h index d0f1a545..99d46d1d 100644 --- a/src/vm.h +++ b/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++){