Files
R8Forge/drv/calc_lex.r8asm
dsh fa12494e43
ci / cell 16 (push) Skipped
ci / cell 32 (push) Skipped
ci / cell 8 (push) Skipped
ci / wide sweep (manual) (11) (push) Skipped
ci / wide sweep (manual) (16) (push) Skipped
ci / wide sweep (manual) (24) (push) Skipped
ci / wide sweep (manual) (32) (push) Skipped
ci / wide sweep (manual) (8) (push) Skipped
ci / cold cache (manual) (push) Skipped
ci / baseline (11 bit) (push) Failing after 16s
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.
2026-11-10 19:47:23 +08:00

115 lines
4.1 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
; calc_lex —— 桌上计算器 stage 1:只做字符分派(docs/calc.md §3 第 1 步)。
;
; 固定读 16 个字符(%rep 16,没有 EOF 处理),每读一个打一个数:
; '0'..'9'(0x30..0x39) -> 1 + digit (1..10)
; '*' '+' ',' '-' '.' '/'(0x2A..0x2F)-> 100 + (C - 0x2A) (100..105)
; 其它(空格、CR、字母……) -> 200
;
; 判法(bit4 是便宜的粗筛):
; * 字母的大写/小写都有 bit6=1,而数字和六个命令都没有 —— 先把 bit6 转到 bit0,
; 有就 erase C,让后面两个测试都跳过,R 留 200;
; * 再把 bit4 转到 bit0:数字 0x30..0x39 有 bit4=1,六个命令没有 —— biz/rnz 判 cell&1;
; * 剩下的 C 做一次减法 t = C - 0x2A(clb_sub_nc,常数 42 用 load 装):
; 命令正好 0..5,进位输出 Ci = 1 表示"没有借位"(C >= 0x2A),
; 空格 0x20 / CR 0x0D 都 < 0x2A,Ci = 0,R 留 200。
;
; 分支形状:每个条件体的身体都回到自己的条件格(净位移 0,见 AGENTS.md
; "语义上会咬人的几条");每轮先把 R 重置成 200,数字/命令分支只负责覆盖 R,
; 最后统一 output R —— 于是"跳过另一条分支"不需要 if/else。
;
; 边界:数字只按任务要求用 bit4 粗筛,所以协议外的 0x10..0x1F、0x3A..0x3F
; 也会落进数字分支(本阶段不喂这些字符);协议内的数字/六个命令/空格/CR/
; LF/TAB/大小写字母都按上面的规则输出。
;
; 验证(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
;
; 这条纸带 90738 字符;上面这个输入 29990 步。(%rep 16 把身体展开 16 份,
; 已超过 ROT-8-viz 的 65536 条上限;这个驱动只用 derive/tape_run 验证。)
;
; stage 1 固定读 16 个字符:更长的输入只吃前 16 个,没有提前退出的逻辑;
; 更短的输入(包括这里 15 个字符的验证串)后面读到 EOF=0,按键值 0 当"其它",
; 所以固定会输出 16 个数。EOF 退出留给 stage 5 的主循环。
%include "asm/lib.r8asm"
%include "asm/raw_sub_nc.r8asm" ; 顺带带进 clb_not_ip / clb_add_nc
; Macro load
; %1: Cell operand
; %2: Count value
;
; 先把整格清干净,再 `.xor` 上常数(`.xor` 是异或,不清格就变成和旧值异或)。
%macro load 2
erase %1
.tp %1
.xor %2
%endmacro
.var C 0 ; 当前字符
.var B 1 ; load 的常数格 / 运算的 B 操作数
.var T 2 ; t = C - 0x2A(命令索引)
.var R 3 ; 这一轮要输出的数(默认 200)
.var Ci 4 ; 进位/借位
%rep 16
load R 200
.tp C
inp
; bit6 -> bit0:字母(含 0x50..0x5A 这种 bit4 也为 1 的大写)先出局
%rep 6
ror
%endrep
.tp C
biz
erase C ; 字母/其它:R 保持 200;C 清 0,后面两个测试自然跳过
rnz
; 恢复原字符(6 + 5 = 11 次旋转是一整圈)
%rep 5
ror
%endrep
; bit4 -> bit0:数字
%rep 4
ror
%endrep
.tp C
biz
; 数字:恢复原字符后 R = C - 47 = 1 + digit
%rep 7
ror
%endrep
load B 47
.tp Ci
set_1 ; Ci=1 -> clb_sub_nc 算 C - B
clb_sub_nc C B R Ci
.tp C ; 身体回到条件格;C 已被减法吃成 0
rnz
; 非数字:恢复原字符(数字分支吃掉的 C=0 转多少次还是 0)
%rep 7
ror
%endrep
; 命令索引 t = C - 0x2A;Ci = 1 表示 C >= 0x2A
load B 42
.tp Ci
set_1
clb_sub_nc C B T Ci
.tp Ci
biz
; 命令:R = t + 100
load B 100
.tp Ci
set_0 ; Ci=0 -> 普通加法
clb_add_nc T B R Ci
.tp Ci ; 身体回到条件格;100..105 没有进位出,Ci=0
rnz
output R
%endrep