From 2bc7a5c13f6ab2b3ee28f772f31eb3414fa2b25b Mon Sep 17 00:00:00 2001 From: mlokr Date: Fri, 13 Sep 2024 20:31:05 +0200 Subject: [PATCH] bratenburg --- hblang/README.md | 22 +++++++++++++++++++ hblang/src/codegen.rs | 3 ++- hblang/src/lib.rs | 7 ++++-- .../tests/codegen_tests_something_somehow.txt | 0 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 hblang/tests/codegen_tests_something_somehow.txt diff --git a/hblang/README.md b/hblang/README.md index 1e467b7..389c445 100644 --- a/hblang/README.md +++ b/hblang/README.md @@ -913,3 +913,25 @@ main := fn(): int { return back_buffer[1024 * 2] } ``` + +#### something_somehow +```hb +foo := @use("foo.hb") +main := fn(): void { + foo.blue + foo.red + return +} + +// in module: foo.hb + +bar := @use("bar.hb") +default := bar +blue := default.blue +red := default.red + +// in module: bar.hb +Color := struct {r: u8, g: u8, b: u8, a: u8} +red := Color.(255, 0, 0, 0) +blue := Color.(1, 0, 1, 0) +``` diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index ac829b0..2446ccf 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -2547,7 +2547,7 @@ impl Codegen { pub fn disasm(&mut self, output: &mut impl std::io::Write) -> std::io::Result<()> { let mut bin = Vec::new(); - self.tys.assemble(&mut bin); + self.assemble(&mut bin); self.tys.disasm(&bin, &self.files, output, |_| {}) } @@ -2701,5 +2701,6 @@ mod tests { writing_into_string => README; request_page => README; tests_ptr_to_ptr_copy => README; + something_somehow => README; } } diff --git a/hblang/src/lib.rs b/hblang/src/lib.rs index cda350d..0b6997d 100644 --- a/hblang/src/lib.rs +++ b/hblang/src/lib.rs @@ -862,11 +862,11 @@ fn disasm( let mut label_count = 0; while let Some(&byte) = binary.first() { - let inst = instr_from_byte(byte)?; let offset: i32 = (prev.len() - binary.len()).try_into().unwrap(); if offset as u32 == off + len { break; } + let Ok(inst) = instr_from_byte(byte) else { break }; instrs::parse_args(binary, inst, &mut buf).ok_or(std::io::ErrorKind::OutOfMemory)?; for op in buf.drain(..) { @@ -903,11 +903,14 @@ fn disasm( binary.take(..off as usize).unwrap(); while let Some(&byte) = binary.first() { - let inst = instr_from_byte(byte).unwrap(); let offset: i32 = (prev.len() - binary.len()).try_into().unwrap(); if offset as u32 == off + len { break; } + let Ok(inst) = instr_from_byte(byte) else { + writeln!(out, "invalid instr {byte}")?; + break; + }; instrs::parse_args(binary, inst, &mut buf).unwrap(); if let Some(label) = labels.get(&offset.try_into().unwrap()) { diff --git a/hblang/tests/codegen_tests_something_somehow.txt b/hblang/tests/codegen_tests_something_somehow.txt new file mode 100644 index 0000000..e69de29