hotfix: branching and labelling branches

This commit is contained in:
Muhammad Nauman Raza 2025-01-29 19:37:35 +00:00
parent 3a369b97c9
commit 92dfd4341c
Signed by: devraza
GPG key ID: 91EAD6081011574B

View file

@ -100,11 +100,14 @@ fn process(
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
let file = File::open("code.asm")?; let file = File::open("code.asm")?;
let reader = BufReader::new(file); let reader = BufReader::new(file);
let mut accumulator: i64 = 0; let mut accumulator: i64 = 0;
let mut memory: HashMap<String, i64> = HashMap::new(); let mut memory: HashMap<String, i64> = HashMap::new();
let mut labels: HashMap<String, usize> = HashMap::new(); let mut labels: HashMap<String, usize> = HashMap::new();
let mut code: Vec<String> = Vec::new(); let mut code: Vec<String> = Vec::new();
let valid: [&str; 3] = ["BRA", "BRZ", "BRP"];
for (index, line) in reader.lines().enumerate() { for (index, line) in reader.lines().enumerate() {
let line = line?; let line = line?;
if line.trim().is_empty() { if line.trim().is_empty() {
@ -113,8 +116,8 @@ fn main() -> io::Result<()> {
let ops: Vec<String> = line.split_whitespace().map(String::from).collect(); let ops: Vec<String> = line.split_whitespace().map(String::from).collect();
if ops.len() > 1 && ops[1] == "DAT" { if ops.len() > 1 && ops[1] == "DAT" {
labels.insert(ops[0].clone(), ops[2].clone().parse::<usize>().unwrap()); labels.insert(ops[0].clone(), ops[2].clone().parse::<usize>().unwrap());
} else if ops.len() > 1 { } else if ops.len() > 1 && !valid.contains(&&*ops[0].clone()) {
labels.insert(ops[1].clone(), index); labels.insert(ops[0].clone(), index);
} }
code.push(line); code.push(line);
} }