diff --git a/src/main.rs b/src/main.rs index bdb4a4b..5586a51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,16 +34,43 @@ fn main() -> Result<()> { None => panic!(), } } - "ADD" => accumulator += &ops[1].clone().parse::().unwrap(), - "SUB" => accumulator -= &ops[1].clone().parse::().unwrap(), + "ADD" => { + let addition = ops[1].clone().parse::(); + 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::(); + 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), &_ => { match ops[1].as_str() { "DAT" => { - memory.insert( - ops[0].clone(), - ops[2].clone().parse::().unwrap(), - ); + if ops.get(2).is_some() { + memory.insert( + ops[0].clone(), + ops[2].clone().parse::().unwrap(), + ); + } else { + memory.insert( + ops[0].clone(), + 0, + ); + } } &_ => { panic!()