add Expr type for union of BoolExpr or NumExpr

This commit is contained in:
Anand Balakrishnan 2023-09-29 13:59:39 -07:00
parent 7c81b30a8f
commit 6632e897ba
6 changed files with 32 additions and 22 deletions

View file

@ -3,7 +3,7 @@
use std::ops::{Bound, RangeBounds};
use std::time::Duration;
use super::{BoolExpr, Expr, NumExpr};
use super::{AnyExpr, BoolExpr, NumExpr};
/// Types of comparison operations
#[derive(Clone, Copy, Debug, PartialEq)]
@ -142,7 +142,7 @@ where
// TODO(anand): Can I implement this within argus_derive?
macro_rules! impl_bool_expr {
($ty:ty$(, $($arg:ident),* )? ) => {
impl Expr for $ty {
impl AnyExpr for $ty {
fn is_numeric(&self) -> bool {
false
}
@ -157,7 +157,7 @@ macro_rules! impl_bool_expr {
}
};
($ty:ty, [$args:ident]) => {
impl Expr for $ty {
impl AnyExpr for $ty {
fn is_numeric(&self) -> bool {
false
}

View file

@ -2,19 +2,19 @@
use std::collections::VecDeque;
use super::{Expr, ExprRef};
use super::{AnyExpr, ExprRef};
/// Iterator that starts from some root [`Expr`] and travels down to it's leaf
/// Iterator that starts from some root [`AnyExpr`] and travels down to it's leaf
/// expressions.
///
/// This essentially implements breadth-first search over the expression tree rooted at
/// the given [`Expr`].
/// the given [`AnyExpr`].
pub struct AstIter<'a> {
queue: VecDeque<ExprRef<'a>>,
}
impl<'a> AstIter<'a> {
/// Create an iterator that traverses an [`Expr`] from root to leaf.
/// Create an iterator that traverses an [`AnyExpr`] from root to leaf.
pub fn new(root: ExprRef<'a>) -> Self {
let mut queue = VecDeque::new();
queue.push_back(root);
@ -28,7 +28,7 @@ impl<'a> Iterator for AstIter<'a> {
fn next(&mut self) -> Option<Self::Item> {
let expr_ref = self.queue.pop_front()?;
let expr: &dyn Expr = match expr_ref {
let expr: &dyn AnyExpr = match expr_ref {
ExprRef::Bool(expr) => expr,
ExprRef::Num(expr) => expr,
};

View file

@ -1,11 +1,11 @@
//! Numeric expression types
use super::{Expr, NumExpr};
use super::{AnyExpr, NumExpr};
// TODO(anand): Can I implement this within argus_derive?
macro_rules! impl_num_expr {
($ty:ty$(, $($arg:ident),* )? ) => {
impl Expr for $ty {
impl AnyExpr for $ty {
fn is_numeric(&self) -> bool {
true
}
@ -20,7 +20,7 @@ macro_rules! impl_num_expr {
}
};
($ty:ty, [$args:ident]) => {
impl Expr for $ty {
impl AnyExpr for $ty {
fn is_numeric(&self) -> bool {
false
}

View file

@ -4,7 +4,7 @@ use super::{BoolExpr, ExprRef, NumExpr};
/// A trait representing expressions
#[enum_dispatch]
pub trait Expr {
pub trait AnyExpr {
/// Check if the given expression is a numeric expression
fn is_numeric(&self) -> bool;
/// Check if the given expression is a boolean expression
@ -17,7 +17,7 @@ pub trait Expr {
}
/// Marker trait for numeric expressions
pub trait IsNumExpr: Expr + Into<NumExpr> {}
pub trait IsNumExpr: AnyExpr + Into<NumExpr> {}
/// Marker trait for Boolean expressions
pub trait IsBoolExpr: Expr + Into<BoolExpr> {}
pub trait IsBoolExpr: AnyExpr + Into<BoolExpr> {}