style(semantics): move macro impls to specific modules

This commit is contained in:
Anand Balakrishnan 2023-04-04 09:59:10 -07:00
parent b517043d0e
commit 55b7cdd075
No known key found for this signature in database
5 changed files with 127 additions and 122 deletions

View file

@ -1,9 +1,53 @@
use argus_core::expr::NumExpr;
use argus_core::signals::{AnySignal, ConstantSignal};
use crate::utils::signal_num_op_impl;
use crate::Trace;
macro_rules! signal_num_op_impl {
// Unary numeric opeartions
(- $signal:ident) => {{
use argus_core::prelude::*;
use AnySignal::*;
match $signal {
Bool(_) | ConstBool(_) => panic!("cannot perform unary operation (-) on Boolean signals"),
Int(signal) => AnySignal::from(-(&signal)),
ConstInt(signal) => AnySignal::from(-(&signal)),
UInt(_) | ConstUInt(_) => panic!("cannot perform unary operation (-) on unsigned integer signals"),
Float(signal) => AnySignal::from(-(&signal)),
ConstFloat(signal) => AnySignal::from(-(&signal)),
}
}};
($lhs:ident $op:tt $rhs:ident, [$( $type:ident ),*]) => {
paste::paste!{
{
use argus_core::prelude::*;
use AnySignal::*;
match ($lhs, $rhs) {
(Bool(_), _) | (ConstBool(_), _) | (_, Bool(_)) | (_, ConstBool(_)) => panic!("cannot perform numeric operation {} for boolean arguments", stringify!($op)),
$(
([<$type >](lhs), [< $type >](rhs)) => AnySignal::from(&lhs $op &rhs),
([<$type >](lhs), [< Const $type >](rhs)) => AnySignal::from(&lhs $op &rhs),
([<Const $type >](lhs), [< $type >](rhs)) => AnySignal::from(&lhs $op &rhs),
([<Const $type >](lhs), [< Const $type >](rhs)) => AnySignal::from(&lhs $op &rhs),
)*
_ => panic!("mismatched argument types for {} operation", stringify!($op)),
}
}
}
};
// Binary numeric opeartions
($lhs:ident $op:tt $rhs:ident) => {
signal_num_op_impl!(
$lhs $op $rhs,
[Int, UInt, Float]
)
};
}
pub(crate) use signal_num_op_impl;
/// Helper struct to evaluate a [`NumExpr`] given a trace.
pub struct NumExprEval;