Skip to content

Commit

Permalink
Integrate into the editor
Browse files Browse the repository at this point in the history
  • Loading branch information
0HyperCube committed Oct 13, 2024
1 parent e0a21bc commit 6a791c0
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 13 deletions.
63 changes: 63 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ graph-craft = { path = "node-graph/graph-craft", features = ["serde"] }
wgpu-executor = { path = "node-graph/wgpu-executor" }
bezier-rs = { path = "libraries/bezier-rs", features = ["dyn-any"] }
path-bool = { path = "libraries/path-bool", default-features = false }
math-parser = { path = "libraries/math-parser" }
node-macro = { path = "node-graph/node-macro" }

# Workspace dependencies
Expand Down
1 change: 1 addition & 0 deletions frontend/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ wasm-bindgen-futures = { workspace = true }
bezier-rs = { workspace = true }
glam = { workspace = true }
meval = { workspace = true }
math-parser = { workspace = true }
wgpu = { workspace = true, features = [
"fragile-send-sync-non-atomic-wasm",
] } # We don't have wgpu on multiple threads (yet) https://github.com/gfx-rs/wgpu/blob/trunk/CHANGELOG.md#wgpu-types-now-send-sync-on-wasm
Expand Down
18 changes: 6 additions & 12 deletions frontend/wasm/src/editor_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,18 +916,12 @@ impl EditorHandle {

#[wasm_bindgen(js_name = evaluateMathExpression)]
pub fn evaluate_math_expression(expression: &str) -> Option<f64> {
// TODO: Rewrite our own purpose-built math expression parser that supports unit conversions.

let mut context = meval::Context::new();
context.var("tau", std::f64::consts::TAU);
context.func("log", f64::log10);
context.func("log10", f64::log10);
context.func("log2", f64::log2);

// Insert asterisks where implicit multiplication is used in the expression string
let expression = implicit_multiplication_preprocess(expression);

meval::eval_str_with_context(expression, &context).ok()
let value = math_parser::evaluate(expression).inspect_err(|err| error!("Math parser error on \"{expression}\": {err}")).ok()?;
let Some(real) = value.as_real() else {
error!("{value} was not a real; skipping.");
return None;
};
Some(real)
}

// Modified from this public domain snippet: <https://gist.github.com/Titaniumtown/c181be5d06505e003d8c4d1e372684ff>
Expand Down
6 changes: 5 additions & 1 deletion libraries/math-parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[package]
name = "math-parser"
version = "0.1.0"
version = "0.0.0"
rust-version = "1.79"
edition = "2021"
authors = ["Graphite Authors <contact@graphite.rs>"]
description = "Parser for Graphite style mathematics expressions"
license = "MIT OR Apache-2.0"

[dependencies]
pest = "2.7"
Expand Down
18 changes: 18 additions & 0 deletions libraries/math-parser/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,28 @@ impl Value {
pub fn from_f64(x: f64) -> Self {
Self::Complex(x, 0.0)
}
/// Attempt to convert to a real number
pub fn as_real(&self) -> Option<f64> {
match self {
Self::Complex(real, imaginary) if imaginary.abs() < f64::EPSILON => Some(*real),
_ => None,
}
}
}

impl From<f64> for Value {
fn from(x: f64) -> Self {
Self::from_f64(x)
}
}

impl core::fmt::Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(real) = self.as_real() {
return real.fmt(f);
}
match self {
Value::Complex(real, imaginary) => write!(f, "{real}{imaginary:+}i"),
}
}
}

0 comments on commit 6a791c0

Please sign in to comment.