fix: constant implicit mult
before, "pi 2" caused a panic while "2 pi" worked just fine. now both directions work :)
This commit is contained in:
parent
475c30b1a9
commit
58b3d876e4
2 changed files with 13 additions and 1 deletions
|
|
@ -126,6 +126,12 @@ impl FromStr for Nodes {
|
||||||
&& !(c == 'e' && filtered_string.chars().nth(i + 1).is_some_and(|c| c == '-'))
|
&& !(c == 'e' && filtered_string.chars().nth(i + 1).is_some_and(|c| c == '-'))
|
||||||
&& !(c == '-' && filtered_string.chars().nth(i - 1).is_some_and(|c| c == 'e'))
|
&& !(c == '-' && filtered_string.chars().nth(i - 1).is_some_and(|c| c == 'e'))
|
||||||
{
|
{
|
||||||
|
// likely a constant before this number
|
||||||
|
// no need to check for parenthesis here though (why?)
|
||||||
|
if matches!(nodes.last(), Some(Node::Number(_))) {
|
||||||
|
nodes.push(Node::Operator(Default::default()));
|
||||||
|
}
|
||||||
|
|
||||||
nodes.push(Node::from(
|
nodes.push(Node::from(
|
||||||
Float::parse(&filtered_string[start_index..i]).map_err(|e| {
|
Float::parse(&filtered_string[start_index..i]).map_err(|e| {
|
||||||
Error::NumberParsing(filtered_string[start_index..i].to_owned(), e)
|
Error::NumberParsing(filtered_string[start_index..i].to_owned(), e)
|
||||||
|
|
@ -180,7 +186,7 @@ impl FromStr for Nodes {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
ParsingState::ReadingNumber(start_index) => {
|
ParsingState::ReadingNumber(start_index) => {
|
||||||
if matches!(nodes.last(), Some(Node::ParenthesisEnd)) {
|
if matches!(nodes.last(), Some(Node::ParenthesisEnd | Node::Number(_))) {
|
||||||
nodes.push(Node::Operator(Default::default()));
|
nodes.push(Node::Operator(Default::default()));
|
||||||
}
|
}
|
||||||
nodes.push(Node::from(
|
nodes.push(Node::from(
|
||||||
|
|
|
||||||
|
|
@ -38,3 +38,9 @@ fn unary_operators() {
|
||||||
assert_eq!(eval("-10"), -10.);
|
assert_eq!(eval("-10"), -10.);
|
||||||
assert_eq!(eval("+10"), 10.);
|
assert_eq!(eval("+10"), 10.);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn constant_implicit_mult() {
|
||||||
|
assert_eq!(eval("2pi"), 6.283185307179586);
|
||||||
|
assert_eq!(eval("pi2"), 6.283185307179586);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue