Skip to content

Commit

Permalink
Negation
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Nov 10, 2024
1 parent ea93bae commit 91042b0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/expr/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ impl Evaluator {
super::lexer::TokenKind::LessThan => Ok(lval < rval),
super::lexer::TokenKind::Equal => Ok(lval == rval),
super::lexer::TokenKind::FuzzyEqual => Ok(lval.to_string().contains(rval.to_string().as_str())),
super::lexer::TokenKind::NotEqual => Ok(lval != rval),
super::lexer::TokenKind::NotFuzzyEqual => Ok(!lval.to_string().contains(rval.to_string().as_str())),
super::lexer::TokenKind::Or => Ok(lval.to_bool() || rval.to_bool()),
super::lexer::TokenKind::And => Ok(lval.to_bool() && rval.to_bool()),
_ => Err(format!("unknown operator: {:?}", op)),
Expand Down Expand Up @@ -118,7 +120,9 @@ mod test {
assert_eq!(true, result.unwrap());
let result = Evaluator::new().parse_and_evaluate("type ~ 'Ru'", &map);
assert_eq!(true, result.unwrap());
let result = Evaluator::new().parse_and_evaluate("type ~ 'Rup'", &map);
let result = Evaluator::new().parse_and_evaluate("type !~ 'Rup'", &map);
assert_eq!(true, result.unwrap());
let result = Evaluator::new().parse_and_evaluate("type != 'Run'", &map);
assert_eq!(false, result.unwrap());
}
}
11 changes: 10 additions & 1 deletion src/expr/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub enum TokenKind {
And,
Equal,
FuzzyEqual,
NotEqual,
NotFuzzyEqual,
Name,
Eol,
}
Expand Down Expand Up @@ -69,6 +71,11 @@ impl Lexer<'_> {
}

match c {
'!' => match self.peek(1) {
'=' => self.spawn_advance(TokenKind::NotEqual, 2),
'~' => self.spawn_advance(TokenKind::NotFuzzyEqual, 2),
_ => self.spawn_advance(TokenKind::Unkown, 1),
},
'"' => self.parse_string(),
'\'' => self.parse_string(),
'~' => self.spawn_advance(TokenKind::FuzzyEqual, 1),
Expand All @@ -82,7 +89,7 @@ impl Lexer<'_> {
'=' => self.spawn_advance(TokenKind::LessThanEqual, 2),
_ => self.spawn_advance(TokenKind::LessThan, 1),
},
_ => self.spawn_advance(TokenKind::Unkown, 0),
_ => self.spawn_advance(TokenKind::Unkown, 1),
}
}
};
Expand Down Expand Up @@ -216,6 +223,8 @@ mod test {
assert_eq!(TokenKind::LessThan, Lexer::new("<").next().kind);
assert_eq!(TokenKind::Equal, Lexer::new("=").next().kind);
assert_eq!(TokenKind::FuzzyEqual, Lexer::new("~").next().kind);
assert_eq!(TokenKind::NotEqual, Lexer::new("!=").next().kind);
assert_eq!(TokenKind::NotFuzzyEqual, Lexer::new("!~").next().kind);
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions src/expr/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ impl Parser<'_> {
| TokenKind::Or
| TokenKind::FuzzyEqual
| TokenKind::Equal
| TokenKind::NotFuzzyEqual
| TokenKind::NotEqual
| TokenKind::LessThanEqual
| TokenKind::LessThan => Ok(Expr::Binary(
Box::new(left),
Expand Down

0 comments on commit 91042b0

Please sign in to comment.