mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-28 04:09:05 +00:00
Fix builtin types methods
This commit is contained in:
parent
c56ef64576
commit
8cdc735486
12 changed files with 180 additions and 7 deletions
100
crates/erg_compiler/lib/std/_erg_float.py
Normal file
100
crates/erg_compiler/lib/std/_erg_float.py
Normal file
|
@ -0,0 +1,100 @@
|
|||
from _erg_result import Error
|
||||
from _erg_control import then__
|
||||
|
||||
class Float(float):
|
||||
def try_new(i): # -> Result[Nat]
|
||||
if isinstance(i, float):
|
||||
Float(i)
|
||||
else:
|
||||
Error("not a float")
|
||||
def mutate(self):
|
||||
return FloatMut(self)
|
||||
def __add__(self, other):
|
||||
return then__(super().__add__(other), Float)
|
||||
def __radd__(self, other):
|
||||
return then__(super().__radd__(other), Float)
|
||||
def __sub__(self, other):
|
||||
return then__(super().__sub__(other), Float)
|
||||
def __rsub__(self, other):
|
||||
return then__(super().__rsub__(other), Float)
|
||||
def __mul__(self, other):
|
||||
return then__(super().__mul__(other), Float)
|
||||
def __rmul__(self, other):
|
||||
return then__(super().__rmul__(other), Float)
|
||||
def __div__(self, other):
|
||||
return then__(super().__div__(other), Float)
|
||||
def __rdiv__(self, other):
|
||||
return then__(super().__rdiv__(other), Float)
|
||||
def __floordiv__(self, other):
|
||||
return then__(super().__floordiv__(other), Float)
|
||||
def __rfloordiv__(self, other):
|
||||
return then__(super().__rfloordiv__(other), Float)
|
||||
def __pow__(self, other):
|
||||
return then__(super().__pow__(other), Float)
|
||||
def __rpow__(self, other):
|
||||
return then__(super().__rpow__(other), Float)
|
||||
|
||||
class FloatMut(): # inherits Float
|
||||
value: Float
|
||||
|
||||
def __init__(self, i):
|
||||
self.value = Float(i)
|
||||
def __repr__(self):
|
||||
return self.value.__repr__()
|
||||
def __deref__(self):
|
||||
return self.value
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return self.value == other
|
||||
else:
|
||||
return self.value == other.value
|
||||
def __ne__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return self.value != other
|
||||
else:
|
||||
return self.value != other.value
|
||||
def __le__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return self.value <= other
|
||||
else:
|
||||
return self.value <= other.value
|
||||
def __ge__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return self.value >= other
|
||||
else:
|
||||
return self.value >= other.value
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return self.value < other
|
||||
else:
|
||||
return self.value < other.value
|
||||
def __gt__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return self.value > other
|
||||
else:
|
||||
return self.value > other.value
|
||||
def __add__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return FloatMut(self.value + other)
|
||||
else:
|
||||
return FloatMut(self.value + other.value)
|
||||
def __sub__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return FloatMut(self.value - other)
|
||||
else:
|
||||
return FloatMut(self.value - other.value)
|
||||
def __mul__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return FloatMut(self.value * other)
|
||||
else:
|
||||
return FloatMut(self.value * other.value)
|
||||
def __floordiv__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return FloatMut(self.value // other)
|
||||
else:
|
||||
return FloatMut(self.value // other.value)
|
||||
def __pow__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return FloatMut(self.value ** other)
|
||||
else:
|
||||
return FloatMut(self.value ** other.value)
|
Loading…
Add table
Add a link
Reference in a new issue