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
1,812 changes: 1,009 additions & 803 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions frontend/wrapper/src/editor_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,9 +917,9 @@ pub fn is_platform_native() -> bool {
#[wasm_bindgen(js_name = evaluateMathExpression)]
pub fn evaluate_math_expression(expression: &str) -> Option<f64> {
let value = math_parser::evaluate(expression)
.inspect_err(|err| error!("Math parser error on \"{expression}\": {err}"))
// TODO: Render to html here
.inspect_err(|err| error!("Math parser error on \"{expression}\""))
.ok()?
.0
.inspect_err(|err| error!("Math evaluate error on \"{expression}\": {err} "))
.ok()?;
let Some(real) = value.as_real() else {
Expand Down
4 changes: 2 additions & 2 deletions libraries/math-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ 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"] }
codespan-reporting = { git = "https://github.com/urisinger/codespan-style-writer.git"}

[dev-dependencies]
criterion = { workspace = true }
Expand Down
10 changes: 8 additions & 2 deletions libraries/math-parser/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ 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) => {
err.print();
panic!(concat!("failed to parse `", $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