Skip to content

Commit 8229bf6

Browse files
urisingerKeavon
authored andcommitted
Rewrite the math-parser library using a chumsky-based lexer and parser, adding functions, comparisons, logic, and conditionals
1 parent b2e5b66 commit 8229bf6

13 files changed

Lines changed: 1457 additions & 575 deletions

File tree

‎Cargo.lock‎

Lines changed: 38 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎editor/src/messages/layout/layout_message_handler.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,6 @@ fn evaluate_and_validate_number_input(expression: &str, number_input: &NumberInp
550550
let value = math_parser::evaluate(expression)
551551
.inspect_err(|err| error!("Math parser error on \"{expression}\": {err}"))
552552
.ok()?
553-
.0
554553
.inspect_err(|err| error!("Math evaluate error on \"{expression}\": {err}"))
555554
.ok()?;
556555

‎libraries/math-parser/Cargo.toml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ description = "Parser for Graphite style mathematics expressions"
88
license = "MIT OR Apache-2.0"
99

1010
[dependencies]
11-
pest = "2.7"
12-
pest_derive = "2.7"
1311
thiserror = "2.0"
1412
lazy_static = "1.5"
1513
num-complex = "0.4"
14+
chumsky = { version = "0.10", default-features = false, features = ["std"] }
1615

1716
[dev-dependencies]
1817
criterion = { workspace = true }

‎libraries/math-parser/benches/bench.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ macro_rules! generate_benchmarks {
99
$(
1010
c.bench_function(concat!("parse ", $input), |b| {
1111
b.iter(|| {
12-
let _ = black_box(ast::Node::try_parse_from_str($input)).unwrap();
12+
let _ = black_box(ast::Node::try_parse_from_str($input));
1313
});
1414
});
1515
)*
1616
}
1717

1818
fn evaluation_bench(c: &mut Criterion) {
1919
$(
20-
let expr = ast::Node::try_parse_from_str($input).unwrap().0;
20+
let expr = match ast::Node::try_parse_from_str($input) {
21+
Ok(expr) => expr,
22+
Err(err) => panic!("failed to parse `{}`: {err}", $input),
23+
};
2124
let context = EvalContext::default();
2225

2326
c.bench_function(concat!("eval ", $input), |b| {

‎libraries/math-parser/src/ast.rs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Unit {
3737
}
3838
}
3939

40-
#[derive(Debug, PartialEq)]
40+
#[derive(Debug, Clone, PartialEq)]
4141
pub enum Literal {
4242
Float(f64),
4343
Complex(Complex),
@@ -54,15 +54,27 @@ pub enum BinaryOp {
5454
Add,
5555
Sub,
5656
Mul,
57+
/// Logical AND (nonzero treated as true, returns 1.0 or 0.0)
58+
And,
5759
Div,
60+
/// Logical OR (nonzero treated as true, returns 1.0 or 0.0)
61+
Or,
62+
Modulo,
5863
Pow,
64+
Leq,
65+
Lt,
66+
Geq,
67+
Gt,
68+
Neq,
69+
Eq,
5970
}
6071

6172
#[derive(Debug, PartialEq, Clone, Copy)]
6273
pub enum UnaryOp {
6374
Neg,
6475
Sqrt,
6576
Fac,
77+
Not,
6678
}
6779

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

0 commit comments

Comments
 (0)