From 6c61e31dde4b01ac6ff9c3db9423e76ff7ba42cf Mon Sep 17 00:00:00 2001 From: bgkillas Date: Wed, 21 Jun 2023 06:31:14 -0400 Subject: [PATCH] fix crash caused by nesting vars into self, bump ver --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 16 ++++++++-------- src/parse.rs | 34 ++++++++++++++++++++++++++++------ src/tests.rs | 38 +++++++++++++++++++------------------- 5 files changed, 57 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aca00ba..cc7e1ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -53,7 +53,7 @@ dependencies = [ [[package]] name = "kalc" -version = "0.6.5" +version = "0.7.0" dependencies = [ "console", "gnuplot", diff --git a/Cargo.toml b/Cargo.toml index adcd3c1..9e75fe9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kalc" -version = "0.6.5" +version = "0.7.0" edition = "2021" [profile.release] diff --git a/src/main.rs b/src/main.rs index b47022c..a305967 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,7 +106,7 @@ fn main() input = args.first().unwrap().replace('_', &format!("({})", last)); args.remove(0); print_answer(&input, - match get_func(&input_var(&input.replace('π', "pi").replace('τ', "tau"), &vars), prec, print_options.deg) + match get_func(&input_var(&input.replace('π', "pi").replace('τ', "tau"), &vars, None), prec, print_options.deg) { Ok(f) => f, Err(()) => @@ -163,7 +163,7 @@ fn main() { if !print_options.real_time_output { - frac = print_concurrent(&input, &input_var(&input.replace('_', &format!("({})", last)), &vars), print_options, prec); + frac = print_concurrent(&input, &input_var(&input.replace('_', &format!("({})", last)), &vars, None), print_options, prec); } if frac { @@ -224,7 +224,7 @@ fn main() } else if print_options.real_time_output { - print_concurrent(&input, &input_var(&input.replace('_', &format!("({})", last)), &vars), print_options, prec) + print_concurrent(&input, &input_var(&input.replace('_', &format!("({})", last)), &vars, None), print_options, prec) } else { @@ -290,7 +290,7 @@ fn main() cursor = input.len(); if print_options.real_time_output { - frac = print_concurrent(&input, &input_var(&input.replace('_', &format!("({})", last)), &vars), print_options, prec); + frac = print_concurrent(&input, &input_var(&input.replace('_', &format!("({})", last)), &vars, None), print_options, prec); } print!("\x1B[2K\x1B[1G{}{}\x1b[0m", if print_options.prompt @@ -352,7 +352,7 @@ fn main() cursor = input.len(); if print_options.real_time_output { - frac = print_concurrent(&input, &input_var(&input.replace('_', &format!("({})", last)), &vars), print_options, prec); + frac = print_concurrent(&input, &input_var(&input.replace('_', &format!("({})", last)), &vars, None), print_options, prec); } print!("\x1B[2K\x1B[1G{}{}\x1b[0m", if print_options.prompt @@ -429,7 +429,7 @@ fn main() } if print_options.real_time_output { - frac = print_concurrent(&input, &input_var(&input.replace('_', &format!("({})", last)), &vars), print_options, prec); + frac = print_concurrent(&input, &input_var(&input.replace('_', &format!("({})", last)), &vars, None), print_options, prec); } else { @@ -628,7 +628,7 @@ fn main() } if r.is_empty() { - println!("{}", input_var(l, &vars)); + println!("{}", input_var(l, &vars, None)); continue; } match l @@ -840,7 +840,7 @@ fn main() { continue; } - funcs.push(match get_func(&input_var(i, &vars), prec, print_options.deg) + funcs.push(match get_func(&input_var(i, &vars, None), prec, print_options.deg) { Ok(f) => f, _ => continue 'main, diff --git a/src/parse.rs b/src/parse.rs index 5e636fb..21cd08c 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -446,7 +446,7 @@ fn place_multiplier(func:&mut Vec, find_word:&bool) func.push(Str('*'.to_string())) } } -pub fn input_var(input:&str, vars:&[[String; 2]]) -> String +pub fn input_var(input:&str, vars:&[[String; 2]], dont_do:Option<&str>) -> String { let mut chars = input.chars().collect::>(); let mut output = String::new(); @@ -535,9 +535,16 @@ pub fn input_var(input:&str, vars:&[[String; 2]]) -> String } if input[j..i + 1] == var[0] { + if let Some(n) = dont_do + { + if n == var[0] + { + return String::new(); + } + } not_pushed = false; output.push('('); - output.push_str(&input_var(&var[1], vars)); + output.push_str(&input_var(&var[1], vars, Some(&var[0]))); output.push(')'); } else if j == 0 || !chars[j - 1].is_ascii_alphabetic() @@ -561,6 +568,13 @@ pub fn input_var(input:&str, vars:&[[String; 2]]) -> String continue; } v = var[0].chars().collect::>(); + if let Some(n) = dont_do + { + if n == var[0] + { + return String::new(); + } + } if input.contains(',') && var[0].contains(',') && chars.len() > 4 { not_pushed = false; @@ -597,10 +611,11 @@ pub fn input_var(input:&str, vars:&[[String; 2]]) -> String start = end + 1; } split.push(&temp[start..]); - value = input_var(&var[1], vars).clone(); + value = input_var(&var[1], vars, Some(&var[0])).clone(); for i in 0..split.len() { - value = value.replace(v[v.len() - 2 * (i as i32 - split.len() as i32).unsigned_abs() as usize], &format!("({})", input_var(split[i], vars))); + value = value.replace(v[v.len() - 2 * (i as i32 - split.len() as i32).unsigned_abs() as usize], + &format!("({})", input_var(split[i], vars, Some(&var[0])))); } output.push_str(&value); output.push(')'); @@ -615,7 +630,7 @@ pub fn input_var(input:&str, vars:&[[String; 2]]) -> String { temp = &temp[..temp.len() - 1]; } - output.push_str(&input_var(&var[1], vars).replace(v[v.len() - 2], &format!("({})", input_var(temp, vars)))); + output.push_str(&input_var(&var[1], vars, Some(&var[0])).replace(v[v.len() - 2], &format!("({})", input_var(temp, vars, Some(&var[0]))))); output.push(')'); } } @@ -629,10 +644,17 @@ pub fn input_var(input:&str, vars:&[[String; 2]]) -> String && (j == 0 || !chars[j - 1].is_ascii_alphabetic()) && (var[0].len() - 1 + i == chars.len() - 1 || !chars[i + 1 + var[0].len() - 1].is_ascii_alphabetic()) { + if let Some(n) = dont_do + { + if n == var[0] + { + return String::new(); + } + } i += var[0].len() - 1; not_pushed = false; output.push('('); - output.push_str(&input_var(&var[1], vars)); + output.push_str(&input_var(&var[1], vars, Some(&var[0]))); output.push(')'); } } diff --git a/src/tests.rs b/src/tests.rs index 184531b..0bafd68 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -6,55 +6,55 @@ use crate::math::do_math; #[test] fn test_math() { - let input = input_var("pi+tau*e/2i^(sqrt(2))/3*3-log(2-2i,-3+i)+sqrt(2)^(sqrt(2))", &get_vars(256)); - let output = get_func(&input, 256, false).unwrap(); - let expected = vec![Num(Complex::with_val(256, Pi)), + let input = input_var("pi+tau*e/2i^(sqrt(2))/3*3-log(2-2i,-3+i)+sqrt(2)^(sqrt(2))", &get_vars(512), None); + let output = get_func(&input, 512, false).unwrap(); + let expected = vec![Num(Complex::with_val(512, Pi)), Str("+".to_string()), - Num(2 * Complex::with_val(256, Pi)), + Num(2 * Complex::with_val(512, Pi)), Str("*".to_string()), - Num(Complex::with_val(256, 1).exp()), + Num(Complex::with_val(512, 1).exp()), Str("/".to_string()), - Num(Complex::with_val(256, 2.0)), + Num(Complex::with_val(512, 2.0)), Str("*".to_string()), - Num(Complex::with_val(256, (0.0, 1.0))), + Num(Complex::with_val(512, (0.0, 1.0))), Str("^".to_string()), Str("(".to_string()), Str("sqrt".to_string()), Str("(".to_string()), - Num(Complex::with_val(256, 2.0)), + Num(Complex::with_val(512, 2.0)), Str(")".to_string()), Str(")".to_string()), Str("/".to_string()), - Num(Complex::with_val(256, 3.0)), + Num(Complex::with_val(512, 3.0)), Str("*".to_string()), - Num(Complex::with_val(256, 3.0)), + Num(Complex::with_val(512, 3.0)), Str("-".to_string()), Str("log".to_string()), Str("(".to_string()), - Num(Complex::with_val(256, 2.0)), + Num(Complex::with_val(512, 2.0)), Str("-".to_string()), - Num(Complex::with_val(256, 2.0)), + Num(Complex::with_val(512, 2.0)), Str("*".to_string()), - Num(Complex::with_val(256, (0.0, 1.0))), + Num(Complex::with_val(512, (0.0, 1.0))), Str(",".to_string()), - Num(Complex::with_val(256, -3.0)), + Num(Complex::with_val(512, -3.0)), Str("+".to_string()), - Num(Complex::with_val(256, (0.0, 1.0))), + Num(Complex::with_val(512, (0.0, 1.0))), Str(")".to_string()), Str("+".to_string()), Str("sqrt".to_string()), Str("(".to_string()), - Num(Complex::with_val(256, 2.0)), + Num(Complex::with_val(512, 2.0)), Str(")".to_string()), Str("^".to_string()), Str("(".to_string()), Str("sqrt".to_string()), Str("(".to_string()), - Num(Complex::with_val(256, 2.0)), + Num(Complex::with_val(512, 2.0)), Str(")".to_string()), Str(")".to_string())]; - let out = do_math(output, false, 256).unwrap().num().unwrap(); - let answer = do_math(expected, false, 256).unwrap().num().unwrap(); + let out = do_math(output, false, 512).unwrap().num().unwrap(); + let answer = do_math(expected, false, 512).unwrap().num().unwrap(); assert_eq!(out.real().to_string(), answer.real().to_string()); assert_eq!(out.imag().to_string(), answer.imag().to_string()); assert_eq!(&out.real().to_string()[..20], "2.009877988310399125");