mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 10:49:54 +00:00
feat: add RMul, RDiv
* `And` has the default type index * impl `Dimension` traits
This commit is contained in:
parent
4651a383ae
commit
ff53af0cb6
17 changed files with 323 additions and 118 deletions
|
@ -4119,6 +4119,39 @@ impl Context {
|
|||
Immutable,
|
||||
Visibility::BUILTIN_PUBLIC,
|
||||
);
|
||||
let mut dimension_to_float = Self::builtin_methods(Some(mono(TO_FLOAT)), 1);
|
||||
dimension_to_float.register_builtin_erg_impl(
|
||||
FUNDAMENTAL_FLOAT,
|
||||
fn0_met(dimension_t.clone(), Float).quantify(),
|
||||
Immutable,
|
||||
Visibility::BUILTIN_PUBLIC,
|
||||
);
|
||||
dimension.register_trait_methods(dimension_t.clone(), dimension_to_float);
|
||||
let mut dimension_to_int = Self::builtin_methods(Some(mono(TO_INT)), 1);
|
||||
dimension_to_int.register_builtin_erg_impl(
|
||||
FUNDAMENTAL_INT,
|
||||
fn0_met(dimension_t.clone(), Int).quantify(),
|
||||
Immutable,
|
||||
Visibility::BUILTIN_PUBLIC,
|
||||
);
|
||||
dimension.register_trait_methods(dimension_t.clone(), dimension_to_int);
|
||||
let mut dimension_eq = Self::builtin_methods(Some(mono(EQ)), 2);
|
||||
let t = fn1_met(dimension_t.clone(), dimension_t.clone(), Bool).quantify();
|
||||
dimension_eq.register_builtin_erg_impl(OP_EQ, t, Immutable, Visibility::BUILTIN_PUBLIC);
|
||||
dimension.register_trait_methods(dimension_t.clone(), dimension_eq);
|
||||
let mut dimension_partial_ord = Self::builtin_methods(Some(mono(PARTIAL_ORD)), 2);
|
||||
dimension_partial_ord.register_builtin_erg_impl(
|
||||
OP_PARTIAL_CMP,
|
||||
fn1_met(
|
||||
dimension_t.clone(),
|
||||
dimension_t.clone(),
|
||||
mono(ORDERING) | NoneType,
|
||||
)
|
||||
.quantify(),
|
||||
Const,
|
||||
Visibility::BUILTIN_PUBLIC,
|
||||
);
|
||||
dimension.register_trait_methods(dimension_t.clone(), dimension_partial_ord);
|
||||
let mut dimension_add =
|
||||
Self::builtin_methods(Some(poly(ADD, vec![ty_tp(dimension_t.clone())])), 2);
|
||||
let t = fn1_met(
|
||||
|
@ -4181,6 +4214,17 @@ impl Context {
|
|||
ValueObj::builtin_class(dimension_added_t),
|
||||
);
|
||||
dimension.register_trait_methods(dimension_t.clone(), dimension_mul);
|
||||
let mut dimension_rmul =
|
||||
Self::builtin_methods(Some(poly(RMUL, vec![ty_tp(Ty.clone())])), 2);
|
||||
let t = fn1_met(dimension_t.clone(), Ty.clone(), dimension_t.clone()).quantify();
|
||||
dimension_rmul.register_builtin_erg_impl(OP_RMUL, t, Immutable, Visibility::BUILTIN_PUBLIC);
|
||||
dimension_rmul.register_builtin_const(
|
||||
OUTPUT,
|
||||
Visibility::BUILTIN_PUBLIC,
|
||||
None,
|
||||
ValueObj::builtin_class(dimension_t.clone()),
|
||||
);
|
||||
dimension.register_trait_methods(dimension_t.clone(), dimension_rmul);
|
||||
let mut dimension_div =
|
||||
Self::builtin_methods(Some(poly(DIV, vec![ty_tp(dimension2_t.clone())])), 2);
|
||||
let dimension_subtracted_t = poly(
|
||||
|
@ -4210,6 +4254,17 @@ impl Context {
|
|||
ValueObj::builtin_class(dimension_subtracted_t),
|
||||
);
|
||||
dimension.register_trait_methods(dimension_t.clone(), dimension_div);
|
||||
let mut dimension_rdiv =
|
||||
Self::builtin_methods(Some(poly(RDIV, vec![ty_tp(Ty.clone())])), 2);
|
||||
let t = fn1_met(dimension_t.clone(), Ty.clone(), dimension_t.clone()).quantify();
|
||||
dimension_rdiv.register_builtin_erg_impl(OP_RDIV, t, Immutable, Visibility::BUILTIN_PUBLIC);
|
||||
dimension_rdiv.register_builtin_const(
|
||||
OUTPUT,
|
||||
Visibility::BUILTIN_PUBLIC,
|
||||
None,
|
||||
ValueObj::builtin_class(dimension_t.clone()),
|
||||
);
|
||||
dimension.register_trait_methods(dimension_t.clone(), dimension_rdiv);
|
||||
let mut base_exception = Self::builtin_mono_class(BASE_EXCEPTION, 2);
|
||||
base_exception.register_superclass(Obj, &obj);
|
||||
base_exception.register_builtin_erg_impl(
|
||||
|
|
|
@ -1005,7 +1005,11 @@ impl Context {
|
|||
Some(FUNC_SUB),
|
||||
);
|
||||
let L = mono_q(TY_L, subtypeof(poly(MUL, params.clone())));
|
||||
let op_t = bin_op(L.clone(), R.clone(), proj(L, OUTPUT)).quantify();
|
||||
let L2 = type_q(TY_L);
|
||||
let R2 = mono_q(TY_R, subtypeof(poly(RMUL, vec![ty_tp(L2.clone())])));
|
||||
let op_t = (bin_op(L.clone(), R.clone(), proj(L, OUTPUT)).quantify()
|
||||
& bin_op(L2.clone(), R2.clone(), proj(R2, OUTPUT)).quantify())
|
||||
.with_default_intersec_index(0);
|
||||
self.register_builtin_py_impl(
|
||||
OP_MUL,
|
||||
op_t,
|
||||
|
@ -1014,7 +1018,11 @@ impl Context {
|
|||
Some(FUNC_MUL),
|
||||
);
|
||||
let L = mono_q(TY_L, subtypeof(poly(DIV, params.clone())));
|
||||
let op_t = bin_op(L.clone(), R.clone(), proj(L, OUTPUT)).quantify();
|
||||
let L2 = type_q(TY_L);
|
||||
let R2 = mono_q(TY_R, subtypeof(poly(RDIV, vec![ty_tp(L2.clone())])));
|
||||
let op_t = (bin_op(L.clone(), R.clone(), proj(L, OUTPUT)).quantify()
|
||||
& bin_op(L2.clone(), R2.clone(), proj(R2, OUTPUT)).quantify())
|
||||
.with_default_intersec_index(0);
|
||||
self.register_builtin_py_impl(
|
||||
OP_DIV,
|
||||
op_t,
|
||||
|
|
|
@ -133,6 +133,8 @@ const ATTR_TRACEBACK: &str = "traceback";
|
|||
const ADD: &str = "Add";
|
||||
const SUB: &str = "Sub";
|
||||
const MUL: &str = "Mul";
|
||||
const RMUL: &str = "RMul";
|
||||
const RDIV: &str = "RDiv";
|
||||
const DIV: &str = "Div";
|
||||
const FLOOR_DIV: &str = "FloorDiv";
|
||||
const POS: &str = "Pos";
|
||||
|
@ -564,6 +566,8 @@ const OP_SUB: &str = "__sub__";
|
|||
const OP_MUL: &str = "__mul__";
|
||||
const OP_DIV: &str = "__div__";
|
||||
const OP_FLOOR_DIV: &str = "__floordiv__";
|
||||
const OP_RMUL: &str = "__rmul__";
|
||||
const OP_RDIV: &str = "__rdiv__";
|
||||
const OP_ABS: &str = "__abs__";
|
||||
const OP_PARTIAL_CMP: &str = "__partial_cmp__";
|
||||
const OP_AND: &str = "__and__";
|
||||
|
|
|
@ -587,6 +587,23 @@ impl Context {
|
|||
let op_t = fn0_met(_Slf.clone(), proj(_Slf, OUTPUT)).quantify();
|
||||
neg.register_builtin_erg_decl(OP_NEG, op_t, Visibility::BUILTIN_PUBLIC);
|
||||
neg.register_builtin_erg_decl(OUTPUT, Type, Visibility::BUILTIN_PUBLIC);
|
||||
/* RMul */
|
||||
let L = mono_q(TY_L, instanceof(Type));
|
||||
let rparams = vec![PS::t(TY_L, false, WithDefault)];
|
||||
let ty_rparams = vec![ty_tp(L.clone())];
|
||||
let mut rmul = Self::builtin_poly_trait(RMUL, rparams.clone(), 2);
|
||||
rmul.register_superclass(poly(OUTPUT, vec![ty_tp(L.clone())]), &output);
|
||||
let RSlf = mono_q(SELF, subtypeof(poly(RMUL, ty_rparams.clone())));
|
||||
let op_t = fn1_met(RSlf.clone(), L.clone(), proj(RSlf, OUTPUT)).quantify();
|
||||
rmul.register_builtin_erg_decl(OP_RMUL, op_t, Visibility::BUILTIN_PUBLIC);
|
||||
rmul.register_builtin_erg_decl(OUTPUT, Type, Visibility::BUILTIN_PUBLIC);
|
||||
/* RDiv */
|
||||
let mut rdiv = Self::builtin_poly_trait(RDIV, rparams.clone(), 2);
|
||||
rdiv.register_superclass(poly(OUTPUT, vec![ty_tp(L.clone())]), &output);
|
||||
let RSlf = mono_q(SELF, subtypeof(poly(RDIV, ty_rparams.clone())));
|
||||
let op_t = fn1_met(RSlf.clone(), L.clone(), proj(RSlf, OUTPUT)).quantify();
|
||||
rdiv.register_builtin_erg_decl(OP_RDIV, op_t, Visibility::BUILTIN_PUBLIC);
|
||||
rdiv.register_builtin_erg_decl(OUTPUT, Type, Visibility::BUILTIN_PUBLIC);
|
||||
/* Num */
|
||||
let num = Self::builtin_mono_trait(NUM, 2);
|
||||
// num.register_superclass(poly(ADD, vec![]), &add);
|
||||
|
@ -832,7 +849,15 @@ impl Context {
|
|||
None,
|
||||
);
|
||||
self.register_builtin_type(mono(POS), pos, vis.clone(), Const, Some(POS));
|
||||
self.register_builtin_type(mono(NEG), neg, vis, Const, Some(NEG));
|
||||
self.register_builtin_type(mono(NEG), neg, vis.clone(), Const, Some(NEG));
|
||||
self.register_builtin_type(
|
||||
poly(RMUL, ty_rparams.clone()),
|
||||
rmul,
|
||||
vis.clone(),
|
||||
Const,
|
||||
Some(RMUL),
|
||||
);
|
||||
self.register_builtin_type(poly(RDIV, ty_rparams), rdiv, vis.clone(), Const, Some(RDIV));
|
||||
self.register_const_param_defaults(
|
||||
ADD,
|
||||
vec![ConstTemplate::Obj(ValueObj::builtin_type(Slf.clone()))],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue