cargo: fmt

This commit is contained in:
Muhammad Nauman Raza 2025-01-26 09:36:13 +00:00
parent a40eba6f8d
commit ec3f371313
Signed by: devraza
GPG key ID: 91EAD6081011574B

View file

@ -2,10 +2,7 @@ use std::collections::HashMap;
use std::fs::File; use std::fs::File;
use std::io::{self, BufRead, BufReader}; use std::io::{self, BufRead, BufReader};
fn retrieve( fn retrieve(memory: &mut HashMap<String, i64>, ops: Vec<String>) -> i64 {
memory: &mut HashMap<String, i64>,
ops: Vec<String>
) -> i64 {
if ops.len() >= 3 { if ops.len() >= 3 {
return *memory.get(&ops[2]).unwrap_or(&0); return *memory.get(&ops[2]).unwrap_or(&0);
} else { } else {
@ -30,55 +27,55 @@ fn process(
} }
for op in options { for op in options {
match op { match op {
"INP" => { "INP" => {
let mut s = String::new(); let mut s = String::new();
io::stdin().read_line(&mut s).unwrap(); io::stdin().read_line(&mut s).unwrap();
*accumulator = s.trim().parse::<i64>().unwrap(); *accumulator = s.trim().parse::<i64>().unwrap();
}
"OUT" => println!("{}", accumulator),
"STA" => {
if ops.len() >= 3 {
memory.insert(ops[2].clone(), *accumulator);
} else {
memory.insert(ops[1].clone(), *accumulator);
} }
} "OUT" => println!("{}", accumulator),
"LDA" => { "STA" => {
*accumulator = retrieve(memory, ops.clone()); if ops.len() >= 3 {
} memory.insert(ops[2].clone(), *accumulator);
"ADD" => { } else {
if let Ok(value) = ops[1].parse::<i64>() { memory.insert(ops[1].clone(), *accumulator);
*accumulator += value; }
} else {
*accumulator += retrieve(memory, ops.clone());
} }
} "LDA" => {
"SUB" => { *accumulator = retrieve(memory, ops.clone());
if let Ok(value) = ops[1].parse::<i64>() {
*accumulator -= value;
} else {
*accumulator -= retrieve(memory, ops.clone());
} }
} "ADD" => {
"BRA" => { if let Ok(value) = ops[1].parse::<i64>() {
if ops.len() >= 3 { *accumulator += value;
*pc = *labels.get(&ops[2]).unwrap(); } else {
} else { *accumulator += retrieve(memory, ops.clone());
*pc = *labels.get(&ops[1]).unwrap(); }
} }
} "SUB" => {
"HLT" => std::process::exit(0), if let Ok(value) = ops[1].parse::<i64>() {
_ => { *accumulator -= value;
if ops.len() > 1 && ops[1] == "DAT" { } else {
memory.insert( *accumulator -= retrieve(memory, ops.clone());
ops[0].clone(), }
ops.get(2).and_then(|s| s.parse::<i64>().ok()).unwrap_or(0), }
); "BRA" => {
if ops.len() >= 3 {
*pc = *labels.get(&ops[2]).unwrap();
} else {
*pc = *labels.get(&ops[1]).unwrap();
}
}
"HLT" => std::process::exit(0),
_ => {
if ops.len() > 1 && ops[1] == "DAT" {
memory.insert(
ops[0].clone(),
ops.get(2).and_then(|s| s.parse::<i64>().ok()).unwrap_or(0),
);
}
} }
} }
} }
}
return 0; return 0;
} }
@ -108,7 +105,13 @@ fn main() -> io::Result<()> {
let mut pc: usize = 0; let mut pc: usize = 0;
while pc < code.len() { while pc < code.len() {
let line = &code[pc]; let line = &code[pc];
let next_pc = process(line.to_string(), &mut accumulator, &mut memory, &labels, &mut pc); let next_pc = process(
line.to_string(),
&mut accumulator,
&mut memory,
&labels,
&mut pc,
);
if next_pc != 0 { if next_pc != 0 {
pc = next_pc; pc = next_pc;
} else { } else {