diff --git a/crates/erg_compiler/context/initialize/classes.rs b/crates/erg_compiler/context/initialize/classes.rs index 69053817..1a93b5f3 100644 --- a/crates/erg_compiler/context/initialize/classes.rs +++ b/crates/erg_compiler/context/initialize/classes.rs @@ -204,7 +204,12 @@ impl Context { ValueObj::builtin_t(Float), ); float.register_trait(Float, float_floordiv); - let mut float_pos = Self::builtin_methods(Some(mono(POS)), 1); + let mut float_pos = Self::builtin_methods(Some(mono(POS)), 2); + float_pos.register_builtin_const( + OUTPUT, + Visibility::BUILTIN_PUBLIC, + ValueObj::builtin_t(Float), + ); float_pos.register_builtin_erg_impl( OP_POS, fn0_met(Float, Float), @@ -212,7 +217,12 @@ impl Context { Visibility::BUILTIN_PUBLIC, ); float.register_trait(Float, float_pos); - let mut float_neg = Self::builtin_methods(Some(mono(NEG)), 1); + let mut float_neg = Self::builtin_methods(Some(mono(NEG)), 2); + float_neg.register_builtin_const( + OUTPUT, + Visibility::BUILTIN_PUBLIC, + ValueObj::builtin_t(Float), + ); float_neg.register_builtin_erg_impl( OP_NEG, fn0_met(Float, Float), @@ -461,6 +471,11 @@ impl Context { ); int.register_trait(Int, int_floordiv); let mut int_pos = Self::builtin_methods(Some(mono(POS)), 2); + int_pos.register_builtin_const( + OUTPUT, + Visibility::BUILTIN_PUBLIC, + ValueObj::builtin_t(Int), + ); int_pos.register_builtin_erg_impl( OP_POS, fn0_met(Int, Int), @@ -469,6 +484,11 @@ impl Context { ); int.register_trait(Int, int_pos); let mut int_neg = Self::builtin_methods(Some(mono(NEG)), 2); + int_neg.register_builtin_const( + OUTPUT, + Visibility::BUILTIN_PUBLIC, + ValueObj::builtin_t(Int), + ); int_neg.register_builtin_erg_impl( OP_NEG, fn0_met(Int, Int), diff --git a/crates/erg_compiler/context/initialize/funcs.rs b/crates/erg_compiler/context/initialize/funcs.rs index 86ebc8e8..f0d94a0a 100644 --- a/crates/erg_compiler/context/initialize/funcs.rs +++ b/crates/erg_compiler/context/initialize/funcs.rs @@ -564,9 +564,11 @@ impl Context { let M = mono_q(TY_M, subtypeof(mono(MUTIZABLE))); let op_t = func1(M.clone(), proj(M, MUTABLE_MUT_TYPE)).quantify(); self.register_builtin_erg_impl(OP_MUTATE, op_t, Const, Visibility::BUILTIN_PRIVATE); - let N = mono_q(TY_N, subtypeof(mono(NUM))); - let op_t = func1(N.clone(), N).quantify(); - self.register_builtin_erg_decl(OP_POS, op_t.clone(), Visibility::BUILTIN_PRIVATE); + let P = mono_q(TY_N, subtypeof(mono(POS))); + let op_t = func1(P.clone(), proj(P, OUTPUT)).quantify(); + self.register_builtin_erg_decl(OP_POS, op_t, Visibility::BUILTIN_PRIVATE); + let N = mono_q(TY_N, subtypeof(mono(NEG))); + let op_t = func1(N.clone(), proj(N, OUTPUT)).quantify(); self.register_builtin_erg_decl(OP_NEG, op_t, Visibility::BUILTIN_PRIVATE); } diff --git a/crates/erg_compiler/lib/std/_erg_float.py b/crates/erg_compiler/lib/std/_erg_float.py index 622374fa..d11a36c7 100644 --- a/crates/erg_compiler/lib/std/_erg_float.py +++ b/crates/erg_compiler/lib/std/_erg_float.py @@ -34,6 +34,10 @@ class Float(float): return then__(float.__pow__(self, other), Float) def __rpow__(self, other): return then__(float.__pow__(float(other), self), Float) + def __pos__(self): + return self + def __neg__(self): + return then__(float.__neg__(self), Float) class FloatMut(): # inherits Float value: Float @@ -102,3 +106,7 @@ class FloatMut(): # inherits Float return FloatMut(self.value ** other) else: return FloatMut(self.value ** other.value) + def __pos__(self): + return self + def __neg__(self): + return FloatMut(-self.value) diff --git a/crates/erg_compiler/lib/std/_erg_int.py b/crates/erg_compiler/lib/std/_erg_int.py index 9999c11d..39284af5 100644 --- a/crates/erg_compiler/lib/std/_erg_int.py +++ b/crates/erg_compiler/lib/std/_erg_int.py @@ -37,6 +37,10 @@ class Int(int): return then__(int.__pow__(self, other), Int) def __rpow__(self, other): return then__(int.__pow__(other, self), Int) + def __pos__(self): + return self + def __neg__(self): + return then__(int.__neg__(self), Int) class IntMut(): # inherits Int value: Int @@ -102,6 +106,10 @@ class IntMut(): # inherits Int return IntMut(self.value ** other) else: return IntMut(self.value ** other.value) + def __pos__(self): + return self + def __neg__(self): + return IntMut(-self.value) def inc(self, i=1): self.value = Int(self.value + i) def dec(self, i=1): diff --git a/crates/erg_compiler/lib/std/_erg_nat.py b/crates/erg_compiler/lib/std/_erg_nat.py index 90a64f97..2f633dc5 100644 --- a/crates/erg_compiler/lib/std/_erg_nat.py +++ b/crates/erg_compiler/lib/std/_erg_nat.py @@ -25,6 +25,8 @@ class Nat(Int): return then__(super().__add__(other), Nat) def __mul__(self, other): return then__(super().__mul__(other), Nat) + def __pos__(self): + return self class NatMut(IntMut): # and Nat value: Nat @@ -90,6 +92,8 @@ class NatMut(IntMut): # and Nat return NatMut(self.value ** other) else: return NatMut(self.value ** other.value) + def __pos__(self): + return self def try_new(i): # -> Result[Nat] if i >= 0: return NatMut(i)