diff options
Diffstat (limited to 'src/stages')
-rw-r--r-- | src/stages/bytecode.rs | 47 | ||||
-rw-r--r-- | src/stages/bytecode_tokens.rs | 42 | ||||
-rw-r--r-- | src/stages/intermediate.rs | 2 | ||||
-rw-r--r-- | src/stages/intermediate_tokens.rs | 24 | ||||
-rw-r--r-- | src/stages/mod.rs | 4 |
5 files changed, 116 insertions, 3 deletions
diff --git a/src/stages/bytecode.rs b/src/stages/bytecode.rs new file mode 100644 index 0000000..2d73767 --- /dev/null +++ b/src/stages/bytecode.rs @@ -0,0 +1,47 @@ +use crate::*; + + +pub fn parse_bytecode(intermediate: Vec<Tracked<IntermediateToken>>, width: Option<u32>) -> Result<Vec<Segment>, Vec<Tracked<BytecodeError>>> { + let mut segments = Vec::new(); + let mut errors = Vec::new(); + let mut current_segment = Vec::new(); + let mut segment_source = None; + let mut segment_address = 0; + + + for token in intermediate { + match token.value { + IntermediateToken::Word(word) => { + if let Some(width) = width { + if word.width != width { + let error = BytecodeError::IncorrectWidth(width, word.width); + errors.push(Tracked::from(error, token.source.clone())); + } + } + let source = token.source.clone(); + current_segment.push(Tracked::from(word, source)); + } + IntermediateToken::PinnedAddress(pinned_address) => { + if !current_segment.is_empty() { + let address = segment_address; + let words = std::mem::take(&mut current_segment); + let source = std::mem::take(&mut segment_source); + segments.push(Segment { address, source, words }); + segment_address = pinned_address; + } + } + } + } + // Finish final segment. + if !current_segment.is_empty() { + let address = segment_address; + let words = std::mem::take(&mut current_segment); + let source = std::mem::take(&mut segment_source); + segments.push(Segment { address, source, words }); + } + + match errors.is_empty() { + true => Ok(segments), + false => Err(errors), + } +} diff --git a/src/stages/bytecode_tokens.rs b/src/stages/bytecode_tokens.rs new file mode 100644 index 0000000..5020827 --- /dev/null +++ b/src/stages/bytecode_tokens.rs @@ -0,0 +1,42 @@ +use crate::*; + +pub struct Segment { + pub address: usize, + /// Source of the address value. + pub source: Option<SourceSpan>, + pub words: Vec<Tracked<Word>>, +} + +pub fn print_segment(segment: &Segment) { + println!("SEGMENT: 0x{:>04x}", segment.address); + // Find maximum width of all words in the segment. + let width = segment.words.iter().map(|w| w.to_string().chars().count()).max().unwrap_or(0); + for word in &segment.words { + let string = word.to_string(); + println!(" {string:>w$}", w=width as usize); + } + +} + +pub enum BytecodeError { + // (expected, received) + IncorrectWidth(u32, u32) +} + +pub fn report_bytecode_errors(errors: &[Tracked<BytecodeError>], source_code: &str) { + for error in errors { + report_bytecode_error(error, source_code); + } +} + +fn report_bytecode_error(error: &Tracked<BytecodeError>, source_code: &str) { + let context = Context { source_code: &source_code, source: &error.source }; + let message = match &error.value { + BytecodeError::IncorrectWidth(expected, received) => + &format!("Word is {received} bits wide, but should be exactly {expected} bits wide"), + }; + + report_source_issue(LogLevel::Error, &context, message); +} + + diff --git a/src/stages/intermediate.rs b/src/stages/intermediate.rs index 7523baf..07773a0 100644 --- a/src/stages/intermediate.rs +++ b/src/stages/intermediate.rs @@ -266,7 +266,7 @@ impl IntermediateParser { } } } - let word = IntermediateWord { width: word_width, value: word_value }; + let word = Word { width: word_width, value: word_value }; let token = IntermediateToken::Word(word); intermediate.push(Tracked::from(token, source.clone())); self.address += 1; diff --git a/src/stages/intermediate_tokens.rs b/src/stages/intermediate_tokens.rs index 71dbd62..2fb29fa 100644 --- a/src/stages/intermediate_tokens.rs +++ b/src/stages/intermediate_tokens.rs @@ -3,17 +3,37 @@ use crate::*; #[derive(Clone)] pub enum IntermediateToken { - Word(IntermediateWord), + Word(Word), PinnedAddress(usize), } #[derive(Clone)] -pub struct IntermediateWord { +pub struct Word { pub value: usize, /// Width of the word in bits. pub width: u32, } +impl std::fmt::Display for Word { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + if self.width == 0 { + write!(f, "0") + } else { + for i in (0..self.width).rev() { + let is_first_bit = i+1 == self.width; + if !is_first_bit && (i+1) % 4 == 0 { + write!(f, "_")?; + } + match (self.value >> i) & 1 { + 0 => write!(f, "0")?, + _ => write!(f, "1")?, + } + } + Ok(()) + } + } +} + #[derive(Clone)] pub enum IntermediateValue { Integer(IntermediateInteger), diff --git a/src/stages/mod.rs b/src/stages/mod.rs index bf0d5a6..571fd65 100644 --- a/src/stages/mod.rs +++ b/src/stages/mod.rs @@ -5,6 +5,8 @@ mod semantic; mod semantic_tokens; mod intermediate; mod intermediate_tokens; +mod bytecode; +mod bytecode_tokens; pub use compiler::*; pub use syntactic::*; @@ -13,6 +15,8 @@ pub use semantic::*; pub use semantic_tokens::*; pub use intermediate::*; pub use intermediate_tokens::*; +pub use bytecode::*; +pub use bytecode_tokens::*; #[macro_export] |