assembler changes & brainf##k compiler lmao

This commit is contained in:
2025-06-22 03:51:16 +01:00
parent 528ceddade
commit 9c56258c48
10 changed files with 731 additions and 154 deletions
+31 -34
View File
@@ -1,4 +1,4 @@
use assembler::prelude::*;
use assembler::{brainf, prelude::*};
use std::{fs, io::Write, path::PathBuf};
fn main() {
@@ -6,7 +6,29 @@ fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() == 2 && args[1] == "init" {
assembler::tooling::project::tool_libcreate();
project::tool_libcreate();
std::process::exit(0);
}
if args.len() == 2 && args[1] == "brainf" {
let src = PathBuf::from("brainf.bf");
let result = brainf::build(&src);
let mut file = match fs::File::create("brainf.dsb") {
Err(e) => {
eprintln!("Failed to create output file: {}", e);
std::process::exit(1);
}
Ok(file) => file,
};
for instruction in result {
if let Err(e) = file.write(&instruction.encode().to_be_bytes()) {
eprintln!("Failed to write to output file: {}", e);
std::process::exit(1);
}
}
std::process::exit(0);
}
@@ -19,41 +41,16 @@ fn main() {
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();
let mut compiler = CompilerEngine::new();
compiler.start_compilation(&src);
// Assemble the source file
if let Err(e) = engine.assemble(&src) {
eprintln!("Assembly error: {}", e);
std::process::exit(1);
}
// Or block until done
let result = compiler.wait_for_result().unwrap();
// 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");
for instruction in result {
if let Err(e) = fs::write(output_path, instruction.encode().to_be_bytes()) {
eprintln!("Failed to write to output file: {}", e);
std::process::exit(1);
}
}