mod code_gen; mod syntax; mod semantic; mod instruction; use std::{ collections::HashMap, fmt::{ Display, Formatter, Result }, }; use crate::Word; #[derive(Debug, Default)] pub(crate) struct Program { definitions: HashMap, constants: Vec, instructions: Vec, } impl Display for Program { fn fmt(&self, f: &mut Formatter) -> Result { writeln!(f, "-- Definitions --")?; for (name, loc) in self.definitions.iter() { writeln!(f, "{} := {:?}", name, loc)?; } writeln!(f, "-- Constants --")?; for (i, cnst) in self.constants.iter().enumerate() { writeln!(f, "{: >8}: {} ", i, cnst)?; } writeln!(f, "-- Instructions --")?; for (i, instr) in self.instructions.iter().enumerate() { write!(f, "{: >8}: {} ", i, instr.name)?; for arg in &instr.args { write!(f, "{:?}, ", arg)?; } writeln!(f, "")?; } Ok(()) } } #[derive(Debug, Default, PartialEq, Eq, Clone)] pub(crate) enum Address { Memory(usize), Constants(usize), Instruction(usize), Definitions(String), #[default] None, } impl Address { fn take(&mut self) -> Address { let val = self.clone(); *self = Address::None; val } fn is_memory(&self) -> bool { if let Address::Memory(_) = &self { true } else { false } } fn is_constant(&self) -> bool { if let Address::Constants(_) = &self { true } else { false } } fn is_instruction(&self) -> bool { if let Address::Instruction(_) = &self { true } else { false } } fn is_definition(&self) -> bool { if let Address::Definitions(_) = &self { true } else { false } } fn is_none(&self) -> bool { if let Address::None = &self { true } else { false } } fn is_location(&self) -> bool { self.is_memory() || self.is_instruction() } } #[derive(Debug, Default, Clone)] pub(crate) struct Instruction { name: String, args: Vec
, }