some more update

This commit is contained in:
blueloveTH 2024-05-10 21:03:39 +08:00
parent 8745f323b8
commit cefa85afa4
5 changed files with 49 additions and 3 deletions

View File

@ -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)` |

View File

@ -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

File diff suppressed because one or more lines are too long

View File

@ -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)){

View File

@ -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