Files
Graphite/libraries/math-parser/src/lib.rs
T
Keavon Chambers e49c707524 Add combinatorics functions and base-suffixed logs to the math expression parser, and rework its constants (#4559)
* Standardize on the 'nonzero' spelling in comments

* Fill small gaps in the math expression parser

Adds the unary plus operator, the `true` and `false` constants, the
`choose` and `pick` combinatorics functions, base-suffixed `logN`/
`rootN` function names with optional underscores and fractional bases
like `log3.25`, the typeset operator spellings (`−`, `×`, `⋅`, `÷`,
`∧`, `∨`, `¬`), and `Value` accessors reading results as `f32` and the
standard integer types. Constants match their lowercase spelling
exactly, since uppercase-initial names are reserved for matrices. The
`¬` spelling is the prefix logical not alone, never the postfix
factorial that `!` also spells, and punctuation never begins a name,
which leaves `#`, `$`, `~`, and `@` free to become namespace prefixes
once a host scope needs them.

Retires the `√` prefix operator, which is untypeable and duplicates
`sqrt()`, and removes the undocumented unitless `G` constant: its
9.80665 is the value of lowercase-g standard gravity under capital-G's
name, and gravity's proper home is the future unit system's `g`
acceleration unit.

Also covers the previously untested interaction between implicit
multiplication and unary minus with parenthesized operands (`2 (-3)`,
`-3(2)`) in the test suite.
2026-09-20 21:32:23 -07:00

466 lines
17 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
pub mod ast;
pub mod constants;
pub mod context;
pub mod executer;
pub mod lexer;
pub mod parser;
pub mod value;
use context::EvalContext;
use executer::EvalError;
use parser::ParseError;
use value::Value;
pub fn evaluate(expression: &str) -> Result<Result<Value, EvalError>, ParseError> {
let expr = ast::Node::try_parse_from_str(expression);
let context = EvalContext::default();
expr.map(|node| node.eval(&context))
}
#[cfg(test)]
mod tests {
use super::*;
use value::{Complex, Number};
const EPSILON: f64 = 1e-10_f64;
#[test]
fn malformed_juxtaposed_numbers_fail_to_parse() {
// Two numbers cannot be glued together by a stray decimal point (they must not parse as implicit multiplication)
for input in ["1..5", "1.5.5", "1..", ".5.5"] {
assert!(evaluate(input).is_err(), "expected `{input}` to be a parse error");
}
}
#[test]
fn unrecognized_characters_fail_to_parse() {
// Unrecognized trailing input must be rejected rather than silently dropped after a valid prefix
for input in ["2@", "5#", "2 $ 3", "sqrt(4)@", "5 & 3", "5 | 3", "2 = 3"] {
assert!(evaluate(input).is_err(), "expected `{input}` to be a parse error");
}
}
#[test]
fn not_sign_is_prefix_only() {
// `¬` spells only the prefix logical not, so it must not stand in for `!` in its postfix factorial role
for input in ["5¬", "5¬3", "(2 + 3)¬", "3¬¬"] {
assert!(evaluate(input).is_err(), "expected `{input}` to be a parse error");
}
}
#[test]
fn juxtaposed_numbers_fail_to_parse() {
// Adjacent number literals like digit-grouped `10 000` must not silently multiply
for input in ["2 3", "10 000", "1 .5", "sqrt(4).5", "2 3 + 1"] {
assert!(evaluate(input).is_err(), "expected `{input}` to be a parse error");
}
}
#[test]
fn dot_led_function_suffixes_fail_to_parse() {
// A `.`-led base suffix must stay an error, keeping dot-after-identifier free for possible future accessor syntax (the supported spelling is `log0.5`)
for input in ["log.5(8)", "log.5", "root.5(9)"] {
assert!(evaluate(input).is_err(), "expected `{input}` to be a parse error");
}
}
#[test]
fn scientific_function_suffixes_are_not_bases() {
struct ScientificName;
impl context::ValueProvider for ScientificName {
fn get_value(&self, name: &str) -> Option<Value> {
(name == "log2e5").then(|| Value::from_f64(10.))
}
}
// A base is plain decimal, so `log2e5(8)` reads as the variable `log2e5` times 8, never a base-200000 log
let result = ast::Node::try_parse_from_str("log2e5(8)").unwrap().eval(&EvalContext::new(ScientificName, context::NothingMap));
assert_eq!(result.unwrap().as_real(), Some(80.));
}
#[test]
fn underscore_dot_suffix_stays_implicit_multiplication() {
struct LogUnderscore;
impl context::ValueProvider for LogUnderscore {
fn get_value(&self, name: &str) -> Option<Value> {
(name == "log_").then(|| Value::from_f64(10.))
}
}
// `log_.5(8)` is not a suffixed function call: it reads as the variable `log_` times 0.5 times 8
let result = ast::Node::try_parse_from_str("log_.5(8)").unwrap().eval(&EvalContext::new(LogUnderscore, context::NothingMap));
assert_eq!(result.unwrap().as_real(), Some(40.));
}
#[test]
fn extremely_long_fraction_parses() {
let input = format!("0.{}", "1".repeat(320));
let value = evaluate(&input).unwrap().unwrap();
assert_eq!(value.as_real(), Some(1. / 9.));
}
fn run_end_to_end_test(input: &str, expected_value: Value) {
let expr = match ast::Node::try_parse_from_str(input) {
Ok(expr) => expr,
Err(err) => panic!("failed to parse `{input}`: {err}"),
};
let context = EvalContext::default();
let actual_value = match expr.eval(&context) {
Ok(v) => v,
Err(err) => panic!("failed to evaluate `{input}` because of error {err}"),
};
match (actual_value, expected_value) {
(Value::Number(Number::Complex(a)), Value::Number(Number::Complex(e))) => {
// real part
if a.re.is_infinite() || e.re.is_infinite() {
assert!(a.re == e.re, "`{}` → real part: expected {:?}, got {:?}", input, e.re, a.re);
} else {
assert!((a.re - e.re).abs() < EPSILON, "`{}` → real part: expected {}, got {}", input, e.re, a.re);
}
// imag part
if a.im.is_infinite() || e.im.is_infinite() {
assert!(a.im == e.im, "`{}` → imag part: expected {:?}, got {:?}", input, e.im, a.im);
} else {
assert!((a.im - e.im).abs() < EPSILON, "`{}` → imag part: expected {}, got {}", input, e.im, a.im);
}
}
(Value::Number(Number::Real(a)), Value::Number(Number::Real(e))) => {
if a.is_infinite() || e.is_infinite() {
// both must be infinite and equal (i.e. both +∞ or both −∞)
assert!(a == e, "`{input}` → expected infinite {e:?}, got {a:?}");
} else if a.is_nan() || e.is_nan() {
// both must be NaN
assert!(a.is_nan() && e.is_nan(), "`{input}` → expected NaN, got {a:?}");
} else {
let diff = (a - e).abs();
assert!(diff < EPSILON, "`{input}` → expected {e}, got {a}, Δ={diff}");
}
}
(got, expect) => {
panic!("`{input}` → mismatched types: expected {expect:?}, got {got:?}");
}
}
}
macro_rules! test_end_to_end {
($($name:ident: $input:expr => $expected:expr),* $(,)?) => {
$(
#[test]
fn $name() {
run_end_to_end_test($input, ($expected).into());
}
)*
};
}
test_end_to_end! {
// Basic arithmetic
infix_addition: "5 + 5" => 10.,
infix_subtraction: "5 - 3" => 2.,
infix_multiplication: "4 * 4" => 16.,
infix_division: "8/2" => 4.,
modulo_pos_pos: "3.2 % 2" => 1.2,
modulo_pos_neg: "3.2 % -2" => 1.2,
modulo_neg_neg: "(-3.2) % -2" => -1.2,
modulo_neg_pos: "(-3.2) % 2" => -1.2,
exp_pos_pos: "3.2 ^ 2" => 256. / 25.,
exp_pos_neg: "3.2 ^ -2" => 25. / 256.,
exp_neg_neg: "-3.2 ^ -2" => -25. / 256.,
exp_neg_pos: "-3.2 ^ 2" => -256. / 25.,
// Order of operations
order_of_operations_negative_prefix: "-10 + 5" => -5.,
order_of_operations_add_multiply: "5+1*1+5" => 11.,
order_of_operations_add_negative_multiply: "5+(-1)*1+5" => 9.,
order_of_operations_sqrt: "sqrt(25) + 11" => 16.,
order_of_operations_sqrt_expression: "sqrt(25+11)" => 6.,
// Parentheses and nested expressions
parentheses_nested_multiply: "(5 + 3) * (2 + 6)" => 64.,
parentheses_mixed_operations: "2 * (3 + 5 * (2 + 1))" => 36.,
parentheses_divide_add_multiply: "10 / (2 + 3) + (7 * 2)" => 16.,
// Square root and nested square root
sqrt_chain_operations: "sqrt(16) + sqrt(9) * sqrt(4)" => 10.,
sqrt_nested: "sqrt(sqrt(81))" => 3.,
sqrt_divide_expression: "sqrt((25 + 11) / 9)" => 2.,
// Mixed square root and units
sqrt_add_multiply: "sqrt(49) - 1 + 2 * 3" => 12.,
sqrt_addition_multiply: "(sqrt(36) + 2) * 2" => 16.,
// Exponentiation
exponent_single: "2^3" => 8.,
exponent_mixed_operations: "2^3 + 4^2" => 24.,
exponent_nested: "2^(3+1)" => 16.,
exponent_right_associative: "2^2^3" => 256.,
exponent_unary_operand: "2^-1" => 0.5,
// Implicit multiplication binds like `*`/`/`: tighter than `+`, looser than `^`, left to right
implicit_multiplication_constant: "2pi" => 2. * std::f64::consts::PI,
implicit_multiplication_before_addition: "2pi + 1" => 2. * std::f64::consts::PI + 1.,
implicit_multiplication_shares_division: "1/2pi" => std::f64::consts::PI / 2.,
implicit_multiplication_left_to_right: "6/2pi" => 3. * std::f64::consts::PI,
implicit_multiplication_power_operand: "2pi^2" => 2. * std::f64::consts::PI.powi(2),
implicit_multiplication_function: "2sqrt(4)" => 4.,
implicit_multiplication_excludes_unary_minus: "2 -3" => -1.,
// Factorial (postfix !)
factorial_simple: "5!" => 120.,
factorial_nested: "(3 + 2)!" => 120.,
factorial_zero: "0!" => 1.,
factorial_chain: "3!!" => 720., // (3!)! = 6! = 720
// Operations with negative values
negative_nested_parentheses: "-(5 + 3 * (2 - 1))" => -8.,
negative_sqrt_addition: "-(sqrt(16) + sqrt(9))" => -7.,
multiply_sqrt_subtract: "5 * 2 + sqrt(16) / 2 - 3" => 9.,
add_multiply_subtract_sqrt: "4 + 3 * (2 + 1) - sqrt(25)" => 8.,
add_sqrt_subtract_nested_multiply: "10 + sqrt(64) - (5 * (2 + 1))" => 3.,
// Mathematical constants
constant_pi: "pi" => std::f64::consts::PI,
constant_e: "e" => std::f64::consts::E,
constant_phi: "phi" => 1.61803398875,
constant_tau: "tau" => 2. * std::f64::consts::PI,
constant_infinity: "if(inf == ∞, inf, 0)" => f64::INFINITY,
multiply_pi: "2 * pi" => 2. * std::f64::consts::PI,
add_e_constant: "e + 1" => std::f64::consts::E + 1.,
multiply_phi_constant: "phi * 2" => 1.61803398875 * 2.,
exponent_tau: "2^tau" => 2f64.powf(2. * std::f64::consts::PI),
infinity_subtract_large_number: "inf - 1000" => f64::INFINITY,
// Decimals with no leading digit before the point
leading_dot_decimal: ".5" => 0.5,
leading_dot_in_expression: "1+.5" => 1.5,
leading_dot_exponent: ".5e3" => 500.,
// Trigonometric functions
trig_sin_pi: "sin(pi)" => 0.,
trig_cos_zero: "cos(0)" => 1.,
trig_tan_pi_div_four: "tan(pi/4)" => 1.,
trig_sin_tau: "sin(tau)" => 0.,
trig_cos_tau_div_two: "cos(tau/2)" => -1.,
trig_csc: "csc(pi/2)" => 1.,
trig_sec: "sec(0)" => 1.,
trig_cot: "cot(pi/4)" => 1.,
// Inverse trig aliases
inverse_trig_asin: "asin(1)" => std::f64::consts::FRAC_PI_2,
inverse_trig_acos: "acos(1)" => 0.,
inverse_trig_atan: "atan(1)" => std::f64::consts::FRAC_PI_4,
inverse_trig_acsc: "acsc(1)" => std::f64::consts::FRAC_PI_2,
inverse_trig_asec: "asec(1)" => 0.,
inverse_trig_acot: "acot(1)" => std::f64::consts::FRAC_PI_4,
// Hyperbolic and reciprocal hyperbolic
hyperbolic_sinh: "sinh(0)" => 0.,
hyperbolic_cosh: "cosh(0)" => 1.,
hyperbolic_tanh: "tanh(0)" => 0.,
hyperbolic_csch: "csch(1)" => 1f64.sinh().recip(),
hyperbolic_sech: "sech(0)" => 1.,
hyperbolic_coth: "coth(1)" => 1f64.tanh().recip(),
// Inverse hyperbolic
inverse_hyperbolic_asinh: "asinh(0)" => 0.,
inverse_hyperbolic_acosh: "acosh(1)" => 0.,
inverse_hyperbolic_atanh: "atanh(0)" => 0.,
inverse_hyperbolic_acsch: "acsch(1)" => 1f64.asinh(),
inverse_hyperbolic_asech: "asech(1)" => 1f64.acosh(),
inverse_hyperbolic_acoth: "acoth(2)" => 0.5f64.atanh(),
// Basic if statements
if_true_condition: "if(1,5,3)" => 5.,
if_false_condition: "if(0, 5, 3)" => 3.,
// Arithmetic conditions
if_arithmetic_true: "if(2+2-4, 1 , 0)" => 0.,
if_arithmetic_false: "if(3*2-5, 1, 0)" => 1.,
// Nested arithmetic
if_complex_arithmetic: "if((5+3)*(2-1), 10, 20)" => 10.,
if_with_division: "if(8/4-2 == 0, 15, 25)" => 15.,
if_with_division_ne: "if(8/4-2 ≠ 0, 15, 25)" => 25.,
// Constants in conditions
if_with_pi: "if(pi > 3, 1, 0)" => 1.,
if_with_e: "if(e < 3, 1, 0)" => 1.,
// Functions in conditions
if_with_sqrt: "if(sqrt(16) == 4, 1, 0)" => 1.,
if_with_sin: "if(sin(pi) == 0.0, 1, 0)" => 0.,
// Logical NOT (prefix !)
logical_not_zero: "!0" => 1.,
logical_not_nonzero: "!5" => 0.,
logical_not_expression: "!(2 - 2)" => 1.,
// Log / exp / pow / root
log_ln: "ln(e)" => 1.,
log_log10: "log(100)" => 2.,
log_log2: "log2(8)" => 3.,
log_change_of_base: "log(8, 2)" => 3.,
exp_function: "exp(1)" => std::f64::consts::E,
root_square: "root(9, 2)" => 3.,
root_cube: "root(8, 3)" => 2.,
// Nested if statements
nested_if: "if(1, if(0, 1, 2), 3)" => 2.,
nested_if_complex: "if(2-2 == 0, if(1, 5, 6), if(1, 7, 8))" => 5.,
// Mixed operations in conditions and blocks
if_complex_condition: "if(sqrt(16) + sin(pi) < 5, 2*pi, 3*e)" => 2. * std::f64::consts::PI,
if_complex_blocks: "if(1, 2*sqrt(16) + sin(pi/2), 3*cos(0) + 4)" => 9.,
// Mapping helpers
mapping_trunc: "trunc(3.7)" => 3.,
mapping_fract: "fract(3.25)" => 0.25,
mapping_sign_pos: "sign(5)" => 1.,
mapping_sign_neg: "sign(-5)" => -1.,
// Geometry / mapping extras
geometry_hypot: "hypot(3, 4)" => 5.,
// Minimum and maximum accept two or more arguments
mapping_min: "min(3, 7)" => 3.,
mapping_max: "max(3, 7)" => 7.,
mapping_min_variadic: "min(5, 2, 8, 4)" => 2.,
mapping_max_variadic: "max(5, 2, 8, 4)" => 8.,
mapping_min_skips_nan: "min(sqrt(-1), 5)" => 5.,
mapping_max_skips_nan: "max(sqrt(-1), 5)" => 5.,
mapping_min_all_nan: "min(sqrt(-1))" => f64::NAN,
mapping_max_all_nan: "max(sqrt(-1))" => f64::NAN,
// Typeset math symbol aliases
alias_minus_sign: "5 − 3" => 2.,
alias_unary_minus_sign: "−5 + 6" => 1.,
alias_multiplication_sign: "3 × 4" => 12.,
alias_dot_operator: "3 ⋅ 4" => 12.,
alias_division_sign: "8 ÷ 2" => 4.,
alias_logical_and: "if(1 ∧ 1, 2, 3)" => 2.,
alias_logical_or: "if(0 ∨ 1, 2, 3)" => 2.,
alias_logical_not: "¬0" => 1.,
mapping_remap: "remap(5, 0, 10, 0, 100)" => 50.,
// GCD / LCM
gcd_simple: "gcd(24, 18)" => 6.,
lcm_simple: "lcm(4, 6)" => 12.,
gcd_negative_operand: "gcd(-24, 18)" => 6.,
lcm_negative_operand: "lcm(-4, 6)" => 12.,
// Combinatorics over whole numbers
combinatorics_choose: "choose(5, 2)" => 10.,
combinatorics_choose_symmetric: "choose(30, 28)" => 435.,
combinatorics_choose_beyond_n: "choose(3, 5)" => 0.,
combinatorics_pick: "pick(5, 2)" => 20.,
combinatorics_pick_all: "pick(4, 4)" => 24.,
// A result beyond f64 stops at infinity instead of stepping through quadrillions of terms
combinatorics_choose_overflows: "choose(9007199254740992, 4503599627370496)" => f64::INFINITY,
combinatorics_pick_overflows: "pick(9007199254740992, 9007199254740992)" => f64::INFINITY,
// Truth values are the numbers 1 and 0
constant_truth_values: "true + true - false" => 2.,
// atan2
trig_atan2_axis: "atan2(1, 0)" => std::f64::consts::FRAC_PI_2,
// Comparison operators combined with logical AND
comparison_operators: "if(1 <= 2 && 1 ≤ 2 && 2 >= 1 && 2 ≥ 1, 1., 0.)" => 1.,
// Logical AND / OR
logical_and_true: "if(1 <= 2 && 2 < 3, 1., 0.)" => 1.,
logical_and_false: "if(1 <= 2 && 3 < 2, 1., 0.)" => 0.,
logical_or_true_left: "if(1 > 2 || 2 < 3, 1., 0.)" => 1.,
logical_or_true_right: "if(2 < 1 || 2 < 3, 1., 0.)" => 1.,
logical_or_false: "if(1 > 2 || 3 < 2, 1., 0.)" => 0.,
logical_precedence_and_over_or: "if(0 == 1 || 1 == 1 && 0 == 0, 1., 0.)" => 1.,
// Edge cases
if_zero: "if(0.0, 1, 2)" => 2.,
// Complex nested expressions
if_nested_expr: "if((sqrt(16) + 2) * (sin(pi) + 1), 3 + 4 * 2, 5 - 2 / 1)" => 11.,
// Overflow-safe evaluation
factorial_overflows_to_infinity: "171!" => f64::INFINITY,
factorial_huge_input: "10000000000000000000000!" => f64::INFINITY,
lcm_huge_no_overflow: "lcm(1099511627776, 1099511627775)" => 1099511627776. * 1099511627775.,
gcd_non_finite: "gcd(inf, 6)" => f64::NAN,
long_literal: "10000000000000000000000" => 1e22,
huge_exponent_saturates: "1e4294967296" => f64::INFINITY,
// Odd integer roots of negative values are real
root_negative_odd: "root(-8, 3)" => -2.,
root_negative_odd_reciprocal: "root(-8, -3)" => -0.5,
root_negative_even: "root(-4, 2)" => f64::NAN,
// NaN poisons conditions and logic instead of acting as a boolean
if_nan_condition: "if(sqrt(-1), 1, 2)" => f64::NAN,
nan_and: "sqrt(-1) && 1" => f64::NAN,
nan_or: "sqrt(-1) || 1" => f64::NAN,
nan_not: "!sqrt(-1)" => f64::NAN,
// Logic and equality span real and complex operands
mixed_equality: "1 == i" => 0.,
complex_equality: "i == i" => 1.,
mixed_and: "1 && i" => 1.,
mixed_nan_and: "sqrt(-1) && i" => f64::NAN,
// Correctly rounded literals via std parsing
seventeen_digit_literal: "999999999999999999" => 1e18,
long_fraction_literal: "0.1111111111111111111111111111111111111111" => 1. / 9.,
// Integer functions reject inputs beyond f64's exact integer range
gcd_beyond_exact_integers: "gcd(10000000000000000000, 2)" => f64::NAN,
// Implicit multiplication with parenthesized and negative-coefficient operands
implicit_multiplication_parenthesized_negative: "2 (-3)" => -6.,
implicit_multiplication_glued_parenthesized_negative: "2(-3)" => -6.,
implicit_multiplication_negative_coefficient: "-3(2)" => -6.,
// Unary plus
unary_plus: "+5" => 5.,
unary_plus_spaced_addition: "1 +2" => 3.,
unary_plus_exponent: "2^+3" => 8.,
// Base-suffixed logarithm and root function names
log_suffixed: "log10(100)" => 2.,
log_suffixed_underscore: "log_10(100)" => 2.,
log_suffixed_fractional_base: "log3.25(5)" => 5f64.ln() / 3.25f64.ln(),
root_suffixed: "root2(9)" => 3.,
root_suffixed_underscore: "root_3(8)" => 2.,
// Change of base widens into the complex plane, including through the base-suffixed spellings
log_complex_change_of_base: "log(i, 2)" => Complex::new(0., std::f64::consts::FRAC_PI_2 / std::f64::consts::LN_2),
log_complex_suffixed_base: "log3(i)" => Complex::new(0., std::f64::consts::FRAC_PI_2 / 3f64.ln()),
}
#[test]
fn sigils_do_not_begin_names() {
// Punctuation never begins a name, so these stay available as future namespace prefixes
for input in ["# + 1", "#foo", "$", "$foo", "~foo * 2", "@foo", "2 ~ 3"] {
assert!(ast::Node::try_parse_from_str(input).is_err(), "expected `{input}` to be a parse error");
}
}
#[test]
fn value_accessors_read_reals_only() {
let value = evaluate("2.6").unwrap().unwrap();
assert_eq!(value.as_f32(), Some(2.6_f32));
assert_eq!(value.as_u8(), Some(3));
assert_eq!(value.as_i32(), Some(3));
let negative = evaluate("-2.6").unwrap().unwrap();
assert_eq!(negative.as_i8(), Some(-3));
assert_eq!(negative.as_u8(), None);
assert_eq!(evaluate("300").unwrap().unwrap().as_u8(), None);
assert_eq!(evaluate("i").unwrap().unwrap().as_i64(), None);
assert_eq!(evaluate("inf").unwrap().unwrap().as_u64(), None);
}
}