diff options
Diffstat (limited to 'src/tokens/bytecode.rs')
-rw-r--r-- | src/tokens/bytecode.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/tokens/bytecode.rs b/src/tokens/bytecode.rs new file mode 100644 index 0000000..9ac340e --- /dev/null +++ b/src/tokens/bytecode.rs @@ -0,0 +1,49 @@ +use crate::*; + + +pub struct Bytecode { + pub words: Vec<Word>, + pub errors: Vec<BytecodeError>, +} + +#[derive(Clone, Copy)] +pub struct Word { + pub bits: usize, + pub value: usize, +} + +impl std::fmt::Display for Word { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + for i in (0..self.bits).rev() { + let is_first_bit = i+1 == self.bits; + if !is_first_bit && (i+1) % 4 == 0 { + write!(f, "_")?; + } + match (self.value >> i) & 1 { + 0 => write!(f, "0")?, + _ => write!(f, "1")?, + } + } + if self.bits == 0 { + write!(f, "0")?; + } + return Ok(()); + } +} + +pub struct BytecodeError { + pub source: SourceSpan, + pub variant: BytecodeErrorVariant, +} + +pub enum BytecodeErrorVariant { + DefinitionNotFound(String), + DuplicateLabelDefinition(String), + /// pin, real + PinnedAddressBacktrack(usize, usize), + /// expected, received + ValueTooLarge(usize, usize), + StackUnderflow, + MultipleReturnValues, + NoReturnValue, +} |