Add inc!, dec! to Int

This commit is contained in:
Shunsuke Shibayama 2022-12-05 02:34:35 +09:00
parent 6cb3231845
commit b0fe1103f3
13 changed files with 303 additions and 29 deletions

View 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()

View file

@ -0,0 +1,5 @@
def mutate_operator(x):
if hasattr(x, 'mutate'):
return x.mutate()
else:
return x

View file

@ -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()

View file

@ -69,7 +69,7 @@ class RangeIterator:
self.needle = chr(ord(self.needle) + 1)
else:
if not(self.needle in self.rng):
self.needle = self.needle.incremented()
self.needle = self.needle.succ()
def __iter__(self):
return self
@ -88,7 +88,7 @@ class RangeIterator:
else:
if self.needle in self.rng:
result = self.needle
self.needle = self.needle.incremented()
self.needle = self.needle.succ()
return result
raise StopIteration

View file

@ -1,7 +1,9 @@
from _erg_range import Range, LeftOpenRange, RightOpenRange, OpenRange, ClosedRange, RangeIterator
from _erg_result import Result, Error, is_ok
from _erg_nat import Nat
from _erg_int import Int, IntMut
from _erg_nat import Nat, NatMut
from _erg_bool import Bool
from _erg_str import Str
from _erg_str import Str, StrMut
from _erg_array import Array
from _erg_in_operator import in_operator
from _erg_mutate_operator import mutate_operator

View file

@ -54,7 +54,6 @@ class Bool(Nat):
class Str(str):
def __instancecheck__(cls, obj):
print(cls, obj)
return obj == Str or obj == str
def try_new(s: str): # -> Result[Nat]

View file

@ -13,27 +13,47 @@ class Str(str):
return Str(self[i])
else:
return None
def mutate(self):
return StrMut(self)
class StrMut(Str):
class StrMut(): # Inherits Str
value: Str
def __init__(self, s: str):
self.value = s
def __repr__(self):
return self.value.__repr__()
def __eq__(self, other):
if isinstance(other, Str):
return self.value == other
else:
return self.value == other.value
def __ne__(self, other):
if isinstance(other, Str):
return self.value != other
else:
return self.value != other.value
def try_new(s: str):
if isinstance(s, str):
return StrMut(s)
self = StrMut()
self.value = s
return self
else:
return Error("Str! can't be other than str")
def clear(self):
self = ""
self.value = ""
def pop(self):
if len(self) > 0:
last = self[-1]
self = self[:-1]
if len(self.value) > 0:
last = self.value[-1]
self.value = self.value[:-1]
return last
else:
return Error("Can't pop from empty `Str!`")
def push(self, c: str):
self += c
self.value += c
def remove(self, idx: int):
char = self[idx]
self = self[:idx] + self[idx+1:]
char = self.value[idx]
self.value = self.value[:idx] + self.value[idx+1:]
return char
def insert(self, idx: int, c: str):
self = self[:idx] + c + self[idx:]
self.value = self.value[:idx] + c + self.value[idx:]