// SPDX-License-Identifier: MIT // Copyright Murad Karammaev, Nikita Kuzmin use clap::Parser; use std::{ error::Error, fs::{File, OpenOptions}, io::{Read, Write}, 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> { 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> { let mut file = OpenOptions::new() .create(true) .write(true) .truncate(true) .open(path)?; file.write_all(&code)?; Ok(()) } fn main() -> Result<(), Box> { let args = Args::parse(); let code = Assembler::new().assemble(&read_code(args.input)?)?; write_code(args.output, code)?; Ok(()) }