That was easy...

but it breaks associativity or is it commutivity?

Anyway you can't add THREE or more things in one expression (yet)
because adding OberonInt objects returns a two-tuple.

I could adjust the semantics of ObInt to accept such two-tuples and do
add_with_carry() but that's probably more trouble than it's worth.

Just gotta be careful with math expressions, eh?
This commit is contained in:
Simon Forman 2022-10-04 08:53:32 -07:00
parent 690f157ac8
commit 53bea5f59b
1 changed files with 8 additions and 2 deletions

View File

@ -20,21 +20,27 @@ class OberonInt:
'''
Return carry bit and new value.
'''
assert isinstance(other, OberonInt)
if not isinstance(other, OberonInt):
other = OberonInt(other)
n = self.value.value + other.value.value
carry = not is_i32(n)
if carry:
n &= (2**31-1)
return int(carry), OberonInt(n)
__radd__ = __add__
def negate(self):
# Instead of binary ops, just cheat:
return OberonInt(-self.value.value)
def __sub__(self, other):
assert isinstance(other, OberonInt)
if not isinstance(other, OberonInt):
other = OberonInt(other)
return self + other.negate()
__rsub__ = __sub__
def __repr__(self):
#b = bin(self.value.value & (2**32-1))
return f'OberonInt({self.value.value})'