-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,53 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::ast::AstNode; | ||
use crate::ast::{AstNode, Atom, ComparisonOp}; | ||
|
||
pub fn eval(expr: AstNode, context: HashMap<&str, &str>) {} | ||
pub fn eval<'a>(expr: &AstNode, context: &HashMap<&str, Atom>) -> Result<bool, &'a str> { | ||
let mut result = false; | ||
result = match expr { | ||
AstNode::Compare(var, op, val) => { | ||
// check var in context | ||
// compare with val | ||
let context_val = context.get(var.as_str().unwrap()); | ||
let val_content = match val.as_ref() { | ||
AstNode::Constant(a) => Some(a), | ||
_ => None, | ||
} | ||
.unwrap(); | ||
|
||
if let Some(c_val) = context_val { | ||
dbg!(c_val, val_content); | ||
let res = match op { | ||
ComparisonOp::More => c_val > val_content, | ||
ComparisonOp::MoreEq => c_val >= val_content, | ||
_ => false, | ||
}; | ||
dbg!(context_val); | ||
res | ||
} else { | ||
false | ||
} | ||
} | ||
_ => false, | ||
}; | ||
dbg!(expr); | ||
Ok(result) | ||
} | ||
|
||
mod tests { | ||
use crate::{ast::Atom, parse::parse}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_basic_eval() { | ||
let context = HashMap::from([ | ||
("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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters