The two register machine (A accumulator + P pending operand) could not nest:
"1 2 + 3 4 * + ." printed 0007 because the second operator overwrote the
first. The calculator now keeps a 3 cell stack (S0 is the top), operators run
immediately (pop two, push one), '.' is a real peek (repeated '.' shows the
same value, and it no longer flushes anything), and ',' clears both the stack
and the depth.
Depth is counted in DEPTH because the unary minus rule needs it: '-' is unary
iff the stack is empty. The previous "the previous character was an operator"
rule is wrong for a stack machine -- in "1 2 + -" the minus has to be a binary
subtraction (popping an empty cell yields 0, and 0 - top is exactly a
negation); treating it as unary would silently swallow the sign. The
"have == 0" rule from the original plan was already wrong for the old machine
("2 * -5" would lose its minus).
Sizing was driven by the tape: every push/pop is one copy per stack cell
(~500 characters), and the file has to stay under 65536. So 3 cells instead
of the 16 the TODO dreamed of: push is two copies (S2<-S1, S1<-S0), an
operator is two more (S1<-S2, then the result), and the new number is written
straight into clb_undigits' target cell (it erases N itself) so the extra
"result -> top" copy disappears -- that one was worth 540 characters. S2 sits
in cell 33, the freed OP cell: putting it at 51 cost 3.7k in walking alone.
64983 characters (was 62624) and 246142 steps for "12 34 + ." (was 280645 --
the old settle-the-pending-OP block is gone). The file header carries the
measured session list, the nesting cases, the stack move order (high to low,
or the source gets overwritten) and the older "2047 3 / ." caveat; docs/calc.md
gets the stack section, and SPEC 5.4/7.10 now point at DEPTH instead of NEED.
5.4 KiB
TODO
"还没做"的事都收在这一个文件里,别散在 README/AGENTS/docs 中。每条写清楚为什么值得做和
从哪开始;做完就删掉(历史在 git log 里)。SPEC.md §12 那张"已经确认走不通"的表是
结论,不是待办,别混进来。
已经做完、从这份清单里移出去的:桌上计算器(examples/calc.r8asm,11 位 64983 字符、
三方一致、位宽矩阵见 tests/widths.ps1)、INP 整格写入的语义修正、参考虚拟机支持 >64k
纸带、有符号数三步走(clb_digits_signed / clb_divmod_signed / 计算器的一元负号,
契约在 SPEC §5.4、实现在 §7.10,穷举在 r8forge_byte_op_test 的 test_digits_signed 2048
个值 和 test_divmod_signed 4202 组)、RPN 栈(3 格栈 + 运算符立刻算 + . 真 peek,
examples/calc.r8asm 现在是真 RPN:1 2 + 3 4 * + . = 0015)。
交互计算(下一步)
- 终端纸带:提示符 + 逐字符回显 + 行编辑(退格打印
\b \b)+ 命令分派。零件都在drv/calc_*(分派、数字攒取、显示、四则)和examples/term.r8asm(提示符、回显、 退格、换行补提示符、EOF 收摊)——缺的是"行缓冲 + 落行 + 命令分派"。 验收用头部; in喂一整段会话,examples_test会自动跑。 - 迷你
printf:clb_puts和load已经有了(examples/hello.r8asm能打一整句),还缺%d/%c/%s的格式化 —— 那需要"格式串 + 参数表"的约定,比 puts 复杂一档。 没有间接寻址,所以格式串在汇编期长度固定(像clb_puts的 LEN),参数表要靠 "第 k 个参数"的分派树;%d直接是clb_digits_signed,%c是一条out,%s得再约定"串 + 长度"两格。 - 栈再深一点:现在是 3 格(
64983字符,离 65536 只剩 553)。4 格要多花一次 push 的copy(约 500)—— 要么先省掉别处的 500 字符,要么认了 65500+ 的纸带。3 格 已经够1 2 + 3 4 * +这种两层的嵌套;真要写长表达式就得再想办法。
工具
-
纸带级的"合并重复段落"(这条原来的标题在交接时丢了,剩下的正文原样留着): 重复清位…… 要能重算跳转偏移。配套
r8forge_tape_diff(比较两条纸带的 OUT 流和步数) 证明等价 —— 一次改动全库受益。 -
全家桶要不要把两个有符号包装宏收回去:
asm/test.r8asm现在是"每个宏至少调一次", 但clb_digits_signed/clb_divmod_signed被排除在外(各要多展开一份clb_digits/clb_divmod,+12.1k / +5.6k,而 65536 的预算只剩 ~3.2k)。两条路:把全家桶拆成两条 纸带,或者承认 65536 只是历史口径(viz 现在按纸带长度分配缓冲;实测 16 位的 calc 是 96052 字符、在 viz 里照跑,见tests/widths.ps1)。现在的兜底是examples/signed_demo.r8asm+byte_op_test的穷举。 -
%synth --verify:把.r8forge_cache/里每条结果按对应表的所有输入重跑一遍, 给"缓存已入库"兜底 —— 一条坏条目会让每次构建静默用错块。 -
CI:暂时不做。本地
ctest --test-dir build -C Release -j 4(8 条,12 秒)和tests/widths.ps1(位宽矩阵,约 20 分钟)都能手动跑;act_runner 的镜像装不出 cmake, 先放着。ROT-8-viz 仍然零测试:串行默认 /-n/ 几何 / 7E1 全靠手测 —— 哪天真要接 CI,先补它。 -
src/r8asm.cc:154有个上游留下的// TODO: FILL THE BLANK(ROT8_NODE宏的 children 是空的)。那是继承来的实现,先记着,别顺手改。
画面
- 把 259 格当帧缓冲:串行下
\r(0x0D)能原地重绘,1 bit/cell 的 16×16 位图或 8×8 生命游戏都能塞进纸带,rot8_test --live里直接看它动。
要重新量的一条
copy的脏缓冲:SPEC §7.7 写着"缓冲格必须干净,脏了拷贝结果是 0",但 stage 2 的探针 (缓冲 2040、源 4)里copy结果是对的、缓冲也自己归零了。要么那条结论当初量的是别的路径 (比如clb_digits里连着用的写法),要么它已经不成立 —— 写清楚之前别把两种说法都留着。 顺便:clb_bit_mov的「吃源」契约也值得一起复验。
想清楚但没做的优化
clb_divmod预计算~D:单轮 2019 → 973 步,估计 28.6k → 17.6k 步,纸带净 −76; 没换是因为当时指名用raw_sub。细节和注意事项见docs/divmod.md第 6 节。clb_divmod里那个 495 字符的提交copy:还是全展开的,可以想压缩的办法。
顺手清掉的小尾巴
- viz 的
read_tape(固定缓冲那版)在vm_test.c改用read_tape_alloc之后应该没人用了: 要么删掉,要么在头注释里标 deprecated。 - viz 的
INS_SIZE现在只是历史常量(真正的上限是ROT8_MAX_TAPE= 16 M 条指令), README 里可以说明一句。实测证据:16 位的examples/calc.r8asm是 96052 字符,rot8_test --cell 16 --input "12 34 + ."照样打出> 00046(tests/widths.ps1)。 顺带:viz 只有在 cell ≥ 8 位时才走串行 ASCII,7 位下每个 OUT 打一行数字 ——tests/widths.ps1的 viz 断言现在按这条跳过了 7 位(那一档由r8forge_derive兜)。