Add a mode switch and a getting-started section

%synth used to always run both searches, so every assembly of the full adder
sat there for twenty minutes.  A block can now say 'mode layers' to stop after
the layer search (7.7 seconds) or 'mode tape' (the default) to pay for the
shortest tape.  examples/full_add_layers.r8asm is the fast one, 95 characters
and still checked on the machine.

The lexer also no longer chokes on a UTF-8 BOM.  Every Windows editor writes
one, and the first token came out as a garbage prefix, which crashed the
assembler with an access violation rather than saying anything useful.

README grew a getting-started walkthrough: build, a complete adder.r8asm, what
the two stderr lines mean, how to drop a synth block into a library, and the
three things that bite (inputs first, don't call them in/out, map length).
This commit is contained in:
starcells committed 2026-11-06 10:14:58 +08:00
1 parent 388c2b7dbd
commit 6fcdc4a1ea
5 files changed
+129 -12

No files matched your search

+19 -1
View File
@@ -464,6 +464,14 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
std::vector<int> &dst = (kw == "inputs") ? spec.in : spec.out;
for (size_t i = 1; i < it->tokens.size(); i++)
dst.push_back(std::stoi(it->tokens[i].value) - 1);
} else if (kw == "mode") {
const std::string &m = it->tokens[1].value;
if (m == "layers")
spec.mode = 1;
else if (m == "tape")
spec.mode = 0;
else
main_tree.fail("unknown mode '" + m + "'");
} else if (kw == "layout") {
for (size_t i = 1; i < it->tokens.size(); i++)
spec.layout.push_back(std::stoi(it->tokens[i].value));
@@ -475,7 +483,8 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
}
}
auto prog = r8asm_synth_program(spec.params, spec.in, spec.out,
spec.map, name, spec.layout);
spec.map, name, spec.layout,
spec.mode);
if (prog.empty()) {
main_tree.fail("%synth '" + name + "': nothing emitted");
} else {
@@ -727,7 +736,16 @@ r8asm::token_stream::lex(const std::string &char_stream) {
{"&", "op_and"}, {"|", "op_or"}, {"~", "op_not"}, {"-", "op_sub"},
};
bool first_line = true;
while (std::getline(iss, str_buffer)) {
// a UTF-8 BOM would otherwise end up glued to the first token and every
// editor on Windows writes one
if (first_line && str_buffer.size() >= 3 &&
(unsigned char)str_buffer[0] == 0xEF &&
(unsigned char)str_buffer[1] == 0xBB &&
(unsigned char)str_buffer[2] == 0xBF)
str_buffer.erase(0, 3);
first_line = false;
std::vector<token> token_buffer = {};
bool start_of_line = true;
bool lquote_appeared = false;
+16 -10
View File
@@ -12,7 +12,7 @@ template <int P>
std::vector<std::array<int, 3>>
r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
const std::vector<int> &map, const std::string &name,
const std::vector<int> &layout) {
const std::vector<int> &layout, int mode) {
using V = r8veritas<P>;
V v;
v.initialize_nicknames();
@@ -50,6 +50,12 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
<< " layers" << (layers.proven_optimal ? " (shortest)" : "")
<< ", " << layers.states << " states, " << layers.seconds << "s\n";
if (mode == 1) { // fewest layers only: skip the tape search
for (const auto &i : layers.program)
out_ins.push_back({int(i.sacrifice), int(i.mask), int(i.or_mode)});
return out_ins;
}
// 2) shortest tape -- exact, so it stops as soon as the goal is popped
typename V::cost_options copt;
copt.max_states = 450'000'000;
@@ -97,7 +103,7 @@ std::vector<std::array<int, 3>>
r8asm_synth_program(int params, const std::vector<int> &in,
const std::vector<int> &out, const std::vector<int> &map,
const std::string &name,
const std::vector<int> &layout) {
const std::vector<int> &layout, int mode) {
if (params < 1 || params > 8) {
std::cerr << "r8asm: %synth " << name << ": supports 1..8 parameters\n";
return {};
@@ -115,21 +121,21 @@ r8asm_synth_program(int params, const std::vector<int> &in,
}
switch (params) {
case 1:
return r8asm_synth_run<1>(in, out, map, name, layout);
return r8asm_synth_run<1>(in, out, map, name, layout, mode);
case 2:
return r8asm_synth_run<2>(in, out, map, name, layout);
return r8asm_synth_run<2>(in, out, map, name, layout, mode);
case 3:
return r8asm_synth_run<3>(in, out, map, name, layout);
return r8asm_synth_run<3>(in, out, map, name, layout, mode);
case 4:
return r8asm_synth_run<4>(in, out, map, name, layout);
return r8asm_synth_run<4>(in, out, map, name, layout, mode);
case 5:
return r8asm_synth_run<5>(in, out, map, name, layout);
return r8asm_synth_run<5>(in, out, map, name, layout, mode);
case 6:
return r8asm_synth_run<6>(in, out, map, name, layout);
return r8asm_synth_run<6>(in, out, map, name, layout, mode);
case 7:
return r8asm_synth_run<7>(in, out, map, name, layout);
return r8asm_synth_run<7>(in, out, map, name, layout, mode);
default:
return r8asm_synth_run<8>(in, out, map, name, layout);
return r8asm_synth_run<8>(in, out, map, name, layout, mode);
}
}