all repos — xyw @ d5462eb6b0080f193c6df100231f3753477974bd

A minimal virtual machine and assembler for terminals.

Improved argument processing.
h3rald h3rald@h3rald.com
Thu, 05 Feb 2026 13:12:42 +0100
commit

d5462eb6b0080f193c6df100231f3753477974bd

parent

c373ff690a595f92dce281c5b11bc9496bf9a86c

2 files changed, 43 insertions(+), 7 deletions(-)

jump to
M test.shtest.sh

@@ -6,7 +6,7 @@ TEST_CASES=(

"run_test helloworld.xyw assert_output_equals \$'Hello, World!\\n'" "run_test d6.xyw assert_output_in_range 1 6" "run_test on-error.xyw assert_output_equals \$'Error: Division by zero.\\nError: Unknown error.\\n'" - "run_test print-args.xyw assert_output_equals \$'print-args.xim, \\n'" + "run_test print-args.xyw test1 test2 -- assert_output_equals \$'print-args.xim, test1, test2\\n'" ) ### Helper functions

@@ -89,11 +89,13 @@ run_image_capture_stdout() {

local xyw_bin="$1" local img_basename="$2" # e.g. helloworld.xim local out_file="$3" + shift 3 + local run_args=("$@") need_file "$EXAMPLES_DIR/$img_basename" local run_log="$TMPDIR/run.${img_basename}.log" - if ! (cd "$EXAMPLES_DIR" && "$xyw_bin" "$img_basename" >"$out_file" 2>"$run_log"); then + if ! (cd "$EXAMPLES_DIR" && "$xyw_bin" "$img_basename" "${run_args[@]}" >"$out_file" 2>"$run_log"); then dump_file_if_exists "Runtime stderr" "$run_log" dump_file_if_exists "Runtime stdout" "$out_file" fail "Execution failed for $img_basename"

@@ -156,10 +158,41 @@ }

### Test runner +# Usage: run_test src.xyw [run_args...] -- assert_fn [assert_args...] +# If no '--' is provided, the second argument is treated as assert_fn. run_test() { local src_basename="$1" - local assert_fn="$2" - shift 2 + shift + + local run_args=() + local assert_fn="" + local assert_args=() + + # Parse arguments: collect run_args until '--', then assert_fn and assert_args + while (($# > 0)); do + if [[ "$1" == "--" ]]; then + shift + break + fi + run_args+=("$1") + shift + done + + # If we found '--', remaining args are assert_fn + assert_args + # Otherwise, run_args[0] is the assert_fn and rest are assert_args + if (($# > 0)); then + assert_fn="$1" + shift + assert_args=("$@") + else + # No '--' found; first element of run_args is assert_fn + if ((${#run_args[@]} == 0)); then + fail "Missing assertion function for $src_basename" + fi + assert_fn="${run_args[0]}" + assert_args=("${run_args[@]:1}") + run_args=() + fi if ! declare -F "$assert_fn" >/dev/null 2>&1; then fail "Assertion function not found: $assert_fn"

@@ -173,10 +206,10 @@

local raw_out="$TMPDIR/${src_basename%.xyw}.raw" local norm_out="$TMPDIR/${src_basename%.xyw}.norm" - run_image_capture_stdout "$XYW_BIN" "$img_basename" "$raw_out" + run_image_capture_stdout "$XYW_BIN" "$img_basename" "$raw_out" "${run_args[@]}" normalize_crlf "$raw_out" "$norm_out" - "$assert_fn" "$norm_out" "$@" + "$assert_fn" "$norm_out" "${assert_args[@]}" echo "-> PASS: $src_basename" }
M xywrun.cxywrun.c

@@ -329,7 +329,10 @@ for (size_t j = 0; arg[j] != '\0'; j++)

{ write(TERMINAL_INPUT, (xyw_byte)arg[j]); } - write(TERMINAL_INPUT, XYW_ARGUMENT_SEPARATOR); + if (i < xyw_argc - 1) + { + write(TERMINAL_INPUT, XYW_ARGUMENT_SEPARATOR); + } } write(TERMINAL_INPUT, XYW_END_OF_ARGUMENTS); xyw_arguments_processed = 1;