Files
Graphite/libraries/math-parser/src/context.rs
T
Keavon Chambers aa27f55ab5 Add matrices, with swizzle and range literals, to the math expression parser (#4611)
* Add matrices to the math expression parser as a second sort beside values

* Add Transform input and output to the Math f(x) node

* Add ranges and boxes to the math expression parser with inside, clamp, and remap over them

* Add Transform lists to the Math f(…) node with reducers composing, averaging, and counting them

* Lex a number's trailing e as Euler's number unless an exponent's digits follow it

* Fix matrix inversion scaling, exact matrix powers, infinite shears, matrix means, kept axes, and the Transform default

* Make shared box parts flat, give composed regions their inner map's axes, and fix lone logic items, spaced ranges, and the Math f(…) tooltip

* Transpose across spaces, name fractional matrix powers, check matrix builtin arities when parsing, read range literals by their corners, and let host functions shadow matrix builtins

* Make inside and clamp hold a region's uncovered parts at zero, so a box contains only the points between its corners

* Require hosts to declare the functions they provide, name the region functions' syntax node after its builtin, and remap onto ranges with equal infinite corners

* Apply a matrix to the whole product after it, so M 2 v is M (2 v)

* Pin prefix NOT's precedence above products, comparisons, and AND with tests

* Rename inside to within, since a range's ends count and the geometry predicates with a point first use that name

* Keep a range's corners as written, so clamp orders an integer past the reals' limit exactly

* Give within a tolerance of a billionth of the region's scale, so a point a float error off an edge or a flat side counts

* Write a unit part as its basis alone, so the identity displays as [1;i;j;k]

* Add tests for product folds, mixed chains, remap, and rejected inputs

* Evaluate matrix operands, builder arguments, regions, and comparison chains on the stack instead of the heap

* Build the expression parsers once per thread, since building the combinator tree cost as much as parsing a short expression

* Compare a chain reducer's items by their order directly, and reject an item with no order rather than reading it as false

* Send a host-built list of builder arguments or regions past the stack buffers to the heap instead of rejecting it
2026-09-26 15:11:16 -07:00

104 lines
2.4 KiB
Rust

use crate::matrix::Matrix;
use crate::value::Value;
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
//TODO: editor integration, implement these traits for whatever is needed, maybe merge them if needed
pub trait ValueProvider {
fn get_value(&self, name: &str) -> Option<Value>;
/// The matrix bound to an uppercase-initial name, which the case rule reserves for matrices.
fn get_matrix(&self, _name: &str) -> Option<Matrix> {
None
}
}
pub trait FunctionProvider {
fn run_function(&self, name: &str, args: &[Value]) -> Option<Value>;
/// Whether the host supplies a function of this name, true for every name `run_function` answers. It is read when parsing, since
/// the host's function shadows any builtin of the same name and gives a value where the builtin might give a matrix.
fn provides(&self, name: &str) -> bool;
}
#[derive(Default)]
pub struct ValueMap(pub HashMap<String, Value>);
pub struct NothingMap;
impl<V: ValueProvider> ValueProvider for &V {
fn get_value(&self, name: &str) -> Option<Value> {
(**self).get_value(name)
}
fn get_matrix(&self, name: &str) -> Option<Matrix> {
(**self).get_matrix(name)
}
}
impl ValueProvider for NothingMap {
fn get_value(&self, _: &str) -> Option<Value> {
None
}
}
impl ValueProvider for ValueMap {
fn get_value(&self, name: &str) -> Option<Value> {
self.0.get(name).cloned()
}
}
impl Deref for ValueMap {
type Target = HashMap<String, Value>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for ValueMap {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl FunctionProvider for NothingMap {
fn run_function(&self, _: &str, _: &[Value]) -> Option<Value> {
None
}
fn provides(&self, _: &str) -> bool {
false
}
}
pub struct EvalContext<V: ValueProvider, F: FunctionProvider> {
values: V,
functions: F,
}
impl Default for EvalContext<NothingMap, NothingMap> {
fn default() -> Self {
Self {
values: NothingMap,
functions: NothingMap,
}
}
}
impl<V: ValueProvider, F: FunctionProvider> EvalContext<V, F> {
pub fn new(values: V, functions: F) -> Self {
Self { values, functions }
}
pub fn get_value(&self, name: &str) -> Option<Value> {
self.values.get_value(name)
}
pub fn get_matrix(&self, name: &str) -> Option<Matrix> {
self.values.get_matrix(name)
}
pub fn run_function(&self, name: &str, args: &[Value]) -> Option<Value> {
self.functions.run_function(name, args)
}
}