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;
}
use std::path::Path;
use std::{fs, path::Path};
use num_cpus 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();
engine.start_compilation(Path::new(input));
let result = engine.wait_for_result().unwrap();
for instruction in result {
if let Err(e) = std::fs::write(output, instruction.encode().to_be_bytes()) {
eprintln!("Failed to write to output file: {e}");
std::process::exit(1);
}
let buffer: Vec<u8> = result
.iter()
.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(())
}
+2 -15
View File
@@ -3,6 +3,7 @@ use num_cpus as _;
use threadpool as _;
use assembler::{
assemble_file,
prelude::*,
tooling::{brainf, project},
};
@@ -46,19 +47,5 @@ fn main() {
let input_path = &args[2];
let output_path = &args[4];
let src = PathBuf::from(input_path);
// 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);
}
}
assemble_file(input_path, output_path).unwrap();
}
+21 -44
View File
@@ -4,6 +4,10 @@ use std::{
path::{Path, PathBuf},
};
use crate::templates::{Dsa, Dsc, Template};
mod templates;
/// Run a command and exit on failure.
fn run(cmd: &mut 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.
let mut name_opt = None;
for i in 0..args.len() {
@@ -64,57 +70,16 @@ fn cmd_new(args: &[String]) {
"dsa" => {
// Minimal DSA binary template.
let path = src_path.join(format!("main.dsa"));
let template = format!(
r#"
// GENERATED BY DSX-BUILD
// Generated at: {timestamp}
// Project name: {project_name}
// Imports
include print: "./lib/io/print.dsa"
let template = Dsa::create(&project_name, lib);
// 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");
}
"dsc" => {
let path = src_path.join(format!("main.dsc"));
let template = r#"
include print: "./lib/io/print.dsa";
fn main() -> u32 {
return 0;
}"#;
let template = Dsc::create(&project_name, lib);
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!(
"Created new {} project in {}.",
lang,