Skip to content

Commit

Permalink
fix: modulo operator bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Feb 4, 2024
1 parent 8189d1c commit 96239b9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
42 changes: 42 additions & 0 deletions crates/erg_compiler/context/initialize/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,27 @@ impl Context {
ValueObj::builtin_class(Int),
);
int.register_trait(Int, int_floordiv);
// needed for implementing `%` operator
let mut int_div = Self::builtin_methods(Some(poly(DIV, vec![ty_tp(Int)])), 2);
int_div.register_builtin_erg_impl(
OP_DIV,
fn1_met(Int, Int, Float),
Const,
Visibility::BUILTIN_PUBLIC,
);
int_div.register_builtin_const(
OUTPUT,
Visibility::BUILTIN_PUBLIC,
None,
ValueObj::builtin_class(Float),
);
int_div.register_builtin_const(
MOD_OUTPUT,
Visibility::BUILTIN_PUBLIC,
None,
ValueObj::builtin_class(Int),
);
int.register_trait(Int, int_div);
let mut int_pos = Self::builtin_methods(Some(mono(POS)), 2);
int_pos.register_builtin_const(
OUTPUT,
Expand Down Expand Up @@ -708,6 +729,27 @@ impl Context {
ValueObj::builtin_class(Nat),
);
nat.register_trait(Nat, nat_floordiv);
// needed for implementing `%` operator
let mut nat_div = Self::builtin_methods(Some(poly(DIV, vec![ty_tp(Nat)])), 2);
nat_div.register_builtin_erg_impl(
OP_DIV,
fn1_met(Nat, Nat, Float),
Const,
Visibility::BUILTIN_PUBLIC,
);
nat_div.register_builtin_const(
OUTPUT,
Visibility::BUILTIN_PUBLIC,
None,
ValueObj::builtin_class(Float),
);
nat_div.register_builtin_const(
MOD_OUTPUT,
Visibility::BUILTIN_PUBLIC,
None,
ValueObj::builtin_class(Nat),
);
nat.register_trait(Nat, nat_div);
let mut nat_mutizable = Self::builtin_methods(Some(mono(MUTIZABLE)), 2);
nat_mutizable.register_builtin_const(
MUTABLE_MUT_TYPE,
Expand Down
2 changes: 2 additions & 0 deletions crates/erg_compiler/ty/free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ impl Constraint {
}
}

/// :> Sub, <: Sup
pub fn get_sub_sup(&self) -> Option<(&Type, &Type)> {
match self {
Self::Sandwiched { sub, sup, .. } => Some((sub, sup)),
Expand Down Expand Up @@ -1075,6 +1076,7 @@ impl<T: CanbeFree + Send + Clone> Free<T> {
self.constraint().and_then(|c| c.get_sub().cloned())
}

/// :> Sub, <: Super
pub fn get_subsup(&self) -> Option<(Type, Type)> {
self.constraint()
.and_then(|c| c.get_sub_sup().map(|(sub, sup)| (sub.clone(), sup.clone())))
Expand Down
7 changes: 7 additions & 0 deletions tests/should_ok/operators.er
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
assert 1 + 1 == 2
assert 1 - 1 == 0
assert 1 * 1 == 1
assert nat(1 / 1) == 1
assert 1 % 1 == 0
assert 1 ** 1 == 1

assert 1 in [1, 2]
assert 1 in (1, 2)
assert 1 in {1, 2}
Expand Down

0 comments on commit 96239b9

Please sign in to comment.