feat: better incorporation of variables defined by DAT

This commit is contained in:
Muhammad Nauman Raza 2025-01-23 12:03:07 +00:00
parent 921136e91e
commit 23f3f0b070
Signed by: devraza
GPG key ID: 91EAD6081011574B

View file

@ -34,16 +34,43 @@ fn main() -> Result<()> {
None => panic!(), None => panic!(),
} }
} }
"ADD" => accumulator += &ops[1].clone().parse::<i64>().unwrap(), "ADD" => {
"SUB" => accumulator -= &ops[1].clone().parse::<i64>().unwrap(), let addition = ops[1].clone().parse::<i64>();
if addition.is_ok() {
accumulator -= addition.unwrap();
} else {
match memory.get(&ops[1].clone()) {
Some(value) => accumulator += *value,
None => panic!(),
}
}
}
"SUB" => {
let subtract = ops[1].clone().parse::<i64>();
if subtract.is_ok() {
accumulator -= subtract.unwrap();
} else {
match memory.get(&ops[1].clone()) {
Some(value) => accumulator -= *value,
None => panic!(),
}
}
}
"HLT" => std::process::exit(0), "HLT" => std::process::exit(0),
&_ => { &_ => {
match ops[1].as_str() { match ops[1].as_str() {
"DAT" => { "DAT" => {
if ops.get(2).is_some() {
memory.insert( memory.insert(
ops[0].clone(), ops[0].clone(),
ops[2].clone().parse::<i64>().unwrap(), ops[2].clone().parse::<i64>().unwrap(),
); );
} else {
memory.insert(
ops[0].clone(),
0,
);
}
} }
&_ => { &_ => {
panic!() panic!()