From 6bdb82b64343091a485d57a458541cb633d38f0f Mon Sep 17 00:00:00 2001 From: Sander in 't Veld Date: Sun, 14 Jan 2024 14:39:05 +0100 Subject: [PATCH] Thinking about how to format slices. It requires writing LLVM IR functions or functors in a way that is hard to do with my current llvm-sys generator.rs setup. --- penne.todo | 1 + src/generator.rs | 12 ++++++++++++ tests/execution.rs | 10 ++++++++++ tests/samples/valid/builtin_format_slice.pn | 17 +++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 tests/samples/valid/builtin_format_slice.pn diff --git a/penne.todo b/penne.todo index 97f6ff8..91671e2 100644 --- a/penne.todo +++ b/penne.todo @@ -229,6 +229,7 @@ Documentation: ☐ Simplify wasm4 saving-data.md using cast Development: + ☐ Drop llvm-sys and generate LLVM BC and textual IR by hand ☐ Release 0.4.0 ☐ Flesh out core and vendor/libc ☐ Add/check documentation for E359, E553 diff --git a/src/generator.rs b/src/generator.rs index bc8bf42..3ff7ee2 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -2640,8 +2640,20 @@ fn format_slice( } else { + // let n = ? + // for i in 0..n { + // + // } + // create format string that is n times %.*s + // for i in 0..n { + // TODO call the same llvm IR snippet for multiple Expressions + // TODO my Generator currently doesn't have a nice way to do that + // } buffer.add_text("["); // TODO print elements + // buffer.add_specifier("%.*s"); + // buffer.insert(formatted_elements_len); + // buffer.insert(formatted_elements_ptr); buffer.add_text("]"); Ok(()) } diff --git a/tests/execution.rs b/tests/execution.rs index d4e69b0..830f6e6 100644 --- a/tests/execution.rs +++ b/tests/execution.rs @@ -703,6 +703,16 @@ fn execute_builtin_format_array() -> Result<(), anyhow::Error> Ok(()) } +#[test] +fn execute_builtin_format_slice() -> Result<(), anyhow::Error> +{ + let output = execute("tests/samples/valid/builtin_format_slice.pn")?; + let stdout = stdout_from_output(output)?; + let expected = "[]\n[200]\n[12, 34]\n[1, 22, 333, 4444, 55555, -6]\n"; + assert_eq!(stdout, expected); + Ok(()) +} + #[test] fn execute_builtin_format_struct() -> Result<(), anyhow::Error> { diff --git a/tests/samples/valid/builtin_format_slice.pn b/tests/samples/valid/builtin_format_slice.pn new file mode 100644 index 0000000..b57615f --- /dev/null +++ b/tests/samples/valid/builtin_format_slice.pn @@ -0,0 +1,17 @@ + +fn main() -> u8 +{ + var empty: []i32 = []; + var one: []i32 = [200]; + var two: []i32 = [12, 34]; + print_slice(empty); + print_slice(one); + print_slice(two); + print_slice([1, 22, 333, 4444, 55555, -6]); + return: 0 +} + +fn print_slice(data: []i32) +{ + print!(data, "\n"); +}