Splice %synth into the tape, and check it on a ROT-8 machine

The synthesised body is written out as a macro file and imported, so the
generated code goes through the ordinary expansion path instead of being
spliced by hand.  set_1 is spelled out as set_0 plus flb, so the generated file
needs nothing from lib.r8asm.

examples/full_add.r8asm now assembles: 7.7 seconds of search at assembly time,
then a 95 character tape.  tests/synth_test.cc runs that tape on a small ROT-8
machine for all eight input combinations and checks S and Co -- all eight pass.

Two things that cost me time and are worth writing down:

  * the opcode map is "><+-[].,", i.e. '-' is flb and '+' is ror.  Guess it the
    other way round and a perfectly good tape looks like an infinite loop.
  * synth names must not collide with mnemonics: 'out' is lexed as rot8_out
    before it can be a block keyword.
This commit is contained in:
starcells committed 2026-11-06 00:08:29 +08:00
1 parent 6d0b6270fd
commit bc9d873cfc
5 files changed
+221 -56

No files matched your search

+26 -11
View File
@@ -1,4 +1,7 @@
#include "r8forge/r8asm.hh"
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <algorithm>
#include <cstddef>
#include <iostream>
@@ -470,21 +473,33 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
}
auto prog = r8asm_synth_program(spec.params, spec.in, spec.out,
spec.map, name);
if (prog.empty())
if (prog.empty()) {
main_tree.fail("%synth '" + name + "': nothing emitted");
else {
// TODO: splice this into the tape. For now the synthesised body is
// printed (ready to paste) and the call site is left undefined, so
// nothing silently assembles to the wrong thing.
std::cerr << "r8asm: %synth " << name << " body:\n";
} else {
// Write the synthesised body out as a macro and import it: the code
// then goes through the normal, well tested expansion path instead
// of being spliced by hand. set_1 is spelled out (set_0 + flb) so
// the generated file needs nothing from lib.r8asm.
const std::string path = (std::filesystem::temp_directory_path() /
(name + ".synth.r8asm"))
.string();
std::ofstream file(path);
file << "%macro " << name << " " << spec.params << "\n";
for (const auto &i : prog) {
std::cerr << "\t.tp %" << i[0] + 1 << "\n\tbiz\n";
file << ".tp %" << i[0] + 1 << "\nbiz\n";
for (int b = 0; b < 8; b++)
if (i[1] & (1 << b))
std::cerr << "\t\t.tp %" << b + 1 << "\n\t\t"
<< (i[2] ? "set_1" : "flb") << "\n";
std::cerr << "\t.tp %" << i[0] + 1 << "\n\tflb\n\trnz\n";
if (i[1] & (1 << b)) {
file << ".tp %" << b + 1 << "\n";
if (i[2])
file << "biz\nflb\nrnz\n"; // set_0
file << "flb\n";
}
file << ".tp %" << i[0] + 1 << "\nflb\nrnz\n";
}
file << "%endmacro\n";
file.close();
import_file(path, main_tree.CELL_SIZE);
std::remove(path.c_str());
}
} break;
default: