From fa12494e43beb21f236b7c717ff281c03960274a Mon Sep 17 00:00:00 2001 From: dsh Date: Tue, 10 Nov 2026 19:47:23 +0800 Subject: [PATCH] calc: stage 1 verified -- the character dispatch of the desk calculator drv/calc_lex.r8asm reads 16 characters and classifies each one: digits print 1+value, the six commands (* + , - . / = 0x2A..0x2F) print 100+index, anything else prints 200. The discriminator is bit 4 of the character (digits 0x30..0x39 have it, the command block does not), so four `ror`s and one `biz` split them, and `t = C - 0x2A` is the command index. Measured (tape_run and derive agree; tape 90738 chars, 29990 steps): in 0 5 9 + - . , * (15 chars; the 16th inp is EOF -> 0) out 1 200 6 200 10 200 101 200 103 200 104 200 102 200 100 200 i.e. 0->1, space->200, 5->6, 9->10, +->101, -->103, .->104, ,->102, *->100, EOF->200. Full-alphabet cross-checks: "0123456789*+,-./" gives 1..10 then 100..105, sixteen letters give sixteen 200s, a one char input gives its value and fifteen 200s. Two things recorded in the file's header: 90738 characters is over ROT-8-viz's 65536 INS_SIZE because %rep 16 unrolls the body (this driver is verified through derive/tape_run only; the final program will use a runtime loop instead), and the bit 4 filter also sends 0x10..0x1F and 0x3A..0x3F down the digit path, which no protocol input can reach. This is also the file that slipped into the CI commit earlier -- now it has a commit of its own with its measured numbers. --- drv/calc_lex.r8asm | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/drv/calc_lex.r8asm b/drv/calc_lex.r8asm index c7e85bc..532dbb8 100644 --- a/drv/calc_lex.r8asm +++ b/drv/calc_lex.r8asm @@ -1,6 +1,6 @@ ; calc_lex —— 桌上计算器 stage 1:只做字符分派(docs/calc.md §3 第 1 步)。 ; -; 固定读 8 个字符(%rep 8,没有 EOF 处理),每读一个打一个数: +; 固定读 16 个字符(%rep 16,没有 EOF 处理),每读一个打一个数: ; '0'..'9'(0x30..0x39) -> 1 + digit (1..10) ; '*' '+' ',' '-' '.' '/'(0x2A..0x2F)-> 100 + (C - 0x2A) (100..105) ; 其它(空格、CR、字母……) -> 200 @@ -21,19 +21,19 @@ ; 也会落进数字分支(本阶段不喂这些字符);协议内的数字/六个命令/空格/CR/ ; LF/TAB/大小写字母都按上面的规则输出。 ; -; 验证(8 个字符,正好读满;六个命令和空格都在里面): -; build/Release/r8forge_derive.exe drv/calc_lex.r8asm 0 -i "9 *+,-./" -; in 9 *+,-./ -; out 10 200 100 101 102 103 104 105 +; 验证(16 次读:15 个字符读完,第 16 次 inp 拿到 EOF=0,按"其它"输出 200): +; build/Release/r8forge_derive.exe drv/calc_lex.r8asm 0 -i "0 5 9 + - . , *" +; in 0 5 9 + - . , * +; out 1 200 6 200 10 200 101 200 103 200 104 200 102 200 100 200 ; -; 这条纸带 45378 字符;上面这个输入 17120 步。 +; 这条纸带 90738 字符;上面这个输入 29990 步。(%rep 16 把身体展开 16 份, +; 已超过 ROT-8-viz 的 65536 条上限;这个驱动只用 derive/tape_run 验证。) ; -; 固定读 8 个字符:更长的输入只吃前 8 个,剩下的不产生输出(stage 1 没有 -; EOF 处理)。例如 -i "0 5 9 + - . , *" 只读 "0 5 9 + "(含空格),输出 -; 1 200 6 200 10 200 101 200;要整行分派得等 stage 5 的主循环。 +; stage 1 固定读 16 个字符:更长的输入只吃前 16 个,没有提前退出的逻辑; +; 更短的输入(包括这里 15 个字符的验证串)后面读到 EOF=0,按键值 0 当"其它", +; 所以固定会输出 16 个数。EOF 退出留给 stage 5 的主循环。 %include "asm/lib.r8asm" -%include "asm/raw_sub_nc.r8asm" -%include "asm/raw_add_nc.r8asm" +%include "asm/raw_sub_nc.r8asm" ; 顺带带进 clb_not_ip / clb_add_nc ; Macro load ; %1: Cell operand @@ -52,7 +52,7 @@ erase %1 .var R 3 ; 这一轮要输出的数(默认 200) .var Ci 4 ; 进位/借位 -%rep 8 +%rep 16 load R 200 .tp C @@ -67,7 +67,7 @@ erase %1 erase C ; 字母/其它:R 保持 200;C 清 0,后面两个测试自然跳过 rnz - ; 恢复原字符(4 + 6 + 1 = 11 次旋转是一整圈) + ; 恢复原字符(6 + 5 = 11 次旋转是一整圈) %rep 5 ror %endrep