mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 02:39:20 +00:00
Fix builtin types methods
This commit is contained in:
parent
c56ef64576
commit
8cdc735486
12 changed files with 180 additions and 7 deletions
|
@ -19,3 +19,9 @@ def with__(obj, body):
|
|||
|
||||
def discard__(obj):
|
||||
pass
|
||||
|
||||
def then__(x, f):
|
||||
if x == None or x == NotImplemented:
|
||||
return x
|
||||
else:
|
||||
return f(x)
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
from _erg_int import Int
|
||||
from _erg_nat import Nat
|
||||
from _erg_float import Float
|
||||
from _erg_str import Str
|
||||
|
||||
def int__(i):
|
||||
try:
|
||||
|
@ -12,3 +14,15 @@ def nat__(i):
|
|||
return Nat(i)
|
||||
except:
|
||||
return None
|
||||
|
||||
def float__(f):
|
||||
try:
|
||||
return Float(f)
|
||||
except:
|
||||
return None
|
||||
|
||||
def str__(s):
|
||||
try:
|
||||
return Str(s)
|
||||
except:
|
||||
return None
|
||||
|
|
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)
|
|
@ -1,4 +1,5 @@
|
|||
from _erg_result import Error
|
||||
from _erg_control import then__
|
||||
|
||||
class Int(int):
|
||||
def try_new(i): # -> Result[Nat]
|
||||
|
@ -12,6 +13,30 @@ class Int(int):
|
|||
return Int(self - 1)
|
||||
def mutate(self):
|
||||
return IntMut(self)
|
||||
def __add__(self, other):
|
||||
return then__(super().__add__(other), Int)
|
||||
def __radd__(self, other):
|
||||
return then__(super().__radd__(other), Int)
|
||||
def __sub__(self, other):
|
||||
return then__(super().__sub__(other), Int)
|
||||
def __rsub__(self, other):
|
||||
return then__(super().__rsub__(other), Int)
|
||||
def __mul__(self, other):
|
||||
return then__(super().__mul__(other), Int)
|
||||
def __rmul__(self, other):
|
||||
return then__(super().__rmul__(other), Int)
|
||||
def __div__(self, other):
|
||||
return then__(super().__div__(other), Int)
|
||||
def __rdiv__(self, other):
|
||||
return then__(super().__rdiv__(other), Int)
|
||||
def __floordiv__(self, other):
|
||||
return then__(super().__floordiv__(other), Int)
|
||||
def __rfloordiv__(self, other):
|
||||
return then__(super().__rfloordiv__(other), Int)
|
||||
def __pow__(self, other):
|
||||
return then__(super().__pow__(other), Int)
|
||||
def __rpow__(self, other):
|
||||
return then__(super().__rpow__(other), Int)
|
||||
|
||||
class IntMut(): # inherits Int
|
||||
value: Int
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from _erg_result import Error
|
||||
from _erg_int import Int
|
||||
from _erg_int import IntMut
|
||||
from _erg_int import Int, IntMut
|
||||
from _erg_control import then__
|
||||
|
||||
class Nat(Int):
|
||||
def try_new(i): # -> Result[Nat]
|
||||
|
@ -20,6 +20,10 @@ class Nat(Int):
|
|||
return 0
|
||||
def mutate(self):
|
||||
return NatMut(self)
|
||||
def __add__(self, other):
|
||||
return then__(super().__add__(other), Nat)
|
||||
def __mul__(self, other):
|
||||
return then__(super().__mul__(other), Nat)
|
||||
|
||||
class NatMut(IntMut): # and Nat
|
||||
value: Nat
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from _erg_range import Range, LeftOpenRange, RightOpenRange, OpenRange, ClosedRange, RangeIterator
|
||||
from _erg_result import Result, Error, is_ok
|
||||
from _erg_float import Float, FloatMut
|
||||
from _erg_int import Int, IntMut
|
||||
from _erg_nat import Nat, NatMut
|
||||
from _erg_bool import Bool
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from _erg_result import Error
|
||||
from _erg_int import Int
|
||||
from _erg_control import then__
|
||||
|
||||
class Str(str):
|
||||
def __instancecheck__(cls, obj):
|
||||
|
@ -18,6 +19,14 @@ class Str(str):
|
|||
return StrMut(self)
|
||||
def to_int(self):
|
||||
return Int(self) if self.isdigit() else None
|
||||
def __add__(self, other):
|
||||
return then__(super().__add__(other), Str)
|
||||
def __radd__(self, other):
|
||||
return then__(super().__radd__(other), Str)
|
||||
def __mul__(self, other):
|
||||
return then__(super().__mul__(other), Str)
|
||||
def __mod__(self, other):
|
||||
return then__(super().__mod__(other), Str)
|
||||
|
||||
class StrMut(): # Inherits Str
|
||||
value: Str
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue