Files
damn_simple_architecture/assembler/src/main.rs
T
2025-06-22 00:30:27 +01:00

61 lines
1.7 KiB
Rust

use assembler::prelude::*;
use std::{fs, io::Write, path::PathBuf};
fn main() {
// Parse command line arguments
let args: Vec<String> = std::env::args().collect();
if args.len() == 2 && args[1] == "init" {
assembler::tooling::project::tool_libcreate();
std::process::exit(0);
}
if args.len() != 5 || args[1] != "-i" || args[3] != "-o" {
eprintln!("Usage: {} -i input_path -o output_path", args[0]);
std::process::exit(1);
}
let input_path = &args[2];
let output_path = &args[4];
let src = PathBuf::from(input_path);
// Create the output file
let mut output_file = match fs::File::create(output_path) {
Ok(file) => file,
Err(e) => {
eprintln!("Failed to create output file: {e}");
std::process::exit(1);
}
};
// Initialize the compiler engine
let mut engine = CompilerEngine::new();
// Assemble the source file
if let Err(e) = engine.assemble(&src) {
eprintln!("Assembly error: {e}");
std::process::exit(1);
}
// Build and write the output
match engine.result() {
Some(Ok(instructions)) => {
for instruction in instructions {
if let Err(e) = output_file.write_all(&instruction.encode().to_le_bytes())
{
eprintln!("Failed to write to output file: {e}");
std::process::exit(1);
}
}
}
Some(Err(e)) => {
eprintln!("Build error: {e}");
std::process::exit(1);
}
None => {
eprintln!("Build error: No result available");
std::process::exit(1);
}
}
}