5 million steps was picked when the worst macro (clb_add_ip with B=2047) sat just under a million, so it caught hangs quickly. The interactive programs changed the arithmetic: one printed line of the desk calculator costs about 225k steps (40k for clb_undigits, up to 32k for the operation, ~141k for clb_digits), which is only about twenty lines before the cap, and examples_test applies the same cap to every example. At roughly 2.3e8 steps per second a 20M cap costs 0.09 s when a program really does run away, and byte_op_test (the only place that runs 465k programs) is unchanged -- so the safety net stays cheap while a session gets four times the room. docs/calc.md, docs/divmod.md, docs/byte-ops.md and SPEC's "500 ?" mentions now say 2000 ?.
146 lines
4.5 KiB
C++
146 lines
4.5 KiB
C++
#include "r8forge/geometry.hh"
|
|
// 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> [-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.
|
|
//
|
|
// 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>
|
|
#include <vector>
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc < 2) {
|
|
std::cerr << "usage: r8forge_tape_run <file.rot8> [addr=value ...]\n";
|
|
return 2;
|
|
}
|
|
std::ifstream in(argv[1], std::ios::binary);
|
|
if (!in) {
|
|
std::cerr << "could not open " << argv[1] << "\n";
|
|
return 1;
|
|
}
|
|
const std::string tape((std::istreambuf_iterator<char>(in)),
|
|
std::istreambuf_iterator<char>());
|
|
|
|
std::vector<uint32_t> cells(kDefaultMemoryCells, 0);
|
|
std::deque<uint32_t> input;
|
|
bool trace = false;
|
|
for (int i = 2; i < argc; i++) {
|
|
const std::string a = argv[i];
|
|
if (a == "--trace" || a == "-t") {
|
|
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(uint32_t((unsigned char)*p & 0x7f));
|
|
continue;
|
|
}
|
|
const size_t eq = a.find('=');
|
|
if (eq == std::string::npos)
|
|
continue;
|
|
const size_t at = size_t(std::stoul(a.substr(0, eq)));
|
|
cells.at(at) = uint32_t(std::stoul(a.substr(eq + 1)) & kDefaultCellMask);
|
|
}
|
|
|
|
std::vector<uint32_t> out;
|
|
size_t dp = 0;
|
|
const size_t limit = 20'000'000; // same as r8forge_derive
|
|
size_t pc = 0;
|
|
size_t steps = 0;
|
|
for (; pc < tape.size(); pc++) {
|
|
if (++steps > limit) {
|
|
std::cout << "HANG after " << limit << " steps (pc " << pc << ", dp " << dp
|
|
<< ", cell=0x" << std::hex << int(cells[dp]) << std::dec
|
|
<< ")\n";
|
|
return 3;
|
|
}
|
|
if (trace) {
|
|
// cells 0..3 are the interesting ones when debugging a library macro
|
|
std::cout << "pc " << pc << " " << tape[pc] << " dp " << dp << " [";
|
|
for (size_t i = 0; i < 4; i++)
|
|
std::cout << (i ? " " : "") << int(cells[i]);
|
|
std::cout << "]\n";
|
|
}
|
|
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] = uint32_t((cells[dp] >> 1) | ((cells[dp] & 1u) << (kDefaultCellSize - 1)));
|
|
break;
|
|
// biz / rnz test the LOW BIT, not "is the cell zero" -- see ROT-8-viz // src/rot8.c. Reading them as a Brainfuck style zero test makes every
|
|
// byte level program in the library look like an endless loop.
|
|
case '[':
|
|
if ((cells[dp] & 1) == 0) {
|
|
for (int depth = 1; depth;) {
|
|
pc++;
|
|
if (pc >= tape.size()) {
|
|
std::cout << "unbalanced '['\n";
|
|
return 4;
|
|
}
|
|
if (tape[pc] == '[')
|
|
depth++;
|
|
else if (tape[pc] == ']')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case ']':
|
|
if ((cells[dp] & 1) == 1) {
|
|
for (int depth = 1; depth;) {
|
|
if (pc == 0) {
|
|
std::cout << "unbalanced ']'\n";
|
|
return 4;
|
|
}
|
|
pc--;
|
|
if (tape[pc] == ']')
|
|
depth++;
|
|
else if (tape[pc] == '[')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case '.':
|
|
out.push_back(cells[dp]);
|
|
break;
|
|
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;
|
|
}
|
|
}
|
|
|
|
std::cout << "out";
|
|
for (uint32_t v : out)
|
|
std::cout << " " << int(v);
|
|
std::cout << " (" << steps << " steps, cell0 ended 0x" << std::hex
|
|
<< int(cells[0]) << std::dec << ")\n";
|
|
return 0;
|
|
}
|