]> Witch of Git - jade-rose/blob - toolchain/src/bin/assembler.rs
Add a very basic assembler utility
[jade-rose] / toolchain / src / bin / assembler.rs
1 use std::io::{self, BufRead};
2 use toolchain::inst::{encode::Encode, parse::parse_inst};
3
4 macro_rules! other_err {
5 ($fmt:expr $(, $arg:expr)* $(,)?) => {
6 io::Error::new(io::ErrorKind::Other, format!($fmt, $($arg),*))
7 }
8 }
9
10 fn main() -> io::Result<()> {
11 let mut cursor = io::Cursor::new(Vec::new());
12 for (i, line) in io::stdin().lock().lines().enumerate() {
13 let line = line.map_err(|_| other_err!("Error reading line {}", i + 1))?;
14 let line = line.trim();
15 if line.is_empty() {
16 continue;
17 }
18 let inst = parse_inst(&line)
19 .ok_or_else(|| other_err!("Error parsing instruction on line {}", i + 1))?;
20 inst.encode(&mut cursor)?;
21 }
22 for line in cursor.into_inner().chunks(40) {
23 for byte in line {
24 print!("{:02x}", byte);
25 }
26 println!();
27 }
28 Ok(())
29 }