summaryrefslogtreecommitdiff
path: root/src/stages/bytecode_tokens.rs
blob: 50208276fad81b9701450c709949e7f46b09dcaf (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
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);
}