main.rs: Basic TUI experience: line numbers, better errors

This commit is contained in:
2023-09-15 18:21:01 -05:00
parent 417ef03e41
commit d73c5b2e5d
3 changed files with 148 additions and 62 deletions

View File

@@ -1,61 +0,0 @@
//! Simple frontend for the assembler
use msp430_asm::preamble::*;
use std::error::Error;
use std::io::Read;
fn main() -> Result<(), Box<dyn Error>> {
let mut repl = true;
for arg in std::env::args() {
match arg.as_str() {
"-" | "-f" | "--file" => repl = false,
_ => (),
}
}
let mut buf = String::new();
if repl {
let mut line = String::new();
while let Ok(len) = std::io::stdin().read_line(&mut line) {
match len {
0 => break, // No newline (reached EOF)
1 => continue, // Line is empty
_ => {
// Try to parse this line in isolation (this restricts preprocessing)
match Parser::default().parse(&line) {
Ok(_) => {
buf += &line;
}
Err(error) => println!("{error}"),
}
line.clear();
}
}
}
match Assembler::assemble(&Parser::default().parse(&buf)?) {
Err(error) => println!("{error}"),
Ok(out) => {
for word in out {
print!("{:04x} ", word.swap_bytes())
}
}
}
println!();
} else {
std::io::stdin().lock().read_to_string(&mut buf)?;
let tree = Parser::default().parse(&buf);
match &tree {
Ok(tree) => {
//msp430_asm::linker::Printer::default().visit_root(tree);
for insn in msp430_asm::assembler::Assembler::assemble(tree)? {
print!("{:04x} ", insn.swap_bytes())
}
println!();
}
Err(error) => eprintln!("{error}"),
}
}
Ok(())
}