diff --git a/src/egui_app.rs b/src/egui_app.rs index 884437f..91c6d78 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -1,6 +1,6 @@ use crate::function::{FunctionEntry, RiemannSum, EMPTY_FUNCTION_ENTRY}; use crate::misc::{debug_log, digits_precision, log_helper}; -use crate::parsing::{add_asterisks, test_func}; +use crate::parsing::{process_func_str, test_func}; use const_format::formatc; use eframe::{egui, epi}; @@ -404,7 +404,7 @@ impl MathApp { ui.text_edit_singleline(&mut self.func_strs[i]); }); - let proc_func_str = add_asterisks(self.func_strs[i].clone()); + let proc_func_str = process_func_str(self.func_strs[i].clone()); if integral_toggle | derivative_toggle | max_x_changed diff --git a/src/parsing.rs b/src/parsing.rs index ca8781d..64168e9 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -40,9 +40,9 @@ lazy_static::lazy_static! { /* EXTREMELY Janky function that tries to put asterisks in the proper places to be parsed. This is so cursed. But it works, and I hopefully won't ever have to touch it again. One limitation though, variables with multiple characters like `pi` cannot be multiplied (like `pipipipi` won't result in `pi*pi*pi*pi`). But that's such a niche use case (and that same thing could be done by using exponents) that it doesn't really matter. -In the future I may want to completely rewrite this or implement this natively into mevel-rs (which would probably be good to do) +In the future I may want to completely rewrite this or implement this natively in exmex. */ -pub fn add_asterisks(function_in: String) -> String { +pub fn process_func_str(function_in: String) -> String { let function = function_in.replace("log10(", "log(").replace("pi", "π"); // pi -> π and log10 -> log let function_chars: Vec = function.chars().collect(); let mut output_string: String = String::new(); @@ -133,7 +133,7 @@ pub fn test_func(function_string: &str) -> Option { // Used for testing: passes function to `add_asterisks` before running `test_func` #[cfg(test)] fn test_func_helper(function_string: &str) -> Option { - test_func(&add_asterisks(function_string.to_string())) + test_func(&process_func_str(function_string.to_string())) } #[test] @@ -155,31 +155,34 @@ fn test_func_test() { // Tests to make sure my cursed function works as intended #[test] -fn asterisk_test() { - assert_eq!(&add_asterisks("2x".to_string()), "2*x"); - assert_eq!(&add_asterisks("x2".to_string()), "x*2"); - assert_eq!(&add_asterisks("x(1+3)".to_string()), "x*(1+3)"); - assert_eq!(&add_asterisks("(1+3)x".to_string()), "(1+3)*x"); - assert_eq!(&add_asterisks("sin(x)".to_string()), "sin(x)"); - assert_eq!(&add_asterisks("2sin(x)".to_string()), "2*sin(x)"); - assert_eq!(&add_asterisks("max(x)".to_string()), "max(x)"); - assert_eq!(&add_asterisks("2e^x".to_string()), "2*e^x"); - assert_eq!(&add_asterisks("2max(x)".to_string()), "2*max(x)"); - assert_eq!(&add_asterisks("cos(sin(x))".to_string()), "cos(sin(x))"); - assert_eq!(&add_asterisks("x^(1+2x)".to_string()), "x^(1+2*x)"); - assert_eq!(&add_asterisks("(x+2)x(1+3)".to_string()), "(x+2)*x*(1+3)"); - assert_eq!(&add_asterisks("(x+2)(1+3)".to_string()), "(x+2)*(1+3)"); - assert_eq!(&add_asterisks("xxx".to_string()), "x*x*x"); - assert_eq!(&add_asterisks("eee".to_string()), "e*e*e"); - assert_eq!(&add_asterisks("pi(x+2)".to_string()), "π*(x+2)"); - assert_eq!(&add_asterisks("(x)pi".to_string()), "(x)*π"); - assert_eq!(&add_asterisks("2e".to_string()), "2*e"); - assert_eq!(&add_asterisks("2log10(x)".to_string()), "2*log(x)"); - assert_eq!(&add_asterisks("2log(x)".to_string()), "2*log(x)"); - assert_eq!(&add_asterisks("x!".to_string()), "x!"); - assert_eq!(&add_asterisks("pipipipipipi".to_string()), "π*π*π*π*π*π"); - assert_eq!(&add_asterisks("10pi".to_string()), "10*π"); - assert_eq!(&add_asterisks("pi10".to_string()), "π*10"); +fn func_process_test() { + assert_eq!(&process_func_str("2x".to_string()), "2*x"); + assert_eq!(&process_func_str("x2".to_string()), "x*2"); + assert_eq!(&process_func_str("x(1+3)".to_string()), "x*(1+3)"); + assert_eq!(&process_func_str("(1+3)x".to_string()), "(1+3)*x"); + assert_eq!(&process_func_str("sin(x)".to_string()), "sin(x)"); + assert_eq!(&process_func_str("2sin(x)".to_string()), "2*sin(x)"); + assert_eq!(&process_func_str("max(x)".to_string()), "max(x)"); + assert_eq!(&process_func_str("2e^x".to_string()), "2*e^x"); + assert_eq!(&process_func_str("2max(x)".to_string()), "2*max(x)"); + assert_eq!(&process_func_str("cos(sin(x))".to_string()), "cos(sin(x))"); + assert_eq!(&process_func_str("x^(1+2x)".to_string()), "x^(1+2*x)"); + assert_eq!( + &process_func_str("(x+2)x(1+3)".to_string()), + "(x+2)*x*(1+3)" + ); + assert_eq!(&process_func_str("(x+2)(1+3)".to_string()), "(x+2)*(1+3)"); + assert_eq!(&process_func_str("xxx".to_string()), "x*x*x"); + assert_eq!(&process_func_str("eee".to_string()), "e*e*e"); + assert_eq!(&process_func_str("pi(x+2)".to_string()), "π*(x+2)"); + assert_eq!(&process_func_str("(x)pi".to_string()), "(x)*π"); + assert_eq!(&process_func_str("2e".to_string()), "2*e"); + assert_eq!(&process_func_str("2log10(x)".to_string()), "2*log(x)"); + assert_eq!(&process_func_str("2log(x)".to_string()), "2*log(x)"); + assert_eq!(&process_func_str("x!".to_string()), "x!"); + assert_eq!(&process_func_str("pipipipipipi".to_string()), "π*π*π*π*π*π"); + assert_eq!(&process_func_str("10pi".to_string()), "10*π"); + assert_eq!(&process_func_str("pi10".to_string()), "π*10"); // Need to fix these checks, maybe I need to rewrite the whole asterisk adding system... (or just implement these changes into meval-rs, idk) // assert_eq!(&add_asterisks("emax(x)".to_string()), "e*max(x)");