Last active
February 5, 2019 15:50
-
-
Save disconnect3d/a9b184205fb642710231b6c47fdc2aaf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
def calc(v): | |
return getattr(v, 'value', v) | |
def gen_c_type(name, ctype, floordiv=True): | |
class Type(ctype): | |
def __add__(self, other): | |
return Type(ctype(self.value + calc(other)).value) | |
def __sub__(self, other): | |
return Type(ctype(self.value - calc(other)).value) | |
def __mul__(self, other): | |
return Type(ctype(self.value * calc(other)).value) | |
def __pow__(self, other): | |
return Type(ctype(self.value ** calc(other)).value) | |
def __rsub__(self, other): | |
return Type(calc(other) - self.value) | |
def __rpow__(self, other): | |
return Type(calc(other) ** self.value) | |
def __iadd__(self, other): | |
self.value += calc(other) | |
def __imul__(self, other): | |
self.value *= calc(other) | |
def __isub__(self, other): | |
self.value -= calc(other) | |
def __ipow__(self, other): | |
self.value **= calc(other) | |
if floordiv: | |
def __truediv__(self, other): | |
return Type(ctype(self.value // calc(other)).value) | |
def __rtruediv__(self, other): | |
return Type(calc(other) // self.value) | |
def __idiv__(self, other): | |
self.value //= calc(other) | |
else: | |
def __truediv__(self, other): | |
return Type(ctype(self.value / calc(other)).value) | |
def __rtruediv__(self, other): | |
return Type(calc(other) / self.value) | |
def __idiv__(self, other): | |
self.value /= calc(other) | |
def __eq__(self, other): | |
return self.value == calc(other) | |
def __gt__(self, other): | |
return self.value > calc(other) | |
def __ge__(self, other): | |
return self.value >= calc(other) | |
def __le__(self, other): | |
return self.value <= calc(other) | |
def __lt__(self, other): | |
return self.value < calc(other) | |
def __lshift__(self, other): | |
return Type(self.value << calc(other)) | |
def __rshift__(self, other): | |
return Type(self.value >> calc(other)) | |
def __rlshift__(self, other): | |
return Type(calc(other) << self.value) | |
def __rrshift__(self, other): | |
return Type(calc(other) >> self.value) | |
def __str__(self): | |
return str(self.value) | |
def __repr__(self): | |
return f'{self.__class__.__name__}({self.value})' | |
__radd__ = __add__ | |
__rmul__ = __mul__ | |
def __int__(self): | |
return int(self.value) | |
def __float__(self): | |
return float(self.value) | |
__index__ = __int__ # make indexing work | |
Type.__name__ = name | |
return Type | |
I8 = gen_c_type('I32', ctypes.c_int8) | |
I16 = gen_c_type('I16', ctypes.c_int16) | |
I32 = gen_c_type('I32', ctypes.c_int32) | |
I64 = gen_c_type('I64', ctypes.c_int64) | |
U8 = gen_c_type('U8', ctypes.c_uint8) | |
U16 = gen_c_type('U16', ctypes.c_uint16) | |
U32 = gen_c_type('U32', ctypes.c_uint32) | |
U64 = gen_c_type('U64', ctypes.c_uint64) | |
Float = gen_c_type('Float', ctypes.c_float, floordiv=False) | |
Double = gen_c_type('Double', ctypes.c_double, floordiv=False) | |
x = I32(2**31-1) # INT32_MAX | |
print(x) | |
print(x+1) | |
print(x+x+x) | |
print(3 + x) | |
print(3 * x) | |
print(1 - x) | |
print(1**x) | |
print(1-10) | |
print(1-I32(10)) | |
print(repr(I32(123))) | |
print(0 + x + 0) | |
print(x * 0) | |
# print(x / 0) # ZeroDivisionError | |
print(I32(4)/3) | |
print(3*Float(34.00000003)) | |
print(3*Double(34.00000003)) | |
print(Float(34)/3) | |
print(Double(34)/3) | |
print(4 << I32(4)) | |
print(int(I32(4))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment