diff --git a/python/builtins.py b/python/builtins.py index a4eb7cc0..a089f460 100644 --- a/python/builtins.py +++ b/python/builtins.py @@ -263,31 +263,8 @@ def __f(self, width: int, fillchar=' '): return self + fillchar * delta str.ljust = __f -##### list ##### -def __qsort(a: list, L: int, R: int, key): - if L >= R: return; - mid = a[(R+L)//2]; - mid = key(mid) - i, j = L, R - while i<=j: - while key(a[i])mid: --j; - if i<=j: - a[i], a[j] = a[j], a[i] - ++i; --j; - __qsort(a, L, j, key) - __qsort(a, i, R, key) +del __f -def __f(self, reverse=False, key=None): - if key is None: - key = lambda x:x - __qsort(self, 0, len(self)-1, key) - if reverse: - self.reverse() -list.sort = __f - -type.__repr__ = lambda self: "" -type.__getitem__ = lambda self, *args: self # for generics def help(obj): if hasattr(obj, '__func__'): @@ -295,8 +272,6 @@ def help(obj): print(obj.__signature__) print(obj.__doc__) -del __f - class Exception: pass from _long import long \ No newline at end of file diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index d3d916bc..a9f92a21 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -696,6 +696,23 @@ void init_builtins(VM* _vm) { }); /************ list ************/ + _vm->bind(_vm->_t(_vm->tp_list), "sort(self, key=None, reverse=False)", [](VM* vm, ArgsView args) { + List& self = _CAST(List&, args[0]); + PyObject* key = args[1]; + if(key == vm->None){ + std::stable_sort(self.begin(), self.end(), [vm](PyObject* a, PyObject* b){ + return vm->py_lt(a, b); + }); + }else{ + std::stable_sort(self.begin(), self.end(), [vm, key](PyObject* a, PyObject* b){ + return vm->py_lt(vm->call(key, a), vm->call(key, b)); + }); + } + bool reverse = CAST(bool, args[2]); + if(reverse) self.reverse(); + return vm->None; + }); + _vm->bind__repr__(_vm->tp_list, [](VM* vm, PyObject* _0){ List& iterable = _CAST(List&, _0); SStream ss; @@ -874,7 +891,7 @@ void init_builtins(VM* _vm) { BIND_RICH_CMP(le, <=, tp_list, List) BIND_RICH_CMP(gt, >, tp_list, List) BIND_RICH_CMP(ge, >=, tp_list, List) - + BIND_RICH_CMP(lt, <, tp_tuple, Tuple) BIND_RICH_CMP(le, <=, tp_tuple, Tuple) BIND_RICH_CMP(gt, >, tp_tuple, Tuple) @@ -1618,6 +1635,19 @@ void add_module_gc(VM* vm){ void VM::post_init(){ init_builtins(this); + // type + bind__getitem__(tp_type, [](VM* vm, PyObject* self, PyObject* _){ + PK_UNUSED(_); + return self; // for generics + }); + + bind__repr__(tp_type, [](VM* vm, PyObject* self){ + SStream ss; + const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, self)]; + ss << ""; + return VAR(ss.str()); + }); + bind_property(_t(tp_object), "__class__", PK_LAMBDA(vm->_t(args[0]))); bind_property(_t(tp_type), "__base__", [](VM* vm, ArgsView args){ const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0])];