Skip to content

Commit

Permalink
evaluation of rules simple comparison first
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhibas committed Feb 13, 2024
1 parent 1b90d29 commit 4342983
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bool_expr_parser_nom"
version = "0.2.1"
version = "0.2.2"
edition = "2021"
authors = ["Nikolajus Krauklis <[email protected]>"]

Expand Down
29 changes: 23 additions & 6 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,35 @@ use core::fmt;
use chrono::NaiveDate;

/// TODO: add date and datetime as its common
#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq)]
pub enum Atom {
String(String),
Number(i64),
Number(i32),
Float(f64),
Boolean(bool),
Variable(String),
Date(NaiveDate),
DateTime(String),
}

impl PartialOrd for Atom {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match self {
Atom::Number(v) => match other {
Atom::Number(v2) => Some(v.cmp(v2)),
Atom::Float(v2) => f64::from(*v).partial_cmp(v2),
_ => None,
},
Atom::Float(v) => match other {
Atom::Float(v2) => v.partial_cmp(v2),
Atom::Number(v2) => v.partial_cmp(&f64::from(*v2)),
_ => None,
},
_ => None,
}
}
}

impl fmt::Display for Atom {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down Expand Up @@ -108,10 +126,9 @@ pub enum AstNode {
impl AstNode {
pub fn as_str(&self) -> Option<&str> {
match self {
AstNode::Variable(val) => match val {
Atom::Variable(s) => Some(s.as_str()),
_ => None,
},
AstNode::Variable(Atom::Variable(s)) => Some(s.as_str()),
AstNode::Constant(Atom::String(s)) => Some(s.as_str()),
AstNode::Constant(Atom::Variable(s)) => Some(s.as_str()),
_ => None,
}
}
Expand Down
32 changes: 22 additions & 10 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ pub fn eval<'a>(expr: &AstNode, context: &HashMap<&str, Atom>) -> Result<bool, &
.unwrap();

if let Some(c_val) = context_val {
dbg!(c_val, val_content);
let res = match op {
match op {
ComparisonOp::More => c_val > val_content,
ComparisonOp::MoreEq => c_val >= val_content,
_ => false,
};
dbg!(context_val);
res
ComparisonOp::Less => c_val < val_content,
ComparisonOp::LessEq => c_val <= val_content,
ComparisonOp::Eq => c_val == val_content,
ComparisonOp::NotEq => c_val != val_content,
}
} else {
false
}
}
_ => false,
};
dbg!(expr);
Ok(result)
}

Expand All @@ -45,9 +44,22 @@ mod tests {
("a", Atom::Number(3)),
("b", Atom::String("demo".to_string())),
]);
let (i, expr) = parse("a < 4 ").unwrap();

let res = eval(&expr, &context).unwrap();
assert_eq!(res, false);
assert_eq!(eval(&parse("a < 4").unwrap().1, &context).unwrap(), true);
assert_eq!(eval(&parse("a < 3.3").unwrap().1, &context).unwrap(), true);
assert_eq!(
eval(
&parse("a < 3.1415").unwrap().1,
&HashMap::from([("a", Atom::Float(3.0))])
)
.unwrap(),
true
);
assert_eq!(eval(&parse("a>4").unwrap().1, &context).unwrap(), false);
assert_eq!(eval(&parse("a<=4").unwrap().1, &context).unwrap(), true);
assert_eq!(eval(&parse("a>=3").unwrap().1, &context).unwrap(), true);
assert_eq!(eval(&parse("a!=4").unwrap().1, &context).unwrap(), true);
assert_eq!(eval(&parse("a==4").unwrap().1, &context).unwrap(), false);
assert_eq!(eval(&parse("a==3").unwrap().1, &context).unwrap(), true);
}
}

0 comments on commit 4342983

Please sign in to comment.