Ver Fonte

add `++i` and `--j` syntax

BLUELOVETH há 2 anos atrás
pai
commit
1ff6f92b94
3 ficheiros alterados com 24 adições e 10 exclusões
  1. 7 8
      python/builtins.py
  2. 2 2
      src/vm.h
  3. 15 0
      tests/29_incdec.py

+ 7 - 8
python/builtins.py

@@ -38,7 +38,7 @@ def enumerate(iterable, start=0):
     n = start
     for elem in iterable:
         yield n, elem
-        n += 1
+        ++n
 
 def sum(iterable):
     res = 0
@@ -87,7 +87,7 @@ def str@split(self, sep):
             self = self[i+len(sep):]
             i = 0
         else:
-            i += 1
+            ++i
     res.append(self)
     return res
 
@@ -104,10 +104,10 @@ def str@strip(self, chars=None):
     chars = chars or ' \t\n\r'
     i = 0
     while i < len(self) and self[i] in chars:
-        i += 1
+        ++i
     j = len(self) - 1
     while j >= 0 and self[j] in chars:
-        j -= 1
+        --j
     return self[i:j+1]
 
 ##### list #####
@@ -122,12 +122,11 @@ def __qsort(a: list, L: int, R: int, key):
     mid = key(mid)
     i, j = L, R
     while i<=j:
-        while key(a[i])<mid: i+=1
-        while key(a[j])>mid: j-=1
+        while key(a[i])<mid: ++i;
+        while key(a[j])>mid: --j;
         if i<=j:
             a[i], a[j] = a[j], a[i]
-            i+=1
-            j-=1
+            ++i; --j;
     __qsort(a, L, j, key)
     __qsort(a, i, R, key)
 

+ 2 - 2
src/vm.h

@@ -934,10 +934,10 @@ inline std::string _opcode_argstr(VM* vm, Bytecode byte, const CodeObject* co){
         case OP_LOAD_NAME: case OP_LOAD_GLOBAL: case OP_LOAD_NONLOCAL: case OP_STORE_GLOBAL:
         case OP_LOAD_ATTR: case OP_LOAD_METHOD: case OP_STORE_ATTR: case OP_DELETE_ATTR:
         case OP_IMPORT_NAME: case OP_BEGIN_CLASS:
-        case OP_DELETE_GLOBAL:
+        case OP_DELETE_GLOBAL: case OP_INC_GLOBAL: case OP_DEC_GLOBAL:
             argStr += fmt(" (", StrName(byte.arg).sv(), ")");
             break;
-        case OP_LOAD_FAST: case OP_STORE_FAST: case OP_DELETE_FAST:
+        case OP_LOAD_FAST: case OP_STORE_FAST: case OP_DELETE_FAST: case OP_INC_FAST: case OP_DEC_FAST:
             argStr += fmt(" (", co->varnames[byte.arg].sv(), ")");
             break;
         case OP_LOAD_FUNCTION:

+ 15 - 0
tests/29_incdec.py

@@ -0,0 +1,15 @@
+a = 1
+
+++a
+assert a == 2
+++a; ++a; --a;
+assert a == 3
+
+def f(a):
+    ++a
+    ++a
+    --a
+    return a
+
+assert f(3) == 4
+assert f(-2) == -1