mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 04:24:43 +00:00
Rename dir: compiler
-> crates
This commit is contained in:
parent
e1004b538d
commit
a127564b31
221 changed files with 17 additions and 19 deletions
85
crates/erg_compiler/lib/std/_erg_int.py
Normal file
85
crates/erg_compiler/lib/std/_erg_int.py
Normal file
|
@ -0,0 +1,85 @@
|
|||
from _erg_result import Error
|
||||
|
||||
class Int(int):
|
||||
def try_new(i): # -> Result[Nat]
|
||||
if isinstance(i, int):
|
||||
Int(i)
|
||||
else:
|
||||
Error("not an integer")
|
||||
def succ(self):
|
||||
return Int(self + 1)
|
||||
def pred(self):
|
||||
return Int(self - 1)
|
||||
def mutate(self):
|
||||
return IntMut(self)
|
||||
|
||||
class IntMut(): # inherits Int
|
||||
value: Int
|
||||
|
||||
def __init__(self, i):
|
||||
self.value = Int(i)
|
||||
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, Int):
|
||||
return IntMut(self.value + other)
|
||||
else:
|
||||
return IntMut(self.value + other.value)
|
||||
def __sub__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return IntMut(self.value - other)
|
||||
else:
|
||||
return IntMut(self.value - other.value)
|
||||
def __mul__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return IntMut(self.value * other)
|
||||
else:
|
||||
return IntMut(self.value * other.value)
|
||||
def __floordiv__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return IntMut(self.value // other)
|
||||
else:
|
||||
return IntMut(self.value // other.value)
|
||||
def __pow__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return IntMut(self.value ** other)
|
||||
else:
|
||||
return IntMut(self.value ** other.value)
|
||||
def inc(self, i=1):
|
||||
self.value = Int(self.value + i)
|
||||
def dec(self, i=1):
|
||||
self.value = Int(self.value - i)
|
||||
def succ(self):
|
||||
return self.value.succ()
|
||||
def pred(self):
|
||||
return self.value.pred()
|
Loading…
Add table
Add a link
Reference in a new issue