Skip to content

Commit

Permalink
fixed bug with multiple operators
Browse files Browse the repository at this point in the history
  • Loading branch information
fenneltheloon committed Dec 9, 2024
1 parent 2c099b6 commit a5176b4
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,15 @@ fn evaluate(mut a: Vec<Token>) -> Result<Vec<Token>, &'static str> {
// If multiplication
// If division
let mut i = 0usize;
let mut modified: bool = false;
'a: loop {
if i >= a.len() {
break 'a;
if modified {
modified = false;
i = 0;
} else {
break 'a;
}
}
let token = &a[i];
match token {
Expand All @@ -258,12 +264,14 @@ fn evaluate(mut a: Vec<Token>) -> Result<Vec<Token>, &'static str> {
a.remove(i);
let lo = a.remove(i - 1).constant();
a.insert(i - 1, Token::Constant(lo * ro));
modified = true;
}
Token::Divide => {
let ro = a.remove(i + 1).constant();
a.remove(i);
let lo = a.remove(i - 1).constant();
a.insert(i - 1, Token::Constant(lo / ro));
modified = true;
}
_ => {}
}
Expand All @@ -272,31 +280,40 @@ fn evaluate(mut a: Vec<Token>) -> Result<Vec<Token>, &'static str> {
// If addition
// If subtraction
let mut i = 0usize;
let mut modified: bool = false;
'a: loop {
if i >= a.len() {
if a.len() == 1 {
break 'a;
}
if i >= a.len() {
if modified {
modified = false;
} else {
return Err("Expression did not simplify");
}
i = 0;
}
let token = &a[i];
match token {
Token::Add => {
let ro = a.remove(i + 1).constant();
a.remove(i);
let lo = a.remove(i - 1).constant();
a.insert(i - 1, Token::Constant(lo + ro));
modified = true;
}
Token::Subtract => {
let ro = a.remove(i + 1).constant();
a.remove(i);
let lo = a.remove(i - 1).constant();
a.insert(i - 1, Token::Constant(lo - ro));
modified = true;
}
_ => {}
}
i += 1;
}
if a.len() != 1 {
return Err("Expression did not simplify");
}
if a.len() != 1 {}
Ok(a)
}

Expand Down

0 comments on commit a5176b4

Please sign in to comment.