mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
add bitwise op
This commit is contained in:
parent
ca4039a52f
commit
a7266c9388
@ -207,6 +207,27 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
return vm->PyStr(std::to_string(vm->PyInt_AS_C(args[0])));
|
return vm->PyStr(std::to_string(vm->PyInt_AS_C(args[0])));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
#define __INT_BITWISE_OP(name,op) \
|
||||||
|
_vm->bindMethod("int", #name, [](VM* vm, PyVarList args) { \
|
||||||
|
if(!args[0]->isType(vm->_tp_int) || !args[1]->isType(vm->_tp_int)) \
|
||||||
|
vm->typeError("unsupported operand type(s) for " #op ); \
|
||||||
|
return vm->PyInt(vm->PyInt_AS_C(args[0]) op vm->PyInt_AS_C(args[1])); \
|
||||||
|
});
|
||||||
|
|
||||||
|
__INT_BITWISE_OP(__lshift__, <<)
|
||||||
|
__INT_BITWISE_OP(__rshift__, >>)
|
||||||
|
__INT_BITWISE_OP(__and__, &)
|
||||||
|
__INT_BITWISE_OP(__or__, |)
|
||||||
|
__INT_BITWISE_OP(__xor__, ^)
|
||||||
|
|
||||||
|
#undef __INT_BITWISE_OP
|
||||||
|
|
||||||
|
_vm->bindMethod("int", "__xor__", [](VM* vm, PyVarList args) {
|
||||||
|
if(!args[0]->isType(vm->_tp_int) || !args[1]->isType(vm->_tp_int))
|
||||||
|
vm->typeError("unsupported operand type(s) for " "^" );
|
||||||
|
return vm->PyInt(vm->PyInt_AS_C(args[0]) ^ vm->PyInt_AS_C(args[1]));
|
||||||
|
});
|
||||||
|
|
||||||
/************ PyFloat ************/
|
/************ PyFloat ************/
|
||||||
_vm->bindMethod("float", "__new__", [](VM* vm, PyVarList args) {
|
_vm->bindMethod("float", "__new__", [](VM* vm, PyVarList args) {
|
||||||
if(args.size() == 0) return vm->PyFloat(0.0);
|
if(args.size() == 0) return vm->PyFloat(0.0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user