holey-bytes/hbasm/src/main.rs

56 lines
1.8 KiB
Rust
Raw Normal View History

2023-07-12 00:16:23 +00:00
use std::io::Write;
use hbasm::Assembler;
2023-07-11 09:08:20 +00:00
use {
ariadne::{ColorGenerator, Label, Report, ReportKind, Source},
std::{
error::Error,
io::{stdin, Read},
},
};
fn main() -> Result<(), Box<dyn Error>> {
let mut code = String::new();
stdin().read_to_string(&mut code)?;
2023-07-12 00:16:23 +00:00
let mut assembler = Assembler::default();
if let Err(e) = hbasm::text::assemble(&mut assembler, &code) {
2023-07-11 09:08:20 +00:00
let mut colors = ColorGenerator::new();
let e_code = match e.kind {
hbasm::text::ErrorKind::UnexpectedToken => 1,
hbasm::text::ErrorKind::InvalidToken => 2,
hbasm::text::ErrorKind::UnexpectedEnd => 3,
hbasm::text::ErrorKind::InvalidSymbol => 4,
};
2023-07-11 09:24:49 +00:00
let message = match e.kind {
hbasm::text::ErrorKind::UnexpectedToken => "This token is not expected!",
hbasm::text::ErrorKind::InvalidToken => "The token is not valid!",
2023-07-12 00:16:23 +00:00
hbasm::text::ErrorKind::UnexpectedEnd => {
"The assembler reached the end of input unexpectedly!"
}
hbasm::text::ErrorKind::InvalidSymbol => {
"This referenced symbol doesn't have a corresponding label!"
}
2023-07-11 09:24:49 +00:00
};
2023-07-11 09:08:20 +00:00
let a = colors.next();
2023-07-11 09:24:49 +00:00
Report::build(ReportKind::Error, "engine_internal", e.span.clone().start)
2023-07-12 00:16:23 +00:00
.with_code(e_code)
.with_message(format!("{:?}", e.kind))
.with_label(
2023-07-12 00:24:05 +00:00
Label::new(("engine_internal", e.span))
2023-07-12 00:16:23 +00:00
.with_message(message)
.with_color(a),
)
.finish()
.eprint(("engine_internal", Source::from(&code)))
.unwrap();
} else {
std::io::stdout().lock().write_all(&assembler.buf).unwrap();
}
2023-07-11 09:08:20 +00:00
Ok(())
}