summaryrefslogtreecommitdiff
path: root/src/tokens/bytecode.rs
blob: 9ac340e123e875c209f71b03d3a5e3abcc3bced4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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,
}