Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 38 additions & 60 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion editor/src/messages/layout/layout_message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,6 @@ fn evaluate_and_validate_number_input(expression: &str, number_input: &NumberInp
let value = math_parser::evaluate(expression)
.inspect_err(|err| error!("Math parser error on \"{expression}\": {err}"))
.ok()?
.0
.inspect_err(|err| error!("Math evaluate error on \"{expression}\": {err}"))
.ok()?;

Expand Down
4 changes: 1 addition & 3 deletions libraries/math-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ description = "Parser for Graphite style mathematics expressions"
license = "MIT OR Apache-2.0"

[dependencies]
pest = "2.7"
pest_derive = "2.7"
thiserror = "2.0"
lazy_static = "1.5"
num-complex = "0.4"
chumsky = { version = "0.10", default-features = false, features = ["std"] }

[dev-dependencies]
criterion = { workspace = true }
Expand Down
7 changes: 5 additions & 2 deletions libraries/math-parser/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ macro_rules! generate_benchmarks {
$(
c.bench_function(concat!("parse ", $input), |b| {
b.iter(|| {
let _ = black_box(ast::Node::try_parse_from_str($input)).unwrap();
let _ = black_box(ast::Node::try_parse_from_str($input));
});
});
)*
}

fn evaluation_bench(c: &mut Criterion) {
$(
let expr = ast::Node::try_parse_from_str($input).unwrap().0;
let expr = match ast::Node::try_parse_from_str($input) {
Ok(expr) => expr,
Err(err) => panic!("failed to parse `{}`: {err}", $input),
};
let context = EvalContext::default();

c.bench_function(concat!("eval ", $input), |b| {
Expand Down
15 changes: 14 additions & 1 deletion libraries/math-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Unit {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
Float(f64),
Complex(Complex),
Expand All @@ -54,15 +54,27 @@ pub enum BinaryOp {
Add,
Sub,
Mul,
/// Logical AND (nonzero treated as true, returns 1.0 or 0.0)
And,
Div,
/// Logical OR (nonzero treated as true, returns 1.0 or 0.0)
Or,
Modulo,
Pow,
Leq,
Lt,
Geq,
Gt,
Neq,
Eq,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum UnaryOp {
Neg,
Sqrt,
Fac,
Not,
}

#[derive(Debug, PartialEq)]
Expand All @@ -72,4 +84,5 @@ pub enum Node {
FnCall { name: String, expr: Vec<Node> },
BinOp { lhs: Box<Node>, op: BinaryOp, rhs: Box<Node> },
UnaryOp { expr: Box<Node>, op: UnaryOp },
Conditional { condition: Box<Node>, if_block: Box<Node>, else_block: Box<Node> },
}
Loading
Loading