feat: implement an Expr trait

This commit is contained in:
Anand Balakrishnan 2023-03-19 23:15:54 -07:00
parent afe265eba2
commit 55894d55fb
No known key found for this signature in database
2 changed files with 85 additions and 0 deletions

View file

@ -4,9 +4,11 @@ mod bool_ops;
mod internal_macros;
pub mod iter;
mod num_ops;
mod traits;
pub use bool_ops::*;
pub use num_ops::*;
pub use traits::*;
use crate::{ArgusResult, Error};
@ -26,6 +28,21 @@ pub enum NumExpr {
Div { dividend: Box<NumExpr>, divisor: Box<NumExpr> },
}
impl Expr for NumExpr {
fn as_any(&self) -> &dyn Any {
self
}
fn args(&self) -> Vec<&dyn Expr> {
match self {
NumExpr::Neg { arg } => vec![arg.as_ref()],
NumExpr::Add { args } | NumExpr::Mul { args } => args.iter().map(|arg| arg as &dyn Expr).collect(),
NumExpr::Div { dividend, divisor } => vec![dividend.as_ref(), divisor.as_ref()],
_ => vec![],
}
}
}
/// Types of comparison operations
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Ordering {
@ -46,6 +63,21 @@ pub enum BoolExpr {
Or { args: Vec<BoolExpr> },
}
impl Expr for BoolExpr {
fn args(&self) -> Vec<&dyn Expr> {
match self {
BoolExpr::Cmp { op: _, lhs, rhs } => vec![lhs.as_ref(), rhs.as_ref()],
BoolExpr::Not { arg } => vec![arg.as_ref()],
BoolExpr::And { args } | BoolExpr::Or { args } => args.iter().map(|arg| arg as &dyn Expr).collect(),
_ => vec![],
}
}
fn as_any(&self) -> &dyn Any {
self
}
}
/// Expression builder
///
/// The `ExprBuilder` is a factory structure that deals with the creation of