Skip to content

Commit

Permalink
tokenizing optimized & compute optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
BHznJNs committed Mar 6, 2023
1 parent 32e4332 commit 2bdf585
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 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 = "calculator"
version = "1.0.2"
version = "1.0.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
1 change: 1 addition & 0 deletions src/compiler/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub fn tokenizer(source: String) -> Result<TokenVec, ()> {
}

if is_num_minus {
is_num_minus = false;
value = Number::Int(0) - value;
}

Expand Down
22 changes: 13 additions & 9 deletions src/computer/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,23 @@ pub fn compute(
}
index += 1;
}
for symbol in symbol_stack {
let options_num2 = number_stack.pop();
let options_num1 = number_stack.pop();

if options_num2.is_none() || options_num1.is_none() {
// clear remain elements in number_stack and symbol_stack
while !symbol_stack.is_empty() {
if symbol_stack.len() < 1 {
println!("Computing symbol missing.");
return Err(())
}
if number_stack.len() < 2 {
println!("Computing number missing.");
return Err(())
}

number_stack.push(operate(
options_num1.unwrap(),
options_num2.unwrap(),
symbol
let symbol = symbol_stack.remove(0);
let num1 = number_stack.remove(0);
let num2 = number_stack.remove(0);

number_stack.insert(0, operate(
num1, num2, symbol,
));
}
return Ok(number_stack[0])
Expand Down

0 comments on commit 2bdf585

Please sign in to comment.