refactor & fixed assembler path handling
This commit is contained in:
+38
-11
@@ -1,7 +1,8 @@
|
||||
use assembler::prelude::*;
|
||||
use std::{fs, io::Write, path::PathBuf};
|
||||
|
||||
fn main() {
|
||||
// parse args:
|
||||
// Parse command line arguments
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
|
||||
if args.len() == 2 && args[1] == "init" {
|
||||
@@ -10,23 +11,49 @@ fn main() {
|
||||
}
|
||||
|
||||
if args.len() != 5 || args[1] != "-i" || args[3] != "-o" {
|
||||
eprintln!("Usage: binary_name -i input_path -o output_path");
|
||||
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);
|
||||
let mut output_file = fs::File::create(output_path).unwrap();
|
||||
|
||||
match assembler::assembler::assemble(&src) {
|
||||
Ok(res) => {
|
||||
res.iter().map(|i| i.encode()).for_each(|i| {
|
||||
output_file.write_all(&i.to_le_bytes()).unwrap();
|
||||
});
|
||||
}
|
||||
// Create the output file
|
||||
let mut output_file = match fs::File::create(output_path) {
|
||||
Ok(file) => file,
|
||||
Err(e) => {
|
||||
eprintln!("{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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user