Skip to content

Commit

Permalink
fix crash caused by nesting vars into self, bump ver
Browse files Browse the repository at this point in the history
  • Loading branch information
bgkillas committed Jun 21, 2023
1 parent 182dfcd commit 6c61e31
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kalc"
version = "0.6.5"
version = "0.7.0"
edition = "2021"

[profile.release]
Expand Down
16 changes: 8 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(()) =>
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -628,7 +628,7 @@ fn main()
}
if r.is_empty()
{
println!("{}", input_var(l, &vars));
println!("{}", input_var(l, &vars, None));
continue;
}
match l
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 28 additions & 6 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ fn place_multiplier(func:&mut Vec<NumStr>, 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::<Vec<char>>();
let mut output = String::new();
Expand Down Expand Up @@ -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()
Expand All @@ -561,6 +568,13 @@ pub fn input_var(input:&str, vars:&[[String; 2]]) -> String
continue;
}
v = var[0].chars().collect::<Vec<char>>();
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;
Expand Down Expand Up @@ -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(')');
Expand All @@ -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(')');
}
}
Expand All @@ -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(')');
}
}
Expand Down
38 changes: 19 additions & 19 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 6c61e31

Please sign in to comment.