mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
some more update
This commit is contained in:
parent
8745f323b8
commit
cefa85afa4
@ -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.
|
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 |
|
| 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 Assignment | `a[b] = c` | `setitem(a, b, c)` |
|
||||||
| Index Deletion | `del a[b]` | `delitem(a, b)` |
|
| 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)` |
|
||||||
|
@ -33,3 +33,17 @@ def matmul(a, b): return a @ b
|
|||||||
def getitem(a, b): return a[b]
|
def getitem(a, b): return a[b]
|
||||||
def setitem(a, b, c): a[b] = c
|
def setitem(a, b, c): a[b] = c
|
||||||
def delitem(a, b): del a[b]
|
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
@ -525,7 +525,11 @@ namespace pkpy{
|
|||||||
if(callback == nullptr) callback = &Compiler::compile_stmt;
|
if(callback == nullptr) callback = &Compiler::compile_stmt;
|
||||||
consume(TK(":"));
|
consume(TK(":"));
|
||||||
if(curr().type!=TK("@eol") && curr().type!=TK("@eof")){
|
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;
|
return;
|
||||||
}
|
}
|
||||||
if(!match_newlines(mode()==REPL_MODE)){
|
if(!match_newlines(mode()==REPL_MODE)){
|
||||||
|
@ -35,3 +35,16 @@ op.setitem(a, 0, 3)
|
|||||||
assert a == [3, 2]
|
assert a == [3, 2]
|
||||||
op.delitem(a, 0)
|
op.delitem(a, 0)
|
||||||
assert a == [2]
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user