updated assembler to write to binary files correctly 🤦

This commit is contained in:
2026-02-06 15:15:10 +00:00
parent 1fcfb3120b
commit bbcef7178f
3 changed files with 34 additions and 65 deletions
+11 -6
View File
@@ -24,7 +24,7 @@ pub mod prelude {
pub use crate::tooling::project; pub use crate::tooling::project;
} }
use std::path::Path; use std::{fs, path::Path};
use num_cpus as _; use num_cpus as _;
use threadpool as _; use threadpool as _;
@@ -35,11 +35,16 @@ pub fn assemble_file(input: &str, output: &str) -> Result<(), std::io::Error> {
let mut engine = CompilerEngine::new(); let mut engine = CompilerEngine::new();
engine.start_compilation(Path::new(input)); engine.start_compilation(Path::new(input));
let result = engine.wait_for_result().unwrap(); let result = engine.wait_for_result().unwrap();
for instruction in result {
if let Err(e) = std::fs::write(output, instruction.encode().to_be_bytes()) { let buffer: Vec<u8> = result
eprintln!("Failed to write to output file: {e}"); .iter()
std::process::exit(1); .flat_map(|instruction| instruction.encode().to_be_bytes())
} .collect();
if let Err(e) = fs::write(output, buffer) {
eprintln!("Failed to write to output file: {e}");
std::process::exit(1);
} }
Ok(()) Ok(())
} }
+2 -15
View File
@@ -3,6 +3,7 @@ use num_cpus as _;
use threadpool as _; use threadpool as _;
use assembler::{ use assembler::{
assemble_file,
prelude::*, prelude::*,
tooling::{brainf, project}, tooling::{brainf, project},
}; };
@@ -46,19 +47,5 @@ fn main() {
let input_path = &args[2]; let input_path = &args[2];
let output_path = &args[4]; let output_path = &args[4];
let src = PathBuf::from(input_path); assemble_file(input_path, output_path).unwrap();
// Initialize the compiler engine
let mut compiler = CompilerEngine::new();
compiler.start_compilation(&src);
// Or block until done
let result = compiler.wait_for_result().unwrap();
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);
}
}
} }
+21 -44
View File
@@ -4,6 +4,10 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use crate::templates::{Dsa, Dsc, Template};
mod templates;
/// Run a command and exit on failure. /// Run a command and exit on failure.
fn run(cmd: &mut Command) { fn run(cmd: &mut Command) {
let status = cmd.status().expect("failed to execute command"); let status = cmd.status().expect("failed to execute command");
@@ -39,6 +43,8 @@ fn cmd_new(args: &[String]) {
} }
} }
let lib = args.contains(&"--lib".to_string());
// Determine project root: a subdirectory named after the supplied --name argument. // Determine project root: a subdirectory named after the supplied --name argument.
let mut name_opt = None; let mut name_opt = None;
for i in 0..args.len() { for i in 0..args.len() {
@@ -64,57 +70,16 @@ fn cmd_new(args: &[String]) {
"dsa" => { "dsa" => {
// Minimal DSA binary template. // Minimal DSA binary template.
let path = src_path.join(format!("main.dsa")); let path = src_path.join(format!("main.dsa"));
let template = format!(
r#"
// GENERATED BY DSX-BUILD
// Generated at: {timestamp}
// Project name: {project_name}
// Imports let template = Dsa::create(&project_name, lib);
include print: "./lib/io/print.dsa"
// Globals & Reserved Memory
dw stack: 0x10000
db message: "Process Exited with code:"
// Entry Point
_init:
ldw stack, bpr
mov bpr, spr
push zero
call main
call print::print_newline
lwi message, rg0
push rg0
call print::print
pop zero
call print::print_hex_word
pop zero
hlt
main:
push bpr
mov spr, bpr
lli 0, rg0
stw rg0, bpr, 8
mov bpr, spr
pop bpr
return"#,
project_name = project_name,
timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string()
);
fs::write(path, template).expect("Unable to write DSA file"); fs::write(path, template).expect("Unable to write DSA file");
} }
"dsc" => { "dsc" => {
let path = src_path.join(format!("main.dsc")); let path = src_path.join(format!("main.dsc"));
let template = r#"
include print: "./lib/io/print.dsa";
fn main() -> u32 { let template = Dsc::create(&project_name, lib);
return 0;
}"#;
fs::write(path, template).expect("Unable to write DSC file"); fs::write(path, template).expect("Unable to write DSC file");
} }
_ => { _ => {
@@ -123,6 +88,18 @@ fn main() -> u32 {
} }
} }
fs::create_dir_all(src_path.join("lib")).expect("Failed to create lib directory");
fs::write(
src_path.join("lib/print.dsa"),
templates::create_print_lib(),
)
.expect("Failed to create print.dsa");
fs::write(
src_path.join("lib/maths.dsa"),
templates::create_maths_lib(),
)
.expect("Failed to create maths.dsa");
println!( println!(
"Created new {} project in {}.", "Created new {} project in {}.",
lang, lang,