mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 12:24:45 +00:00
Add inc!
, dec!
to Int
This commit is contained in:
parent
6cb3231845
commit
b0fe1103f3
13 changed files with 303 additions and 29 deletions
|
@ -1,6 +1,8 @@
|
|||
from _erg_result import Error
|
||||
from _erg_int import Int
|
||||
from _erg_int import IntMut
|
||||
|
||||
class Nat(int):
|
||||
class Nat(Int):
|
||||
def try_new(i): # -> Result[Nat]
|
||||
if i >= 0:
|
||||
return Nat(i)
|
||||
|
@ -16,3 +18,67 @@ class Nat(int):
|
|||
return self - other
|
||||
else:
|
||||
return 0
|
||||
def mutate(self):
|
||||
return NatMut(self)
|
||||
|
||||
class NatMut(IntMut): # and Nat
|
||||
value: Nat
|
||||
|
||||
def __init__(self, n):
|
||||
self.value = n
|
||||
def __repr__(self):
|
||||
return self.value.__repr__()
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return self.value == other
|
||||
else:
|
||||
return self.value == other.value
|
||||
def __ne__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return self.value != other
|
||||
else:
|
||||
return self.value != other.value
|
||||
def __le__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return self.value <= other
|
||||
else:
|
||||
return self.value <= other.value
|
||||
def __ge__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return self.value >= other
|
||||
else:
|
||||
return self.value >= other.value
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return self.value < other
|
||||
else:
|
||||
return self.value < other.value
|
||||
def __gt__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return self.value > other
|
||||
else:
|
||||
return self.value > other.value
|
||||
def __add__(self, other):
|
||||
if isinstance(other, Nat):
|
||||
return NatMut(self.value + other)
|
||||
else:
|
||||
return NatMut(self.value + other.value)
|
||||
def __mul__(self, other):
|
||||
if isinstance(other, Nat):
|
||||
return NatMut(self.value * other)
|
||||
else:
|
||||
return NatMut(self.value * other.value)
|
||||
def __pow__(self, other):
|
||||
if isinstance(other, Nat):
|
||||
return NatMut(self.value ** other)
|
||||
else:
|
||||
return NatMut(self.value ** other.value)
|
||||
def try_new(i): # -> Result[Nat]
|
||||
if i >= 0:
|
||||
return NatMut(i)
|
||||
else:
|
||||
return Error("Nat can't be negative")
|
||||
|
||||
def times(self, f):
|
||||
for _ in range(self.value):
|
||||
f()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue