Browse Source

fix https://github.com/blueloveTH/pocketpy/issues/172

blueloveTH 2 years ago
parent
commit
04df1dbe5b
2 changed files with 32 additions and 27 deletions
  1. 1 26
      python/builtins.py
  2. 31 1
      src/pocketpy.cpp

+ 1 - 26
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: ++i;
-        while key(a[j])>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)
-
-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
+del __f
 
-type.__repr__ = lambda self: "<class '" + self.__name__ + "'>"
-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

+ 31 - 1
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 << "<class '" << info.name << "'>";
+        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])];