feat(v4.0.0): the stacks are guarded (D-16); DEPTH, PICK and ROLL

Ruled 2026-10-04, revising D-2: stack overflow and underflow are errors
that are shown and return to the prompt, not silent wrap-around.

- Each stack counts what it holds.  Before every opcode the executor
  checks that the stacks hold what it takes and have room for what it
  leaves; otherwise the opcode does nothing and the node faults, as for a
  bad address, to that kind's handler.  Every fault empties both stacks.
- The fault handler is now a table of five jumps: address, data overflow,
  data underflow, return overflow, return underflow.  The host node says
  "Stack overflow", "Stack underflow", "Return stack overflow",
  "Return stack underflow", then ERROR and the prompt.
- Two registers, DSTACK-DEPTH and RSTACK-DEPTH: a fetch reads the depth,
  a store empties the stack.  QUIT, ABORT and the error exits empty the
  return stack before they call anything; ABORT empties the data stack.
- capsule/forth.v4: DEPTH, PICK and ROLL, to FORTH-79 (counting from
  one).  PICK and ROLL set the values above the one wanted aside in
  memory, and work with the stack full.
- Division by zero now takes its operands off the stack, as v3 does.
- A colon with no room for its entry abandons the line.
- tests: every opcode at every depth of both stacks; the faults, the
  registers and the three words from the prompt.

The sizes are unchanged: ten values, nine return entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rajames
2026-10-04 17:31:18 -04:00
co-authored by Claude Opus 5.5
parent 361dcd1148
commit 0da7e32a0b
16 changed files with 542 additions and 106 deletions
+7 -4
View File
@@ -45,10 +45,13 @@ void v4_exec_reset(v4_exec_state *es);
* so the call does not return while R is nonzero at a `unext`. Returns the
* number of instructions retired.
*
* An address outside node memory -- P at the fetch, or the address an opcode
* uses -- is a fault (node.h, D-14): that opcode and the rest of the word do
* not execute, and P is the node's fault handler on return. With no handler
* the node stops, and this does nothing and returns 0 from then on. */
* FAULTS (node.h; D-14 and D-16). Before an opcode runs, the stacks must
* hold what it takes -- a T or S it only reads counts -- and have room for
* what it leaves; and an address it uses, or P at the fetch, must be inside
* node memory. If not, that opcode and the rest of the word do not execute,
* both stacks are emptied, and P is that kind's word of the node's fault
* table on return. With no table the node stops, and this does nothing and
* returns 0 from then on. */
unsigned v4_exec_step_word(v4_node *n, v4_exec_state *es, v4_heat *h);
/* Execute a single opcode. The branch opcodes read their target from `w` and