All 161 task packages, including the 21 batch 6 C++17 tasks that passed
gates 1/2, are now in Git. rollout.catalog builds docs/task-catalog.{json,md}
from the task packages, a fresh trace5 calibration (128/128 PASS), every
verified controller receipt on the current task digest, documented holds
and digest-bound difficulty reviews. 123 tasks are usable (6 HARD),
5 are on hold and 33 still need a subprocess-only grader.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
317 lines
12 KiB
C++
317 lines
12 KiB
C++
#include "unit_specifiers.h"
|
|
#include <charconv>
|
|
#include <cstdint>
|
|
#include <iostream>
|
|
#include <limits>
|
|
#include <sstream>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace unit_specifiers;
|
|
|
|
namespace {
|
|
std::string word(std::istringstream& row) {
|
|
std::string value;
|
|
if (!(row >> value)) throw std::runtime_error("missing field");
|
|
return value;
|
|
}
|
|
|
|
void end(std::istringstream& row) {
|
|
std::string value;
|
|
if (row >> value) throw std::runtime_error("extra field");
|
|
}
|
|
|
|
unsigned nibble(char c) {
|
|
if (c >= '0' && c <= '9') return static_cast<unsigned>(c - '0');
|
|
if (c >= 'a' && c <= 'f') return static_cast<unsigned>(c - 'a' + 10);
|
|
if (c >= 'A' && c <= 'F') return static_cast<unsigned>(c - 'A' + 10);
|
|
throw std::runtime_error("hex digit");
|
|
}
|
|
|
|
std::string bytes(std::istringstream& row) {
|
|
const std::string text = word(row);
|
|
if (text == "-") return {};
|
|
if (text.size() % 2 != 0) throw std::runtime_error("hex length");
|
|
std::string value;
|
|
value.reserve(text.size() / 2);
|
|
for (std::size_t i = 0; i < text.size(); i += 2) {
|
|
const unsigned byte = nibble(text[i]) * 16 + nibble(text[i + 1]);
|
|
value.push_back(static_cast<char>(byte));
|
|
}
|
|
return value;
|
|
}
|
|
|
|
std::string hex(std::string_view value) {
|
|
static constexpr char digits[] = "0123456789abcdef";
|
|
std::string result;
|
|
result.reserve(value.size() * 2);
|
|
for (unsigned char byte : value) {
|
|
result.push_back(digits[byte >> 4]);
|
|
result.push_back(digits[byte & 15]);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int integer(std::istringstream& row) {
|
|
const std::string text = word(row);
|
|
int value = 0;
|
|
const auto parsed = std::from_chars(text.data(), text.data() + text.size(), value);
|
|
if (parsed.ec != std::errc{} || parsed.ptr != text.data() + text.size())
|
|
throw std::runtime_error("int32");
|
|
return value;
|
|
}
|
|
|
|
std::size_t size(std::istringstream& row) {
|
|
const std::string text = word(row);
|
|
std::uint64_t value = 0;
|
|
const auto parsed = std::from_chars(text.data(), text.data() + text.size(), value);
|
|
if (parsed.ec != std::errc{} || parsed.ptr != text.data() + text.size() ||
|
|
value > std::numeric_limits<std::size_t>::max())
|
|
throw std::runtime_error("size_t");
|
|
return static_cast<std::size_t>(value);
|
|
}
|
|
|
|
unsigned slot(std::istringstream& row) {
|
|
const std::size_t value = size(row);
|
|
if (value > 1) throw std::runtime_error("slot");
|
|
return static_cast<unsigned>(value);
|
|
}
|
|
|
|
void string_value(std::string_view value) {
|
|
std::cout << '"' << hex(value) << '"';
|
|
}
|
|
|
|
void boolean(bool value) {
|
|
std::cout << (value ? "true" : "false");
|
|
}
|
|
|
|
void view_value(const StreamView& value) {
|
|
std::cout << '[' << static_cast<int>(value.mode) << ','
|
|
<< static_cast<int>(value.phase) << ',' << value.capacity << ','
|
|
<< value.input_bytes << ',' << value.utf8_pending << ','
|
|
<< value.escape_pending << ',';
|
|
string_value(value.result);
|
|
std::cout << ']';
|
|
}
|
|
|
|
void service_value(const ServiceName& value) {
|
|
std::cout << '[';
|
|
string_value(value.unit);
|
|
std::cout << ',';
|
|
string_value(value.prefix);
|
|
std::cout << ',';
|
|
string_value(value.instance);
|
|
std::cout << ',';
|
|
string_value(value.decoded_prefix);
|
|
std::cout << ',';
|
|
string_value(value.decoded_instance);
|
|
std::cout << ',';
|
|
boolean(value.instantiated);
|
|
std::cout << ']';
|
|
}
|
|
|
|
void program_value(const Program& value) {
|
|
std::cout << '[';
|
|
bool separator = false;
|
|
for (const Piece& piece : value.pieces) {
|
|
if (separator) std::cout << ',';
|
|
separator = true;
|
|
std::cout << '[' << static_cast<int>(piece.kind) << ',';
|
|
string_value(piece.text);
|
|
std::cout << ']';
|
|
}
|
|
std::cout << ']';
|
|
}
|
|
|
|
bool same(const Program& left, const Program& right) {
|
|
if (left.pieces.size() != right.pieces.size()) return false;
|
|
for (std::size_t i = 0; i < left.pieces.size(); ++i) {
|
|
if (left.pieces[i].kind != right.pieces[i].kind ||
|
|
left.pieces[i].text != right.pieces[i].text) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
ServiceName dirty_service(const std::string& nonce) {
|
|
return {nonce, nonce, nonce, nonce, nonce, true};
|
|
}
|
|
|
|
void text_result(Error error, const std::string& output, bool unchanged) {
|
|
std::cout << '[' << static_cast<int>(error) << ',';
|
|
string_value(output);
|
|
std::cout << ',';
|
|
boolean(unchanged);
|
|
std::cout << ']';
|
|
}
|
|
} // namespace
|
|
|
|
int main() {
|
|
Stream streams[2];
|
|
StreamView held_view;
|
|
ServiceName held_service;
|
|
Program held_program;
|
|
std::string held_result;
|
|
std::string line;
|
|
try {
|
|
while (std::getline(std::cin, line)) {
|
|
std::istringstream row(line);
|
|
std::string command;
|
|
if (!(row >> command) || command.front() == '#') continue;
|
|
if (command == "V") {
|
|
const std::string text = bytes(row);
|
|
end(row);
|
|
const std::string before = text;
|
|
const bool result = valid_utf8(text);
|
|
std::cout << '[';
|
|
boolean(result);
|
|
std::cout << ',';
|
|
boolean(text == before);
|
|
std::cout << ']';
|
|
} else if (command == "E" || command == "U") {
|
|
const std::string text = bytes(row);
|
|
std::string output = bytes(row);
|
|
end(row);
|
|
const std::string before = text;
|
|
const Error error = command == "E" ? escape(text, output)
|
|
: unescape(text, output);
|
|
held_result = output;
|
|
text_result(error, output, text == before);
|
|
} else if (command == "N") {
|
|
const unsigned index = slot(row);
|
|
const Mode mode = static_cast<Mode>(integer(row));
|
|
const std::size_t capacity = size(row);
|
|
end(row);
|
|
const Error error = streams[index].reset(mode, capacity);
|
|
std::cout << '[' << static_cast<int>(error) << ',';
|
|
view_value(streams[index].view());
|
|
std::cout << ']';
|
|
} else if (command == "F") {
|
|
const unsigned index = slot(row);
|
|
const std::string chunk = bytes(row);
|
|
end(row);
|
|
const std::string before = chunk;
|
|
const Error error = streams[index].feed(chunk);
|
|
std::cout << '[' << static_cast<int>(error) << ',';
|
|
view_value(streams[index].view());
|
|
std::cout << ',';
|
|
boolean(chunk == before);
|
|
std::cout << ']';
|
|
} else if (command == "D") {
|
|
const unsigned index = slot(row);
|
|
end(row);
|
|
const Error error = streams[index].finish();
|
|
std::cout << '[' << static_cast<int>(error) << ',';
|
|
view_value(streams[index].view());
|
|
std::cout << ']';
|
|
} else if (command == "Q") {
|
|
const unsigned index = slot(row);
|
|
end(row);
|
|
held_view = streams[index].view();
|
|
view_value(held_view);
|
|
} else if (command == "Y") {
|
|
const unsigned index = slot(row);
|
|
const std::string nonce = bytes(row);
|
|
end(row);
|
|
StreamView copied = streams[index].view();
|
|
copied.mode = static_cast<Mode>(17);
|
|
copied.phase = static_cast<Phase>(18);
|
|
copied.capacity = 19;
|
|
copied.input_bytes = 20;
|
|
copied.utf8_pending = 21;
|
|
copied.escape_pending = 22;
|
|
copied.result = nonce;
|
|
std::cout << '[';
|
|
view_value(copied);
|
|
std::cout << ',';
|
|
view_value(streams[index].view());
|
|
std::cout << ']';
|
|
} else if (command == "P") {
|
|
const std::string unit = bytes(row);
|
|
const std::string nonce = bytes(row);
|
|
end(row);
|
|
const std::string before = unit;
|
|
ServiceName output = dirty_service(nonce);
|
|
const Error error = parse_unit(unit, output);
|
|
held_service = output;
|
|
std::cout << '[' << static_cast<int>(error) << ',';
|
|
service_value(output);
|
|
std::cout << ',';
|
|
boolean(unit == before);
|
|
std::cout << ']';
|
|
} else if (command == "C") {
|
|
const std::string text = bytes(row);
|
|
const std::string nonce = bytes(row);
|
|
end(row);
|
|
const std::string before = text;
|
|
Program output;
|
|
output.pieces.push_back({PieceKind::decoded_instance, nonce});
|
|
const Error error = compile_template(text, output);
|
|
held_program = output;
|
|
std::cout << '[' << static_cast<int>(error) << ',';
|
|
program_value(output);
|
|
std::cout << ',';
|
|
boolean(text == before);
|
|
std::cout << ']';
|
|
} else if (command == "R") {
|
|
const std::size_t count = size(row);
|
|
if (count > 8192) throw std::runtime_error("wire count");
|
|
Program program;
|
|
for (std::size_t i = 0; i < count; ++i) {
|
|
const PieceKind kind = static_cast<PieceKind>(integer(row));
|
|
program.pieces.push_back({kind, bytes(row)});
|
|
}
|
|
const std::string unit = bytes(row);
|
|
const std::size_t capacity = size(row);
|
|
std::string output = bytes(row);
|
|
end(row);
|
|
const Program before = program;
|
|
const std::string unit_before = unit;
|
|
const Error error = render(program, unit, capacity, output);
|
|
held_result = output;
|
|
text_result(error, output, unit == unit_before && same(program, before));
|
|
} else if (command == "T") {
|
|
const std::string unit = bytes(row);
|
|
const std::size_t capacity = size(row);
|
|
std::string output = bytes(row);
|
|
end(row);
|
|
const Program before = held_program;
|
|
const std::string unit_before = unit;
|
|
const Error error = render(held_program, unit, capacity, output);
|
|
held_result = output;
|
|
text_result(error, output,
|
|
unit == unit_before && same(held_program, before));
|
|
} else if (command == "X") {
|
|
const std::string text = bytes(row);
|
|
const std::string unit = bytes(row);
|
|
const std::size_t capacity = size(row);
|
|
std::string output = bytes(row);
|
|
end(row);
|
|
const std::string text_before = text;
|
|
const std::string unit_before = unit;
|
|
const Error error = expand(text, unit, capacity, output);
|
|
held_result = output;
|
|
text_result(error, output, text == text_before && unit == unit_before);
|
|
} else if (command == "H") {
|
|
end(row);
|
|
std::cout << '[';
|
|
view_value(held_view);
|
|
std::cout << ',';
|
|
service_value(held_service);
|
|
std::cout << ',';
|
|
program_value(held_program);
|
|
std::cout << ',';
|
|
string_value(held_result);
|
|
std::cout << ']';
|
|
} else {
|
|
throw std::runtime_error("command");
|
|
}
|
|
std::cout << '\n';
|
|
}
|
|
} catch (const std::exception& error) {
|
|
std::cerr << error.what() << '\n';
|
|
return 2;
|
|
}
|
|
return std::cin.bad() || !std::cout ? 2 : 0;
|
|
}
|