From a7266c938822ac53d600e7359903fa0b1d647aec Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 11 Nov 2022 01:07:52 +0800 Subject: [PATCH] add bitwise op --- src/pocketpy.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/pocketpy.h b/src/pocketpy.h index 5e244cda..0d1aec5b 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -207,6 +207,27 @@ void __initializeBuiltinFunctions(VM* _vm) { 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 ************/ _vm->bindMethod("float", "__new__", [](VM* vm, PyVarList args) { if(args.size() == 0) return vm->PyFloat(0.0);