Interactive I/O: teach the VMs INP, and let examples declare their input
The library can read characters now that the viz defaults to serial ASCII, but
R8Forge's own machines could not: r8forge_derive, r8forge_tape_run and the
examples harness all skipped ',' ("input is not modelled"), so an interactive
program could only be run through ROT-8-viz -- the wrong end of the dev loop,
and nothing in CI could cover it.
Three small pieces:
* INP (',') reads the next scripted character (low 7 bits) and 0 once the
text runs out, in tests/tape_run.cc, src/derive.cc and the examples VM.
r8forge_tape_run and r8forge_derive take -i/--input "<text>", so an
interactive program is run and checked exactly like any other:
r8forge_tape_run t.rot8 -i "12+34="
r8forge_derive calc.r8asm 0 -i "12+34="
* examples_test learns an optional `; in ...` header next to `; out`:
the characters after the marker are fed to INP before the run. The marker
must be the first word after ';' so "include" in a comment is not eaten.
* examples/echo_chars.r8asm: the smallest interactive program -- read four
characters, print them back. in R8F! -> out 82 56 70 33, 8 characters,
8 steps.
Verified the same tape three ways: tape_run -i "R8F!" -> 82 56 70 33,
derive 0 -i "R8F!" -> same, rot8_test --input R8F! -> prints R8F. With no
input at all INP reads 0, which is how every non-interactive example keeps
running unchanged.
Baseline: examples_test is now 15 examples + asm/test.r8asm.
This commit is contained in:
1 parent
ebc7b7c72e
commit
ae268b9442
7 files changed
+110
-6
No files matched your search
@@ -32,7 +32,7 @@ cmake --build build --config Release
|
||||
# 当前基线:都通过,别弄坏
|
||||
r8forge asm/test.r8asm # 64513 字符(每个标准宏至少一次 + 每个语言特性)
|
||||
r8forge_byte_op_test # 21 项(divmod 16384 组、digits 2048、undigits 4096)
|
||||
r8forge_examples_test # 14 个 example + asm/test.r8asm,核对头部 `; out`
|
||||
r8forge_examples_test # 15 个 example + asm/test.r8asm,核对头部 `; out`/`; in`
|
||||
r8forge_macro_docs_test # 48 macros in 22 files
|
||||
r8forge_synth_test examples/full_add.r8asm # all eight combinations ok
|
||||
r8forge_derive verify_loop.r8asm -1 0=5 # out 5 0
|
||||
@@ -60,6 +60,9 @@ r8forge_derive verify_loop.r8asm -1 0=5 # out 5 0
|
||||
`StarCells <peng080306@foxmail.com>` 别用)。具体命令和本机的 git 配置见 `AGENTS.local.md`。
|
||||
* **数字要能被别人复现**:§1.3 的口径写死了怎么量(最小驱动、不带 `output`);改宏之后
|
||||
顺手跑一遍基线,别只信自己这次跑出来的数。
|
||||
* **交互程序这样测**:`r8forge_tape_run t.rot8 -i "12+34="`(字符喂给 `inp`,低 7 位,读空后为 0),
|
||||
`r8forge_derive <src> 0 -i "..."` 是同一份输入的第二个实现;例程把输入写在头部
|
||||
`; in ...`,`r8forge_examples_test` 会自动喂。实时终端那一侧在 ROT-8-viz(默认串行)。
|
||||
* 一次会话很难把结论留在对话里 —— **落进 commit message 和文件注释**。
|
||||
|
||||
## 语义上会咬人的几条
|
||||
|
||||
@@ -83,7 +83,7 @@ src/r8asm.cc 汇编器实现
|
||||
src/synth.cc 真值表 -> 搜索
|
||||
asm/ 标准库(48 个宏,含 8 个 %synth 合成块)
|
||||
drv/ 单点验证驱动(喂固定输入跑一个宏,看 out/stat)
|
||||
examples/ 14 个能跑的例程:每组宏一份 + %synth 的几种写法
|
||||
examples/ 15 个能跑的例程:每组宏一份 + %synth 的几种写法 + 一个交互例程
|
||||
tests/ 汇编器 / 虚拟机 / 字节级 / 例程 / 文档格式 自检
|
||||
docs/ 设计笔记(长文)
|
||||
.r8forge_cache/ 合成结果缓存(已入库)
|
||||
@@ -113,7 +113,7 @@ build/Release/r8forge examples/digits_demo.r8asm > t.rot8
|
||||
build/Release/r8forge_tape_run.exe t.rot8 # 打印 out 和步数
|
||||
```
|
||||
|
||||
`r8forge_examples_test` 会把它们全跑一遍、逐项核对头部那行 —— 例程不会悄悄腐掉。每份例程
|
||||
`r8forge_examples_test` 会把它们全跑一遍、逐项核对头部那行 —— 例程不会悄悄腐掉。要喂输入的例程\n在头部多写一行 `; in ...`(`examples/echo_chars.r8asm` 是最小的例子),测试会先把那串字符\n交给 `inp`。每份例程
|
||||
演示哪一组宏见 [`SPEC.md`](SPEC.md) §10.3。
|
||||
|
||||
## 构建与自检
|
||||
|
||||
@@ -750,7 +750,7 @@ r8forge_tape_run t.rot8 # 打印 out 和步数
|
||||
|---|---|---|
|
||||
| `r8forge_byte_op_test` | 21 个测试项 = 20 个宏(`clb_neg_nc` 的 Ci=1 / Ci=0 各算一项),见 §7.1、§7.2、§7.5、§7.6、§7.7、§7.9、§8、§9 | **按返回值**:穷举 0..255(`clb_add_ip` 2048 抽样、`clb_mul` 65536 全穷举、`clb_ones` 12 个计数全穷举、`clb_divmod` 8 个除数 × 全部 2048 个被除数 = 16384 组全穷举、`clb_digits` 全部 2048 个值全穷举、`clb_undigits` 2048 个值 × 干净/高位垃圾 2 种 = 4096 组全穷举),任一不符即退出码非 0 |
|
||||
| `r8forge_synth_test examples/full_add.r8asm` | `%synth` 端到端:汇编 + 跑 8 种输入 | `all eight combinations ok` |
|
||||
| `r8forge_examples_test` | `examples/` 里全部 14 个程序(10 组宏例程 + 4 个 `%synth` 例子,见 §10.3)**加上 `asm/test.r8asm`**(§10.2) | **按返回值**:每个头部 `; out ...` 与实跑 `out` 逐项相等,缺这一行也算失败 |
|
||||
| `r8forge_examples_test` | `examples/` 里全部 15 个程序(10 组宏例程 + 4 个 `%synth` 例子 + 1 个交互例程,见 §10.3)**加上 `asm/test.r8asm`**(§10.2) | **按返回值**:每个头部 `; out ...` 与实跑 `out` 逐项相等,缺这一行也算失败;写了 `; in ...` 的先把那串字符喂给 `inp` |
|
||||
| `r8forge verify_clb_bit_add_nc.r8asm` | 4 cell 加法切片(进位进→出) | 单点:`0+0+Ci` |
|
||||
| `r8forge verify_clb_bit_inc_nc.r8asm` | 3 cell 自增切片 | 单点 |
|
||||
| `r8forge_derive verify_loop.r8asm -1 0=N` | 动态循环:`N=0..11` 时循环体跑 `N` 次(`A = N`、`C = 0`) | 逐值 `out` |
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
; in R8F!
|
||||
; out 82 56 70 33
|
||||
;
|
||||
; 最小的交互程序:读一个字符(`inp`)、原样打出来(`out`),重复 4 次.
|
||||
; 输入是序列的,由运行器喂:r8forge_tape_run t.rot8 -i "R8F!"
|
||||
; (实时终端那一侧在 ROT-8-viz,它的默认 I/O 就是串行字符.)
|
||||
%include "asm/lib.r8asm"
|
||||
|
||||
.var C 0
|
||||
|
||||
%rep 4
|
||||
.tp C
|
||||
inp
|
||||
output C
|
||||
%endrep
|
||||
@@ -11,10 +11,16 @@
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
// Characters handed to INP (','); -i/--input fills it once per process, which is
|
||||
// what a scripted run of an interactive program needs. The live terminal case
|
||||
// lives in ROT-8-viz, not here.
|
||||
std::deque<uint16_t> g_input;
|
||||
|
||||
struct machine {
|
||||
std::vector<uint16_t> cells = std::vector<uint16_t>(kDefaultMemoryCells, 0);
|
||||
std::vector<uint16_t> out;
|
||||
@@ -72,6 +78,11 @@ struct machine {
|
||||
case '.':
|
||||
out.push_back(cells[dp]);
|
||||
break;
|
||||
case ',': // INP: low 7 bits of the next scripted character, 0 at EOF
|
||||
cells[dp] = g_input.empty() ? 0 : g_input.front();
|
||||
if (!g_input.empty())
|
||||
g_input.pop_front();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -100,6 +111,22 @@ bool assemble(r8asm &a, const char *file, std::string &tape) {
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
// -i/--input <text> may appear anywhere: its characters go to INP (',')
|
||||
int keep = 0;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (std::strcmp(argv[i], "-i") == 0 ||
|
||||
std::strcmp(argv[i], "--input") == 0) {
|
||||
if (i + 1 >= argc) {
|
||||
std::cerr << "-i needs a string of input characters\n";
|
||||
return 2;
|
||||
}
|
||||
for (const char *p = argv[++i]; *p; p++)
|
||||
g_input.push_back(uint16_t((unsigned char)*p & 0x7f));
|
||||
continue;
|
||||
}
|
||||
argv[++keep] = argv[i];
|
||||
}
|
||||
argc = keep + 1; // argv[0] is still the program name
|
||||
if (argc < 3) {
|
||||
std::cerr << "usage: r8forge_derive <file.r8asm> <params> [cells...]\n";
|
||||
return 2;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <deque>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -29,6 +30,7 @@ namespace {
|
||||
struct machine {
|
||||
std::vector<uint16_t> cells = std::vector<uint16_t>(kDefaultMemoryCells, 0);
|
||||
std::vector<uint16_t> out;
|
||||
std::deque<uint16_t> in; // characters for INP, from the header's `; in`
|
||||
size_t dp = 0;
|
||||
size_t steps = 0;
|
||||
bool finished = true; // false: the step limit ran out
|
||||
@@ -86,6 +88,11 @@ struct machine {
|
||||
case '.':
|
||||
out.push_back(cells[dp]);
|
||||
break;
|
||||
case ',': // INP: low 7 bits of the next header character, 0 when empty
|
||||
cells[dp] = in.empty() ? 0 : in.front();
|
||||
if (!in.empty())
|
||||
in.pop_front();
|
||||
break;
|
||||
default: // ',' (input) is not modelled
|
||||
break;
|
||||
}
|
||||
@@ -137,6 +144,35 @@ bool expected_out(const std::filesystem::path &file, std::vector<int> &want) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The optional `; in R8F!` line: characters handed to INP. Everything after
|
||||
// the marker is taken literally (leading spaces are dropped), so an interactive
|
||||
// example can spell out a whole session in its header. No marker means "no
|
||||
// input": INP then reads 0, which is how the non-interactive examples run.
|
||||
void expected_in(const std::filesystem::path &file, std::string &text) {
|
||||
std::ifstream in(file);
|
||||
std::string line;
|
||||
while (std::getline(in, line)) {
|
||||
if (!line.empty() && line.back() == '\r')
|
||||
line.pop_back();
|
||||
const size_t at = line.find_first_not_of(" \t");
|
||||
if (at == std::string::npos || line[at] != ';')
|
||||
continue;
|
||||
// Require the marker to be the first word after ';' -- "include" in a
|
||||
// comment must not be mistaken for an input declaration.
|
||||
size_t w = at + 1;
|
||||
while (w < line.size() && (line[w] == ' ' || line[w] == '\t'))
|
||||
w++;
|
||||
if (line.compare(w, 2, "in") != 0)
|
||||
continue;
|
||||
w += 2;
|
||||
if (w < line.size() && line[w] != ' ' && line[w] != '\t')
|
||||
continue;
|
||||
const size_t b = line.find_first_not_of(" \t", w);
|
||||
text = b == std::string::npos ? std::string() : line.substr(b);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::string join(const std::vector<int> &v) {
|
||||
std::ostringstream os;
|
||||
for (size_t i = 0; i < v.size(); i++)
|
||||
@@ -173,7 +209,11 @@ int main(int argc, char **argv) {
|
||||
failures++;
|
||||
return;
|
||||
}
|
||||
std::string in_text;
|
||||
expected_in(file, in_text);
|
||||
machine m;
|
||||
for (char ch : in_text)
|
||||
m.in.push_back(uint16_t((unsigned char)ch & 0x7f));
|
||||
m.run(tape);
|
||||
std::vector<int> got;
|
||||
for (uint16_t v : m.out)
|
||||
|
||||
+21
-2
@@ -2,9 +2,12 @@
|
||||
// Independent ROT-8 interpreter. Reads a .rot8 tape straight from a file --
|
||||
// no macro, no assembler, no r8asm -- and prints what `out` ('.') produced.
|
||||
//
|
||||
// r8forge_tape_run <file.rot8> [cells...]
|
||||
// r8forge_tape_run <file.rot8> [-i text] [cells...]
|
||||
//
|
||||
// cells are addr=value (decimal), e.g. 0=128
|
||||
// -i/--input gives INP (",") its characters, low 7 bits each; when the text
|
||||
// runs out INP reads 0. That is enough to drive an interactive program from
|
||||
// a script -- the ROT-8-viz CLI is the one that prompts a live terminal.
|
||||
//
|
||||
// This exists as a second opinion for r8forge_derive: if the two disagree, the
|
||||
// difference is in derive; if they agree, the disagreement is in the program.
|
||||
@@ -12,6 +15,7 @@
|
||||
// Note the character map: "><+-[].," -- '-' is flb, '+' is ror, and ror does
|
||||
// not change whether a cell is zero (that is what makes [-] a two value loop).
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
@@ -31,6 +35,7 @@ int main(int argc, char **argv) {
|
||||
std::istreambuf_iterator<char>());
|
||||
|
||||
std::vector<uint16_t> cells(kDefaultMemoryCells, 0);
|
||||
std::deque<uint16_t> input;
|
||||
bool trace = false;
|
||||
for (int i = 2; i < argc; i++) {
|
||||
const std::string a = argv[i];
|
||||
@@ -38,6 +43,15 @@ int main(int argc, char **argv) {
|
||||
trace = true;
|
||||
continue;
|
||||
}
|
||||
if (a == "-i" || a == "--input") {
|
||||
if (i + 1 >= argc) {
|
||||
std::cerr << "-i needs a string of input characters\n";
|
||||
return 2;
|
||||
}
|
||||
for (const char *p = argv[++i]; *p; p++)
|
||||
input.push_back(uint16_t((unsigned char)*p & 0x7f));
|
||||
continue;
|
||||
}
|
||||
const size_t eq = a.find('=');
|
||||
if (eq == std::string::npos)
|
||||
continue;
|
||||
@@ -112,7 +126,12 @@ int main(int argc, char **argv) {
|
||||
case '.':
|
||||
out.push_back(cells[dp]);
|
||||
break;
|
||||
default: // ',' and anything else: not modelled
|
||||
case ',': // INP: the low 7 bits of the next scripted character
|
||||
cells[dp] = input.empty() ? 0 : input.front();
|
||||
if (!input.empty())
|
||||
input.pop_front();
|
||||
break;
|
||||
default: // anything else: not modelled
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user