all repos — xyw @ 4f43feac68ef6e669550d985923de58832d74713

A minimal virtual machine and assembler for terminals.

Reviewed error handling.
h3rald h3rald@h3rald.com
Wed, 04 Feb 2026 09:47:09 +0100
commit

4f43feac68ef6e669550d985923de58832d74713

parent

d47c74dd74465f347a388b3b2bcf28905a6ee6df

3 files changed, 9 insertions(+), 9 deletions(-)

jump to
M examples/on-error.xywexamples/on-error.xyw

@@ -9,13 +9,13 @@ end JMPw

; on error handler .label on_error - system.error $1 EQU on_error.div-by-zero.print JCNw + system.error LDB $1 EQU on_error.div-by-zero.print JCNw unknown-error print JSRw $0 system.error STB RTS .label on_error.div-by-zero.print div-by-zero-error print JSRw - $0 system.error STB + $3 system.error STB RTS ; printing subroutine
M test.shtest.sh

@@ -5,7 +5,7 @@ ### Test cases

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.\\n'" + "run_test on-error.xyw assert_output_equals \$'Error: Division by zero.\\nError: Unknown error.\\n'" ) ### Helper functions
M xywrun.cxywrun.c

@@ -298,24 +298,23 @@ //// Returns 1 on success, 0 on error

int xyw_eval(xyw_word start_pc) { *pc = start_pc; - xyw_byte handler_entry_s = 0; // Track stack depth when entering error handler + xyw_byte error_handler_entry_s = 0; if (xyw_memory[SYSTEM_STATE] & xyw_debug) { xyw_memory[SYSTEM_STATE] |= SYSTEM_STATE_DEBUG; } - while ((xyw_memory[SYSTEM_STATE] & SYSTEM_STATE_RUNNING) && *pc < SYSTEM_MEMORY_START) { if (xyw_memory[SYSTEM_ERROR] != XYW_ERROR_NONE && !(xyw_memory[SYSTEM_STATE] & SYSTEM_STATE_ERROR)) { - // An error has occurred, update state - xyw_memory[SYSTEM_STATE] |= SYSTEM_STATE_ERROR; xyw_word on_error_handler = XYW_PEEKW(&xyw_memory[SYSTEM_ON_ERROR]); // If an error handler is set, jump to it if (on_error_handler != 0) { - handler_entry_s = *s; // Record stack depth before pushing return address + // Enter error state + xyw_memory[SYSTEM_STATE] |= SYSTEM_STATE_ERROR; + error_handler_entry_s = *s; // Record stack depth before pushing return address ss_pushw(*pc); *pc = on_error_handler; }

@@ -376,8 +375,9 @@ /* JSR */ OPC_SR1(0x1E, xyw_word addr = ADDR(ARG), ss_pushw(*pc+1); *pc = addr-1;);

/* RTS */ case 0x1F: *pc = ss_popw()-1; // If returning from error handler, clear flag to allow re-trigger - if ((xyw_memory[SYSTEM_STATE] & SYSTEM_STATE_ERROR) && *s == handler_entry_s) { + if ((xyw_memory[SYSTEM_STATE] & SYSTEM_STATE_ERROR) && *s == error_handler_entry_s) { xyw_memory[SYSTEM_STATE] &= ~SYSTEM_STATE_ERROR; + error_handler_entry_s = 0; } break; // clang-format on