mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 02:39:20 +00:00
fix: mutable object class bug
This commit is contained in:
parent
fce88717b0
commit
3eec9ed590
7 changed files with 135 additions and 117 deletions
|
@ -1,7 +1,7 @@
|
|||
from _erg_nat import Nat
|
||||
from _erg_nat import NatMut
|
||||
from _erg_result import Error
|
||||
|
||||
from _erg_type import MutType
|
||||
|
||||
class Bool(Nat):
|
||||
def try_new(b: bool): # -> Result[Nat]
|
||||
|
@ -42,16 +42,16 @@ class BoolMut(NatMut):
|
|||
return self.value.__hash__()
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, bool):
|
||||
return self.value == other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value == other.value
|
||||
else:
|
||||
return self.value == other
|
||||
|
||||
def __ne__(self, other):
|
||||
if isinstance(other, bool):
|
||||
return self.value != other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value != other.value
|
||||
else:
|
||||
return self.value != other
|
||||
|
||||
def update(self, f):
|
||||
self.value = Bool(f(self.value))
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from _erg_result import Error
|
||||
from _erg_control import then__
|
||||
|
||||
from _erg_type import MutType
|
||||
|
||||
class Float(float):
|
||||
EPSILON = 2.220446049250313e-16
|
||||
|
@ -32,6 +32,9 @@ class Float(float):
|
|||
def __floordiv__(self, other):
|
||||
return then__(float.__floordiv__(self, other), Float)
|
||||
|
||||
def __truediv__(self, other):
|
||||
return then__(float.__truediv__(self, other), Float)
|
||||
|
||||
def __pow__(self, other):
|
||||
return then__(float.__pow__(self, other), Float)
|
||||
|
||||
|
@ -47,7 +50,7 @@ class Float(float):
|
|||
def nearly_eq(self, other, epsilon=EPSILON):
|
||||
return abs(self - other) < epsilon
|
||||
|
||||
class FloatMut: # inherits Float
|
||||
class FloatMut(MutType): # inherits Float
|
||||
value: Float
|
||||
|
||||
EPSILON = 2.220446049250313e-16
|
||||
|
@ -64,71 +67,80 @@ class FloatMut: # inherits Float
|
|||
def __deref__(self):
|
||||
return self.value
|
||||
|
||||
def __float__(self):
|
||||
return self.value.__float__()
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return self.value == other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value == other.value
|
||||
else:
|
||||
return self.value == other
|
||||
|
||||
def __ne__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return self.value != other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value != other.value
|
||||
else:
|
||||
return self.value != other
|
||||
|
||||
def __le__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return self.value <= other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value <= other.value
|
||||
else:
|
||||
return self.value <= other
|
||||
|
||||
def __ge__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return self.value >= other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value >= other.value
|
||||
else:
|
||||
return self.value >= other
|
||||
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return self.value < other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value < other.value
|
||||
else:
|
||||
return self.value < other
|
||||
|
||||
def __gt__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return self.value > other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value > other.value
|
||||
else:
|
||||
return self.value > other
|
||||
|
||||
def __add__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return FloatMut(self.value + other)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return FloatMut(self.value + other.value)
|
||||
else:
|
||||
return FloatMut(self.value + other)
|
||||
|
||||
def __sub__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return FloatMut(self.value - other)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return FloatMut(self.value - other.value)
|
||||
else:
|
||||
return FloatMut(self.value - other)
|
||||
|
||||
def __mul__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return FloatMut(self.value * other)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return FloatMut(self.value * other.value)
|
||||
else:
|
||||
return FloatMut(self.value * other)
|
||||
|
||||
def __floordiv__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return FloatMut(self.value // other)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return FloatMut(self.value // other.value)
|
||||
else:
|
||||
return FloatMut(self.value // other)
|
||||
|
||||
def __truediv__(self, other):
|
||||
if isinstance(other, MutType):
|
||||
return FloatMut(self.value / other.value)
|
||||
else:
|
||||
return FloatMut(self.value / other)
|
||||
|
||||
def __pow__(self, other):
|
||||
if isinstance(other, Float):
|
||||
return FloatMut(self.value**other)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return FloatMut(self.value**other.value)
|
||||
else:
|
||||
return FloatMut(self.value**other)
|
||||
|
||||
def __pos__(self):
|
||||
return self
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from _erg_result import Error
|
||||
from _erg_control import then__
|
||||
|
||||
from _erg_type import MutType
|
||||
|
||||
class Int(int):
|
||||
def try_new(i): # -> Result[Nat]
|
||||
|
@ -52,7 +52,7 @@ class Int(int):
|
|||
return then__(int.__neg__(self), Int)
|
||||
|
||||
|
||||
class IntMut: # inherits Int
|
||||
class IntMut(MutType): # inherits Int
|
||||
value: Int
|
||||
|
||||
def __init__(self, i):
|
||||
|
@ -67,70 +67,70 @@ class IntMut: # inherits Int
|
|||
return self.value.__hash__()
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return self.value == other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value == other.value
|
||||
else:
|
||||
return self.value == other
|
||||
|
||||
def __ne__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return self.value != other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value != other.value
|
||||
else:
|
||||
return self.value != other
|
||||
|
||||
def __le__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return self.value <= other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value <= other.value
|
||||
else:
|
||||
return self.value <= other
|
||||
|
||||
def __ge__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return self.value >= other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value >= other.value
|
||||
else:
|
||||
return self.value >= other
|
||||
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return self.value < other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value < other.value
|
||||
else:
|
||||
return self.value < other
|
||||
|
||||
def __gt__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return self.value > other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value > other.value
|
||||
else:
|
||||
return self.value > other
|
||||
|
||||
def __add__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return IntMut(self.value + other)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return IntMut(self.value + other.value)
|
||||
else:
|
||||
return IntMut(self.value + other)
|
||||
|
||||
def __sub__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return IntMut(self.value - other)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return IntMut(self.value - other.value)
|
||||
else:
|
||||
return IntMut(self.value - other)
|
||||
|
||||
def __mul__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return IntMut(self.value * other)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return IntMut(self.value * other.value)
|
||||
else:
|
||||
return IntMut(self.value * other)
|
||||
|
||||
def __floordiv__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return IntMut(self.value // other)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return IntMut(self.value // other.value)
|
||||
else:
|
||||
return IntMut(self.value // other)
|
||||
|
||||
def __pow__(self, other):
|
||||
if isinstance(other, Int):
|
||||
return IntMut(self.value**other)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return IntMut(self.value**other.value)
|
||||
else:
|
||||
return IntMut(self.value**other)
|
||||
|
||||
def __pos__(self):
|
||||
return self
|
||||
|
|
|
@ -2,6 +2,7 @@ from _erg_result import Error
|
|||
from _erg_int import Int
|
||||
from _erg_int import IntMut # don't unify with the above line
|
||||
from _erg_control import then__
|
||||
from _erg_type import MutType
|
||||
|
||||
class Nat(Int):
|
||||
def __init__(self, i):
|
||||
|
@ -55,70 +56,70 @@ class NatMut(IntMut): # and Nat
|
|||
return self.value.__hash__()
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, int):
|
||||
return self.value == other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value == other.value
|
||||
else:
|
||||
return self.value == other
|
||||
|
||||
def __ne__(self, other):
|
||||
if isinstance(other, int):
|
||||
return self.value != other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value != other.value
|
||||
else:
|
||||
return self.value != other
|
||||
|
||||
def __le__(self, other):
|
||||
if isinstance(other, int):
|
||||
return self.value <= other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value <= other.value
|
||||
else:
|
||||
return self.value <= other
|
||||
|
||||
def __ge__(self, other):
|
||||
if isinstance(other, int):
|
||||
return self.value >= other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value >= other.value
|
||||
else:
|
||||
return self.value >= other
|
||||
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, int):
|
||||
return self.value < other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value < other.value
|
||||
else:
|
||||
return self.value < other
|
||||
|
||||
def __gt__(self, other):
|
||||
if isinstance(other, int):
|
||||
return self.value > other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value > other.value
|
||||
else:
|
||||
return self.value > other
|
||||
|
||||
def __add__(self, other):
|
||||
if isinstance(other, Nat):
|
||||
return NatMut(self.value + other)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return NatMut(self.value + other.value)
|
||||
else:
|
||||
return NatMut(self.value + other)
|
||||
|
||||
def __radd__(self, other):
|
||||
if isinstance(other, Nat):
|
||||
return Nat(other + self.value)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return Nat(other.value + self.value)
|
||||
else:
|
||||
return Nat(other + self.value)
|
||||
|
||||
def __mul__(self, other):
|
||||
if isinstance(other, Nat):
|
||||
return NatMut(self.value * other)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return NatMut(self.value * other.value)
|
||||
else:
|
||||
return NatMut(self.value * other)
|
||||
|
||||
def __rmul__(self, other):
|
||||
if isinstance(other, Nat):
|
||||
return Nat(other * self.value)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return Nat(other.value * self.value)
|
||||
else:
|
||||
return Nat(other * self.value)
|
||||
|
||||
def __pow__(self, other):
|
||||
if isinstance(other, Nat):
|
||||
return NatMut(self.value**other)
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return NatMut(self.value**other.value)
|
||||
else:
|
||||
return NatMut(self.value**other)
|
||||
|
||||
def __pos__(self):
|
||||
return self
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from _erg_result import Error
|
||||
from _erg_int import Int
|
||||
from _erg_control import then__
|
||||
from _erg_type import MutType
|
||||
|
||||
class Str(str):
|
||||
def __instancecheck__(cls, obj):
|
||||
|
@ -46,7 +47,7 @@ class Str(str):
|
|||
return str.__getitem__(self, index_or_slice)
|
||||
|
||||
|
||||
class StrMut: # Inherits Str
|
||||
class StrMut(MutType): # Inherits Str
|
||||
value: Str
|
||||
|
||||
def __init__(self, s: str):
|
||||
|
@ -62,16 +63,16 @@ class StrMut: # Inherits Str
|
|||
return self.value.__hash__()
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Str):
|
||||
return self.value == other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value == other.value
|
||||
else:
|
||||
return self.value == other
|
||||
|
||||
def __ne__(self, other):
|
||||
if isinstance(other, Str):
|
||||
return self.value != other
|
||||
else:
|
||||
if isinstance(other, MutType):
|
||||
return self.value != other.value
|
||||
else:
|
||||
return self.value != other
|
||||
|
||||
def update(self, f):
|
||||
self.value = Str(f(self.value))
|
||||
|
|
|
@ -50,3 +50,6 @@ def _isinstance(obj, classinfo) -> bool:
|
|||
return isinstance(obj, classinfo)
|
||||
except:
|
||||
return False
|
||||
|
||||
class MutType:
|
||||
value: object
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue