You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
988 B
42 lines
988 B
use clap::Parser;
|
|
use std::io::Write;
|
|
use std::{
|
|
error::Error,
|
|
fs::{File, OpenOptions},
|
|
io::Read,
|
|
path::PathBuf,
|
|
};
|
|
use toy_cpu_4bit::assembler::Assembler;
|
|
|
|
/// ToyCPU-4bit Assembler
|
|
#[derive(Parser)]
|
|
#[clap(version, about, long_about = None)]
|
|
struct Args {
|
|
/// Path to input source code
|
|
input: PathBuf,
|
|
/// Path to output machine code
|
|
output: PathBuf,
|
|
}
|
|
|
|
fn read_code(path: PathBuf) -> Result<String, Box<dyn Error>> {
|
|
let mut code = String::new();
|
|
File::open(path)?.read_to_string(&mut code)?;
|
|
Ok(code)
|
|
}
|
|
|
|
fn write_code(path: PathBuf, code: [u8; 256]) -> Result<(), Box<dyn Error>> {
|
|
let mut file = OpenOptions::new()
|
|
.create(true)
|
|
.write(true)
|
|
.truncate(true)
|
|
.open(path)?;
|
|
file.write_all(&code)?;
|
|
Ok(())
|
|
}
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
let args = Args::parse();
|
|
let code = Assembler::new().assemble(&read_code(args.input)?)?;
|
|
write_code(args.output, code)?;
|
|
Ok(())
|
|
}
|
|
|