SPEC is the contract and drv/ is what the tests sweep, but there was no place
that showed a reader how a group is actually used. examples/ now has one stand
alone program per group, each ending in output calls:
base_macros.r8asm S5 set_0 vs erase, rol/ror, copy vs bit_copy vs
clb_bit_mov, step
bit_adder.r8asm S6 4 bit ripple carry adder built from
clb_bit_add_nc, then bit_raw_add for the
preserving variant
byte_raw_ops.r8asm S7.1 raw_add/raw_inc/raw_and/raw_or/raw_xor/raw_sub,
printing the operands afterwards to show the
whole point of the family: they survive
byte_clobber.r8asm S7.2 clb_add/sub/neg/inc_nc (operands eaten) and the
S7.3 clb_and residue (240 & 204 -> B = 12)
byte_in_place.r8asm S8 not/inc/neg/dec/add_ip and clb_nonzero
sum_loop.r8asm S5.1 clb_ones + loop/endloop: 1+2+...+N, count from
data rather than from the assembler
mul_demo.r8asm S9 82*141 and 2047*2047
divmod_demo.r8asm S7.6 2047/10 and 2047/1 (the case that used to hang),
reusing the scratch block on purpose
digits_demo.r8asm S7.7 1234 and 7 -> four ASCII digits
Every header carries a `; out 1 2 3` line: the expected OUT stream, so the
file itself says what it prints. The new r8forge_examples_test target runs all
of examples/*.r8asm and compares that line with the real output (a file without
the marker counts as a failure), printing the tape length and step count on the
way. That makes examples the second place after byte_op_test that checks
behaviour instead of just "it assembles", and keeps the examples from rotting as
the macros change. The six %synth examples get the marker too: their inputs are
all zero, so they print 0 0.
All 15 examples pass. The values in the headers were checked with
r8forge_tape_run (the second interpreter) as well as the test's own machine.
Docs: SPEC S10.3 has the table and the convention, S11 gets the test row, and
README gets a short section plus the layout list. AGENTS records the examples
in the handoff baseline and the "verified" table.
197 lines
5.8 KiB
C++
197 lines
5.8 KiB
C++
// Runs every program in examples/ and compares its OUT stream with the one
|
|
// written in the file header:
|
|
//
|
|
// ; out 254 0 1024 ...
|
|
//
|
|
// The marker is part of the example's documentation -- a reader sees at a
|
|
// glance what the program prints -- and this test is what keeps the doc true:
|
|
// an example that stops printing what its header says is a broken example, and
|
|
// one that does not assemble at all would otherwise rot silently.
|
|
//
|
|
// It is also the only place that runs examples/*.r8asm: the %synth ones are
|
|
// covered combination by combination by r8forge_synth_test, but every file in
|
|
// examples/ is still a stand alone program, so it has to keep running.
|
|
//
|
|
// r8forge_examples_test [examples dir] default: examples
|
|
#include "r8forge/r8asm.hh"
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
struct machine {
|
|
std::vector<uint16_t> cells = std::vector<uint16_t>(kDefaultMemoryCells, 0);
|
|
std::vector<uint16_t> out;
|
|
size_t dp = 0;
|
|
size_t steps = 0;
|
|
bool finished = true; // false: the step limit ran out
|
|
|
|
static uint16_t ror(uint16_t v) {
|
|
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
|
|
}
|
|
|
|
void run(const std::string &tape, size_t limit = 5'000'000) {
|
|
for (size_t pc = 0; pc < tape.size(); pc++) {
|
|
if (++steps > limit) {
|
|
finished = false;
|
|
return;
|
|
}
|
|
switch (tape[pc]) {
|
|
case '>':
|
|
dp = (dp + 1) % cells.size();
|
|
break;
|
|
case '<':
|
|
dp = (dp + cells.size() - 1) % cells.size();
|
|
break;
|
|
case '-': // flb
|
|
cells[dp] ^= 1;
|
|
break;
|
|
case '+': // ror
|
|
cells[dp] = ror(cells[dp]);
|
|
break;
|
|
// biz / rnz test the LOW BIT, not "is the cell zero" (ROT-8-viz
|
|
// src/rot8.c). Reading them as a Brainfuck style zero test makes every
|
|
// byte level program look like an endless loop.
|
|
case '[':
|
|
if ((cells[dp] & 1) == 0) {
|
|
int depth = 1;
|
|
while (depth) {
|
|
pc++;
|
|
if (tape[pc] == '[')
|
|
depth++;
|
|
else if (tape[pc] == ']')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case ']':
|
|
if ((cells[dp] & 1) == 1) {
|
|
int depth = 1;
|
|
while (depth) {
|
|
pc--;
|
|
if (tape[pc] == ']')
|
|
depth++;
|
|
else if (tape[pc] == '[')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case '.':
|
|
out.push_back(cells[dp]);
|
|
break;
|
|
default: // ',' (input) is not modelled
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
// import_file() only reports what the parser found; an unknown macro is found
|
|
// while the tree is expanded, and a truncated tape still runs and prints
|
|
// something plausible. Same order as r8forge's own main.
|
|
bool assemble(const std::filesystem::path &file, std::string &tape) {
|
|
r8asm assembler;
|
|
if (!assembler.import_file(file.string(), kDefaultCellSize)) {
|
|
std::cerr << " could not assemble " << file.string() << "\n";
|
|
return false;
|
|
}
|
|
tape = assembler.export_rot8();
|
|
if (assembler.failed()) {
|
|
std::cerr << " could not assemble " << file.string() << "\n";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// The `; out 1 2 3` line in the header. Returns false when it is missing or
|
|
// carries no numbers -- the marker is required, not optional.
|
|
bool expected_out(const std::filesystem::path &file, std::vector<int> &want) {
|
|
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;
|
|
std::istringstream rest(line.substr(at + 1));
|
|
std::string word;
|
|
if (!(rest >> word) || word != "out")
|
|
continue;
|
|
std::vector<int> values;
|
|
int v;
|
|
while (rest >> v)
|
|
values.push_back(v);
|
|
if (values.empty())
|
|
continue;
|
|
want = values;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::string join(const std::vector<int> &v) {
|
|
std::ostringstream os;
|
|
for (size_t i = 0; i < v.size(); i++)
|
|
os << (i ? " " : "") << v[i];
|
|
return os.str();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char **argv) {
|
|
const std::filesystem::path dir = argc > 1 ? argv[1] : "examples";
|
|
std::error_code ec;
|
|
std::vector<std::filesystem::path> files;
|
|
for (const auto &entry : std::filesystem::directory_iterator(dir, ec)) {
|
|
if (entry.path().extension() == ".r8asm")
|
|
files.push_back(entry.path());
|
|
}
|
|
if (ec || files.empty()) {
|
|
std::cerr << "cannot read " << dir.string() << " (" << ec.message() << ")\n";
|
|
return 2;
|
|
}
|
|
std::sort(files.begin(), files.end());
|
|
|
|
int failures = 0;
|
|
for (const std::filesystem::path &file : files) {
|
|
const std::string name = file.filename().string();
|
|
std::vector<int> want;
|
|
if (!expected_out(file, want)) {
|
|
std::cout << name << ": no '; out ...' line in the header\n";
|
|
failures++;
|
|
continue;
|
|
}
|
|
std::string tape;
|
|
if (!assemble(file, tape)) {
|
|
failures++;
|
|
continue;
|
|
}
|
|
machine m;
|
|
m.run(tape);
|
|
std::vector<int> got;
|
|
for (uint16_t v : m.out)
|
|
got.push_back(int(v));
|
|
const bool ok = m.finished && got == want;
|
|
std::cout << name << ": " << tape.size() << " characters, " << m.steps
|
|
<< " steps, out " << join(got) << (ok ? " ok" : " FAILED");
|
|
if (!ok) {
|
|
std::cout << " (want " << join(want) << (m.finished ? "" : ", HANG") << ")";
|
|
failures++;
|
|
}
|
|
std::cout << "\n";
|
|
}
|
|
|
|
std::cout << (failures ? "FAILED: " + std::to_string(failures) + " example(s)\n"
|
|
: "all " + std::to_string(files.size()) +
|
|
" examples print what their headers say\n");
|
|
return failures ? 1 : 0;
|
|
}
|