Skip to content

Commit

Permalink
adding nested scope
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhibas committed Feb 16, 2024
1 parent b17b0aa commit 2d080c5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 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.5"
version = "0.2.6"
edition = "2021"
authors = ["Nikolajus Krauklis <[email protected]>"]

Expand Down
8 changes: 4 additions & 4 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ impl PartialEq<Atom> for Atom {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Atom::String(s1), Atom::String(s2)) => s1 == s2,
(Atom::Variable(v1), Atom::Variable(v2)) => v1 == v2,
(Atom::String(v1), Atom::Variable(v2)) => v1 == v2,
(Atom::Variable(v1), Atom::String(v2)) => v1 == v2,
(Atom::Variable(v1), Atom::Variable(v2)) => v1 == v2,
(Atom::String(v1), Atom::Variable(v2)) => v1 == v2,
(Atom::Variable(v1), Atom::String(v2)) => v1 == v2,
(Atom::Number(n1), Atom::Number(n2)) => n1 == n2,
(Atom::Float(f1), Atom::Float(f2)) => f1 == f2,
(Atom::Float(f1), Atom::Float(f2)) => f1 == f2,
(Atom::Boolean(b1), Atom::Boolean(b2)) => b1 == b2,
(Atom::Date(d1), Atom::Date(d2)) => d1 == d2,
(Atom::DateTime(t1), Atom::DateTime(t2)) => t1 == t2,
Expand Down
48 changes: 48 additions & 0 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ pub fn eval<'a>(expr: &AstNode, context: &HashMap<&str, Atom>) -> Result<bool, &
}
result
}
// todo Function, Logic, Scope
AstNode::Logic(expr1, op, expr2) => {
todo!()
}
AstNode::Function(func, variable) => {
todo!()
}
AstNode::Scope { expr, negate } => {
let res = eval(expr, context).unwrap();
match negate {
true => !res,
false => res,
}
}
_ => false,
};
Ok(result)
Expand All @@ -85,6 +99,40 @@ mod tests {

use super::*;

#[test]
fn simple_scope_test() {
let (i, expr) = parse("!(country=LT)").unwrap();
assert_eq!(
false,
eval(
&expr,
&HashMap::from([("country", Atom::String("LT".to_string()))])
)
.unwrap()
);

// scope inside scope
let (i, expr) = parse("(not (country == Lithuania))").unwrap();
assert_eq!(
false,
eval(
&expr,
&HashMap::from([("country", Atom::String("Lithuania".to_string()))])
)
.unwrap()
);

let (i, expr) = parse("((country == Netherlands))").unwrap();
assert_eq!(
true,
eval(
&expr,
&HashMap::from([("country", Atom::String("Netherlands".to_string()))])
)
.unwrap()
);
}

#[test]
fn array_eval_test() {
let context = HashMap::from([
Expand Down

0 comments on commit 2d080c5

Please sign in to comment.