#![allow(dead_code)] mod code_gen; mod syntax; mod semantic; mod instruction; use std::collections::HashMap; use crate::Word; #[derive(Debug, Default)] pub(crate) struct Program { definitions: HashMap, constants: Vec, instructions: Vec, absolute_start: usize, } impl Program { pub(crate) fn print(&self, start_index: usize) { println!("-- Definitions --"); for (name, loc) in self.definitions.iter() { println!("{} := {:?}", name, loc); } println!("-- Constants --"); for (i, cnst) in self.constants.iter().enumerate() { println!("{: >8}: {} ", i + start_index, cnst); } println!("-- Instructions --"); for (i, instr) in self.instructions.iter().enumerate() { print!("{: >8}: {} ", i + start_index, instr.name); for arg in &instr.args { print!("{:?}, ", arg); } println!(""); } } } #[derive(Debug, Default, PartialEq, Eq, Clone)] pub(crate) enum Address { Memory(usize), Constants(usize), Instruction(usize), Definitions(String), InstructionOffset(usize, usize), // Internal InstructionConstant(usize), // Internal #[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
, }