Skip to content

Commit

Permalink
feat: try compare with exprtk #13
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 7, 2022
1 parent 7116093 commit 7dcee06
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions extensions/ext-computing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ lazy_static = "1.4.0"
actix-web = "4"
tokio = { version = "1", features = ["full"] }
futures = "0.3.18"
exprtk_rs="0.1.0"
2 changes: 1 addition & 1 deletion extensions/ext-computing/src/expr/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ program = _{ SOI ~ expr ~ EOI }
stmt = _{ assign | expr }
assign = { ident ~ "=" ~ expr }
expr = { prefix* ~ primary ~ postfix* ~ (infix ~ prefix* ~ primary ~ postfix* )* }
primary = _{ constants | function | variable | num | "(" ~ expr ~ ")" }
primary = _{ (num ~ constants) | constants | function | variable | num | "(" ~ expr ~ ")" }
infix = _{ sub | add | mul | div | pow | percentOf | percentOn | rightShift | leftShift | modulus }
prefix = _{ neg }
postfix = _{ fac }
Expand Down
22 changes: 19 additions & 3 deletions extensions/ext-computing/src/expr/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ lazy_static! {
.op(Op::infix(Rule::mul, Assoc::Left) | Op::infix(Rule::div, Assoc::Left))
.op(Op::infix(Rule::pow, Assoc::Right))
.op(Op::postfix(Rule::fac))
.op(Op::postfix(Rule::e))
.op(Op::prefix(Rule::neg))
};
}
Expand All @@ -27,6 +28,7 @@ pub fn parse(input: &str, vars: &BTreeMap<String, Instruction>) -> f64 {
match Calculator::parse(Rule::program, input) {
Ok(mut pairs) => {
let expr = parse_expr(pairs.next().unwrap().into_inner());
println!("expr: {:?}", expr);
namespace.eval(expr)
}
Err(err) => {
Expand Down Expand Up @@ -102,6 +104,11 @@ fn parse_expr(pairs: Pairs<Rule>) -> Instruction {
Rule::pow => Instruction::Pow { lhs: Box::from(lhs), rhs: Box::from(rhs) },
_ => panic!("unimplemented: {:?}", op),
})
.map_postfix(|lhs, op: Pair<Rule>| match op.as_rule() {
Rule::fac => Instruction::Fac { val: Box::from(lhs) },
Rule::e => Instruction::Mul { lhs: Box::from(lhs), rhs: Box::from(Instruction::Const(std::f64::consts::E)) },
_ => panic!("unimplemented: {:?}", op),
})
.parse(pairs)
}

Expand Down Expand Up @@ -154,13 +161,22 @@ mod tests {
}

#[test]
fn function_sqrt() {
fn function_demo() {
assert_eq!(parse("sqrt(4)", &Default::default()), 2.0);
let vars = BTreeMap::from_iter(vec![("x".to_string(), Instruction::Const(2.0))]);
assert_eq!(parse("sqrt(1 - (3 / x^2))", &vars), 0.5);
assert_eq!(parse("sqrt(1 - (3 / 3^2))", &vars), 0.816496580927726);
}

#[test]
fn const_e() {
let vars2 = BTreeMap::from_iter(vec![("x".to_string(), Instruction::Const(2.0))]);
assert_eq!(parse("sin(2.34e-3 * x)", &vars2), 0.004679982916146709);

let mut symbol_table = exprtk_rs::SymbolTable::new();
symbol_table.add_variable("x", 2.0).unwrap().unwrap();
let mut expr = exprtk_rs::Expression::new("sin(2.34e-3 * x)", symbol_table).unwrap();

let vars2 = BTreeMap::from_iter(vec![("x".to_string(), Instruction::Const(3.0))]);
assert_eq!(parse("sin(2.34e-x *2)", &vars2), 0.00467998291);
assert_eq!(expr.value(), 0.004679982916146709);
}
}
3 changes: 3 additions & 0 deletions extensions/ext-computing/src/expr/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ pub enum Instruction {
// Functions
FuncSin,
FuncCos,
Fac {
val: Box<Instruction>,
},

// Others
// with Builtin Types?
Expand Down

0 comments on commit 7dcee06

Please # to comment.