refactor+perf: revamp codebase

This commit is contained in:
Muhammad Nauman Raza 2025-01-29 19:54:15 +00:00
parent faeedb7850
commit f7fdda37f4
Signed by: devraza
GPG key ID: 91EAD6081011574B
2 changed files with 51 additions and 64 deletions

View file

@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::fs::File;
use std::io::{self, BufRead, BufReader};
fn retrieve(memory: &mut HashMap<String, i64>, ops: Vec<String>) -> i64 {
fn retrieve(memory: &HashMap<String, i64>, ops: &[String]) -> i64 {
if ops.len() >= 3 {
*memory.get(&ops[2]).unwrap_or(&0)
} else {
@ -10,16 +10,16 @@ fn retrieve(memory: &mut HashMap<String, i64>, ops: Vec<String>) -> i64 {
}
}
fn branch(labels: &HashMap<String, usize>, ops: Vec<String>) -> usize {
fn branch(labels: &HashMap<String, usize>, ops: &[String]) -> usize {
if ops.len() >= 3 {
return *labels.get(&ops[2]).unwrap();
*labels.get(&ops[2]).unwrap_or(&0)
} else {
return *labels.get(&ops[1]).unwrap();
*labels.get(&ops[1]).unwrap_or(&0)
}
}
fn process(
line: String,
line: &str,
accumulator: &mut i64,
memory: &mut HashMap<String, i64>,
labels: &HashMap<String, usize>,
@ -27,58 +27,46 @@ fn process(
) -> usize {
let ops: Vec<String> = line.split_whitespace().map(String::from).collect();
let options: Vec<&str> = if ops.len() >= 3 {
vec![ops[0].as_str(), ops[1].as_str()]
} else {
vec![ops[0].as_str()]
};
for op in options {
match op {
"INP" => {
match ops.get(0).map(String::as_str) {
Some("INP") => {
let mut s = String::new();
io::stdin().read_line(&mut s).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);
Some("OUT") => println!("{}", accumulator),
Some("STA") => {
let key = if ops.len() >= 3 { &ops[2] } else { &ops[1] };
memory.insert(key.clone(), *accumulator);
}
Some("LDA") => {
*accumulator = retrieve(memory, &ops);
}
"LDA" => {
*accumulator = retrieve(memory, ops.clone());
}
"ADD" => {
Some("ADD") => {
if let Ok(value) = ops[1].parse::<i64>() {
*accumulator += value;
} else {
*accumulator += retrieve(memory, ops.clone());
*accumulator += retrieve(memory, &ops);
}
}
"SUB" => {
Some("SUB") => {
if let Ok(value) = ops[1].parse::<i64>() {
*accumulator -= value;
} else {
*accumulator -= retrieve(memory, ops.clone());
*accumulator -= retrieve(memory, &ops);
}
}
"BRA" => {
return branch(labels, ops);
}
"BRP" => {
Some("BRA") => return branch(labels, &ops),
Some("BRP") => {
if *accumulator >= 0 {
return branch(labels, ops);
return branch(labels, &ops);
}
}
"BRZ" => {
Some("BRZ") => {
if *accumulator == 0 {
return branch(labels, ops);
return branch(labels, &ops);
}
}
"HLT" => std::process::exit(0),
Some("HLT") => std::process::exit(0),
_ => {
if ops.len() > 1 && ops[1] == "DAT" {
memory.insert(
@ -88,9 +76,8 @@ fn process(
}
}
}
}
return pc + 1;
pc + 1
}
fn main() -> io::Result<()> {
@ -111,8 +98,8 @@ fn main() -> io::Result<()> {
}
let ops: Vec<String> = line.split_whitespace().map(String::from).collect();
if ops.len() > 1 && ops[1] == "DAT" {
labels.insert(ops[0].clone(), ops[2].clone().parse::<usize>().unwrap());
} else if ops.len() > 1 && !valid.contains(&&*ops[0].clone()) {
labels.insert(ops[0].clone(), ops[2].parse::<usize>().unwrap_or(0));
} else if ops.len() > 1 && !valid.contains(&&*ops[0]) {
labels.insert(ops[0].clone(), index);
}
code.push(line);
@ -121,7 +108,7 @@ fn main() -> io::Result<()> {
let mut pc: usize = 1;
while pc < code.len() {
let line = &code[pc];
pc = process(line.to_string(), &mut accumulator, &mut memory, &labels, pc);
pc = process(line, &mut accumulator, &mut memory, &labels, pc);
}
Ok(())

BIN
tinypc Executable file

Binary file not shown.