use super::{ Address, Program }; impl Program { pub(crate) fn remove_definition_addresses(&mut self) { for instr in &mut self.instructions { for arg in &mut instr.args { if let Address::Definitions(name) = arg { *arg = self.definitions[name].clone(); } } } self.definitions.clear(); } pub(crate) fn analyze(&self) { for instr in &self.instructions { let result = instr.check_args(); if result.is_err() { panic!("Instruction '{}' error: {:?}", instr.name, result.err().unwrap()); } } } } #[cfg(test)] mod tests { use super::*; use crate::lexer::Token; #[test] fn remove_definition_addresses_removes_symbols() { let mut prog = Program::new(vec![ Token::SymbolDef("hello".to_string()), Token::Number(1000), Token::SymbolDef("world".to_string()), Token::Number(64), Token::Operator("jsr".to_string()), Token::Symbol("hello".to_string()), Token::Operator("mov".to_string()), Token::Address(1001), Token::Symbol("world".to_string()), Token::Operator("subleq".to_string()), Token::Symbol("hello".to_string()), Token::Address(1002), Token::Symbol("world".to_string()), ], 10); prog.remove_definition_addresses(); assert_eq!(prog.definitions.len(), 0); for i in prog.instructions { for arg in i.args { assert!(! matches!(arg, Address::Definitions(_))); } } } }