summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: d9683a3f7ff4ad6a676b9af1668ba797d509636e (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
use std::io::{Read, Write};
use bedrock_asm::*;

fn main() {
    // Read source code from standard input
    let mut source_code = String::new();
    if let Err(err) = std::io::stdin().read_to_string(&mut source_code) {
        eprintln!("Could not read from standard input, quitting.");
        eprintln!("({err:?})");
        std::process::exit(1);
    };

    let (bytecode, tokens) = assemble(&source_code);
    let mut is_error = false;
    for token in &tokens {
        if token.print_error(&source_code) { is_error = true };
    }
    if !is_error {
        for token in &tokens {
            if let SemanticTokenType::LabelDefinition(def) = &token.r#type {
                if def.references.is_empty() {
                    eprintln!("Unused label definition: {}", def.name);
                }
            }
        }
        eprintln!();
    }

    eprintln!("Assembled program in {} bytes.", bytecode.len());

    if is_error {
        std::process::exit(1)
    }

    // Write bytecode to standard output
    if let Err(_) = std::io::stdout().write_all(&bytecode) {
        eprintln!("Could not write to standard output, quitting.");
        std::process::exit(1);
    }
}