From 5707ede2aedce34ee359f444c08d3c565db4fc2e Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Mon, 22 Jul 2024 00:50:53 -0700 Subject: [PATCH] support IntCmp with primitive integer types --- crates/fayalite/src/expr/ops.rs | 37 +++++++---------------------- crates/fayalite/src/int.rs | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/crates/fayalite/src/expr/ops.rs b/crates/fayalite/src/expr/ops.rs index 5900e30..e932ff5 100644 --- a/crates/fayalite/src/expr/ops.rs +++ b/crates/fayalite/src/expr/ops.rs @@ -784,38 +784,17 @@ macro_rules! cmp_op { CanonicalType = DynIntType<::Signed>, CanonicalValue = DynInt<::Signed>, >, - Rhs: Value, - Lhs: Value, - > IntCmp> for Expr - { - type Output = Expr>; - - $(fn $fn(self, rhs: Expr) -> Self::Output { - $CmpOp::>::new_unchecked( - self.canonical(), - rhs.canonical(), - ).to_expr() - })* - } - - impl< - LhsType: IntTypeTrait< - CanonicalType = DynIntType<::Signed>, - CanonicalValue = DynInt<::Signed>, - >, - RhsType: IntTypeTrait< - Signed = LhsType::Signed, - CanonicalType = DynIntType<::Signed>, - CanonicalValue = DynInt<::Signed>, - >, - Rhs: Value, + Rhs: ToExpr, Lhs: Value, > IntCmp for Expr { type Output = Expr>; $(fn $fn(self, rhs: Rhs) -> Self::Output { - self.$fn(rhs.to_expr()) + $CmpOp::>::new_unchecked( + self.canonical(), + rhs.to_expr().canonical(), + ).to_expr() })* } @@ -829,11 +808,11 @@ macro_rules! cmp_op { CanonicalType = DynIntType<::Signed>, CanonicalValue = DynInt<::Signed>, >, - Rhs: Value, - > IntCmp> for IntValue { + Rhs: ToExpr, + > IntCmp for IntValue { type Output = Expr>; - $(fn $fn(self, rhs: Expr) -> Self::Output { + $(fn $fn(self, rhs: Rhs) -> Self::Output { self.to_expr().$fn(rhs) })* } diff --git a/crates/fayalite/src/int.rs b/crates/fayalite/src/int.rs index 4f41e53..b1b796e 100644 --- a/crates/fayalite/src/int.rs +++ b/crates/fayalite/src/int.rs @@ -860,6 +860,47 @@ pub trait IntCmp { fn cmp_ge(self, rhs: Rhs) -> Self::Output; } +macro_rules! forward_prim_int_cmp { + ($prim_ty:ident) => { + impl IntCmp for $prim_ty + where + IntValue<<$prim_ty as ToExpr>::Type>: IntCmp, + { + type Output = ::Type> as IntCmp>::Output; + fn cmp_eq(self, rhs: Rhs) -> Self::Output { + IntValue::<<$prim_ty as ToExpr>::Type>::from(self).cmp_eq(rhs) + } + fn cmp_ne(self, rhs: Rhs) -> Self::Output { + IntValue::<<$prim_ty as ToExpr>::Type>::from(self).cmp_ne(rhs) + } + fn cmp_lt(self, rhs: Rhs) -> Self::Output { + IntValue::<<$prim_ty as ToExpr>::Type>::from(self).cmp_lt(rhs) + } + fn cmp_le(self, rhs: Rhs) -> Self::Output { + IntValue::<<$prim_ty as ToExpr>::Type>::from(self).cmp_le(rhs) + } + fn cmp_gt(self, rhs: Rhs) -> Self::Output { + IntValue::<<$prim_ty as ToExpr>::Type>::from(self).cmp_gt(rhs) + } + fn cmp_ge(self, rhs: Rhs) -> Self::Output { + IntValue::<<$prim_ty as ToExpr>::Type>::from(self).cmp_ge(rhs) + } + } + }; +} + +forward_prim_int_cmp!(bool); +forward_prim_int_cmp!(u8); +forward_prim_int_cmp!(u16); +forward_prim_int_cmp!(u32); +forward_prim_int_cmp!(u64); +forward_prim_int_cmp!(u128); +forward_prim_int_cmp!(i8); +forward_prim_int_cmp!(i16); +forward_prim_int_cmp!(i32); +forward_prim_int_cmp!(i64); +forward_prim_int_cmp!(i128); + mod sealed { pub trait Sealed {} }