Comments are thrown away by the reader, not read one byte at a time

A comment used to walk through tokGet and srcNext and be classified,
character by character, on the way to being discarded. Comments are most
of what this assembler reads: 87 per cent of what Say.asm pulls in once
services.asm is counted, 59 per cent of CosmOS. The house style here is
dense commentary, so the assembler is penalised more than most by its own
sources.

The saving is not the classifying, it is the BOOKKEEPING. srcNext loads
and stores the walking pointer through memory for every character and
asks numCompare whether the buffer is used up. srcSkipComment keeps the
pointer in a data pointer for a whole run and the newline in B, so a
comment byte costs a load, a compare and two steps. A run is capped at
255 so one byte can count it, which is the only reason it loops.

Measured, each version with its own rebuilt images:

              without        with
  hello       791,957     586,184
  Say       9,924,401   4,506,702   2.20x
  Files    13,197,710   7,188,351   1.84x
  Keys     23,091,447  15,069,880   1.53x
  cosmos  886,498,996 789,982,899   1.12x

Which tracks the comment ratios: Say gains most and cosmos least, in
proportion to how much of each is prose.

---- And two mistakes worth keeping ----

The scratch went among the READER'S STATE, which is a block copied whole
by a count written down somewhere else - so every saved file lost the
last four bytes of itself and an include came back with its pointer
wrong. The comment above that block says not to do this, in capitals.
That is twice this week: scriptCopyState had the same shape this morning.

And a file that ends inside a comment has to put back the file that
included it, exactly as srcAtEnd does for a character. NOTHING IN THIS
REPOSITORY ENDS THAT WAY - every source here ends on a line of code with
a newline after it - so break.sh could not catch an error in that path
because nothing reached it. tail.asm is generated with no newline on its
last line for that reason, and usestail.asm names a label after the
include, which is what goes missing when the include never returns.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E2JrLzFvuFX9fgi1LDRjrW
This commit is contained in:
Anachronaut
2026-09-06 21:33:15 -04:00
co-authored by Claude Opus 5
parent e20c9bac1f
commit 2e9cb9af7e
3 changed files with 195 additions and 7 deletions
+31 -1
View File
@@ -71,13 +71,37 @@ APPS="Say greet Files Keys"
"$TOOL" put "$WORK/native.img" "$ROOT/Programs/CosmOS/Source/services.asm" services.asm >/dev/null
"$TOOL" put "$WORK/native.img" "$ROOT/Programs/CosmOS/Source/console.asm" console.asm >/dev/null
for app in $APPS; do
[ -f "$ROOT/Programs/CosmOS/Apps/$app.asm" ] || continue
"$TOOL" put "$WORK/native.img" "$ROOT/Programs/CosmOS/Apps/$app.asm" "$app.asm" >/dev/null
done
# ---- A file that ends in the middle of a comment ----
#
# Generated rather than committed, because what makes it work is the ABSENCE of a newline on
# the last line and no editor can be trusted to leave that alone.
#
# An included file running out inside a comment is the case the reader has to pop the include
# stack for, and it is the one shape no source in this repository happens to have: every file
# here ends on a line of code with a newline after it. Nothing exercised that path, so an
# error in it could not be caught - and comment skipping is exactly where such an error would
# live, since a comment is the only thing that reads past the end of a line without looking.
#
# usestail.asm names a label AFTER the include, which is what goes missing if the file that
# did the including is not put back: the include never returns, the rest of the program is
# never read, and the label is never defined.
printf '; nothing but a comment, and no newline after it' > "$WORK/tail.asm"
printf '#Include tail.asm\n#Program\n #Base 0x5000\nstart:\n SETD.0 Later\n HALT\n#Data\n #Base 0x3000\nLater:\n 0x00\n' > "$WORK/usestail.asm"
"$TOOL" put "$WORK/native.img" "$WORK/tail.asm" tail.asm >/dev/null
"$TOOL" put "$WORK/native.img" "$WORK/usestail.asm" usestail.asm >/dev/null
{
echo "load Asm.sbx"
echo "run hello.asm"
for app in $APPS; do echo "run $app.asm"; done
echo "run usestail.asm"
for app in $APPS; do
[ -f "$ROOT/Programs/CosmOS/Apps/$app.asm" ] || continue
echo "run $app.asm"
done
echo "exit"
} | "$EMU" --fast --cycles 600000000 -D "$WORK/native.img" "$WORK/cosmos.bin" \
> "$WORK/session.txt" 2>&1
@@ -85,6 +109,12 @@ done
# ---- It got as far as writing something ----
check "it wrote a file" grep -q "wrote hello.bin" "$WORK/session.txt"
# ---- And a file that ends inside a comment did not swallow the one that included it ----
#
# The label is defined after the #Include, so it exists only if the reader came back.
check "an include that ends in a comment comes back" \
grep -q "wrote usestail.sbx" "$WORK/session.txt"
# ---- And the file is the one the host assembler makes ----
"$ASM" -o "$WORK/reference.bin" "$ROOT/Programs/Examples/hello.asm" >/dev/null
"$TOOL" get "$WORK/native.img" hello.bin "$WORK/native.bin" >/dev/null 2>&1