blueloveTH před 1 rokem
rodič
revize
cefa85afa4
5 změnil soubory, kde provedl 48 přidání a 2 odebrání
  1. 16 1
      docs/modules/operator.md
  2. 14 0
      python/operator.py
  3. 0 0
      src/_generated.cpp
  4. 5 1
      src/compiler.cpp
  5. 13 0
      tests/89_operator.py

+ 16 - 1
docs/modules/operator.md

@@ -5,7 +5,7 @@ label: operator
 
 The operator module exports a set of efficient functions corresponding to the intrinsic operators of Python. For example, `operator.add(x, y)` is equivalent to the expression `x+y`. Many function names are those used for special methods, without the double underscores.
 
-## Supported operators
+## Mapping Operators to Functions
 
 | Operation | Syntax | Function |
 | --- | --- | --- |
@@ -39,3 +39,18 @@ The operator module exports a set of efficient functions corresponding to the in
 | Index Assignment | `a[b] = c` | `setitem(a, b, c)` |
 | Index Deletion | `del a[b]` | `delitem(a, b)` |
 
+
+## In-place Operators
+| Operation | Syntax | Function |
+| --- | --- | --- |
+| Addition | `a += b` | `iadd(a, b)` |
+| Subtraction | `a -= b` | `isub(a, b)` |
+| Multiplication | `a *= b` | `imul(a, b)` |
+| Division | `a /= b` | `itruediv(a, b)` |
+| Division | `a //= b` | `ifloordiv(a, b)` |
+| Modulo | `a %= b` | `imod(a, b)` |
+| Bitwise AND | `a &= b` | `iand(a, b)` |
+| Bitwise OR | `a |= b` | `ior(a, b)` |
+| Bitwise XOR | `a ^= b` | `ixor(a, b)` |
+| Left Shift | `a <<= b` | `ilshift(a, b)` |
+| Right Shift | `a >>= b` | `irshift(a, b)` |

+ 14 - 0
python/operator.py

@@ -33,3 +33,17 @@ def matmul(a, b): return a @ b
 def getitem(a, b): return a[b]
 def setitem(a, b, c): a[b] = c
 def delitem(a, b): del a[b]
+
+def iadd(a, b): a += b; return a
+def isub(a, b): a -= b; return a
+def imul(a, b): a *= b; return a
+def itruediv(a, b): a /= b; return a
+def ifloordiv(a, b): a //= b; return a
+def imod(a, b): a %= b; return a
+# def ipow(a, b): a **= b; return a
+# def imatmul(a, b): a @= b; return a
+def iand(a, b): a &= b; return a
+def ior(a, b): a |= b; return a
+def ixor(a, b): a ^= b; return a
+def ilshift(a, b): a <<= b; return a
+def irshift(a, b): a >>= b; return a

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
src/_generated.cpp


+ 5 - 1
src/compiler.cpp

@@ -525,7 +525,11 @@ namespace pkpy{
         if(callback == nullptr) callback = &Compiler::compile_stmt;
         consume(TK(":"));
         if(curr().type!=TK("@eol") && curr().type!=TK("@eof")){
-            compile_stmt();     // inline block
+            while(true){
+                compile_stmt();
+                bool possible = curr().type!=TK("@eol") && curr().type!=TK("@eof");
+                if(prev().type != TK(";") || !possible) break;
+            }
             return;
         }
         if(!match_newlines(mode()==REPL_MODE)){

+ 13 - 0
tests/89_operator.py

@@ -35,3 +35,16 @@ op.setitem(a, 0, 3)
 assert a == [3, 2]
 op.delitem(a, 0)
 assert a == [2]
+
+a = 'abc'
+assert op.iadd(a, 'def') == 'abcdef'
+assert op.isub(8, 3) == 5
+assert op.imul(a, 2) == 'abcabc'
+assert op.itruediv(8, 2) == 4.0
+assert op.ifloordiv(8, 3) == 2
+assert op.imod(8, 3) == 2
+assert op.iand(0b01, 0b11) == 0b01
+assert op.ior(0b01, 0b11) == 0b11
+assert op.ixor(0b01, 0b11) == 0b10
+assert op.ilshift(0b01, 1) == 0b10
+assert op.irshift(0b10, 1) == 0b01

Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů