From 67470aea034fd46f4bbcfe815c51ad3451043188 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Thu, 27 Feb 2025 14:53:21 +1300 Subject: Finish first working version of Torque This is a huge and messy commit, worked on piecemeal while traveling and while the language was still being designed. --- src/tokens/bytecode.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/tokens/bytecode.rs (limited to 'src/tokens/bytecode.rs') 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, + pub errors: Vec, +} + +#[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, +} -- cgit v1.2.3-70-g09d2