2 Commits
Author SHA1 Message Date
dsh c417304140 term: a line-editing terminal -- buffer, backspace and command dispatch
examples/term.r8asm was a per-character echo (prompt, echo, backspace erase,
newline, EOF).  It is now a small shell: a 4 character line buffer, a backspace
that really removes a character, and Enter -- or EOF, for a half typed line --
commits the line and dispatches on its first character.  'l' prints the length,
'r' prints the buffer backwards, 'q' quits, anything else is ignored.

The header session "lhi" exercises the whole chain: EOF commits the pending
line, so examples_test sees the '3'.  Three more sessions are documented in the
header: "rab" gives NUL+'b'+'a'+'r', "lab<BS>" gives '2' (the backspace really
dropped the 'b'), and "lhi<NL>rxy<NL>" prints '3' then the reversed second
line.

Three things ate most of the debugging time; all three are written up in the
file:

* "else" needs a flag cell.  biz enters when the low bit is set, so "if not A
  then B" cannot be inferred from "the last test missed": set FLAG=1 before the
  test, clear it in the matched branch, look at FLAG afterwards.  And biz tests
  the CURRENT dp cell, so the order has to be "set FLAG, then .tp to the
  condition cell, then biz" -- the wrong order made every character look like
  a commit key.
* clb_sub_nc eats its operands, so the echo and the append both work from the
  copy CE.  The first version appended C (already zeroed by the comparison),
  which left the line buffer empty: 'l' printed 0 and 'r' printed four NULs.
* the cell layout dominates the tape.  copy / clb_sub_nc / clb_nonzero all emit
  .tp internally, some of them inside %rep _CELL_SIZE, so a 20 cell gap costs
  a couple of thousand characters: putting the constants at 22..28 gave 33067
  characters, packing the hot cells into 0..27 gives 21721.

The buffer is left aligned with a length counter -- appending needs a dispatch
on LEN, but backspace is just LEN-- -- and 'r' prints all four cells instead of
dispatching on the length, which costs a NUL for the unused cells and is
documented.

tests/widths.ps1 now also checks term.r8asm at every width (its expected
stream is width independent, which is what makes it a good second program for
the matrix), and TODO/AGENTS/README record the item as done.
2026-11-18 21:58:29 +08:00
dsh 55ab5dfebd examples: the terminal tape -- prompt, echo, newline, backspace, EOF
examples/term.r8asm is the "type at it and it answers" program: it prints "> ", then
echoes every character the machine reads, prints a fresh prompt after each newline,
and answers a backspace with space + backspace so the cursor erases the glyph.
EOF (the 0 that inp returns when the input is gone) erases the loop quota, so the
loop ends and the tape runs off its end after one final newline.

  tape 3467 characters, 4743 steps for a two line session (the calculator is 62585)

Measured (derive and tape_run, then the reference machine's serial output):

    in  hi            out 62 32 104 105 10                       "> hi\n"
    in  hi<LF>ok      out 62 32 104 105 10 62 32 111 107 10      two prompts
    in  h<BS>i        out 62 32 104 8 32 8 105 10                the 8 32 8 pair

All three character streams agree with ROT-8-viz byte for byte (bar CRLF).
r8forge_examples_test passes all 18 examples.

One thing worth recording: my brief for this said "a body entered on LSB==0 needs no
clear" -- that is backwards, `[` *jumps* on LSB==0, so bodies are entered on LSB==1.
Following it inverted the dispatch (EOF fired with C=0x68) and it took a
`tape_run --trace` to see it; the file now documents the real rule next to the loop.
2026-11-17 20:31:36 +08:00