One user-space repair task per eligible HWE debug-diag / compile-deploy base ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content filter. Each ships a public spec with a check-to-basis table, at least two defects with single-fix fixtures, a symptom-masking fixture and an alternative correct implementation. Claude re-checked all 320: structure, the HWE quality items and trace6 Docker calibration receipts against current digests (320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120 ad-hoc family names; the next batch maps them to the canonical list. The batch's 1,256-line progress log stays in its factory report and the 36-local branch batch15-hwe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
159 lines
4.0 KiB
C++
159 lines
4.0 KiB
C++
#include "ledger.hpp"
|
|
#include <climits>
|
|
#include <istream>
|
|
#include <ostream>
|
|
|
|
namespace scoped {
|
|
namespace {
|
|
bool horizontal(char value) {
|
|
return value == ' ' || value == '\t' || value == '\r';
|
|
}
|
|
|
|
bool decimal(const std::string& token, int& output) {
|
|
if (token.empty()) {
|
|
return false;
|
|
}
|
|
std::size_t cursor = 0;
|
|
bool negative = false;
|
|
if (token[cursor] == '+' || token[cursor] == '-') {
|
|
negative = token[cursor] == '-';
|
|
++cursor;
|
|
}
|
|
if (cursor == token.size()) {
|
|
return false;
|
|
}
|
|
const unsigned long long maximum = negative
|
|
? static_cast<unsigned long long>(INT_MAX) + 1
|
|
: static_cast<unsigned long long>(INT_MAX);
|
|
unsigned long long value = 0;
|
|
for (; cursor < token.size(); ++cursor) {
|
|
const char character = token[cursor];
|
|
if (character < '0' || character > '9') {
|
|
return false;
|
|
}
|
|
const unsigned digit = static_cast<unsigned>(character - '0');
|
|
if (value > (maximum - digit) / 10) {
|
|
return false;
|
|
}
|
|
value = value * 10 + digit;
|
|
}
|
|
if (negative && value == static_cast<unsigned long long>(INT_MAX) + 1) {
|
|
output = INT_MIN;
|
|
} else {
|
|
output = negative ? -static_cast<int>(value) : static_cast<int>(value);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int arity(char kind) {
|
|
switch (kind) {
|
|
case 'R':
|
|
case 'Q':
|
|
return 0;
|
|
case 'D':
|
|
case 'L':
|
|
case 'X':
|
|
return 1;
|
|
case 'M':
|
|
case 'W':
|
|
case 'F':
|
|
return 2;
|
|
case 'A':
|
|
return 3;
|
|
case 'U':
|
|
return 4;
|
|
default:
|
|
return -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
Command parse_command(const std::string& line) {
|
|
Command command;
|
|
std::vector<std::string> tokens;
|
|
std::size_t cursor = 0;
|
|
while (cursor < line.size()) {
|
|
while (cursor < line.size() && horizontal(line[cursor])) {
|
|
++cursor;
|
|
}
|
|
if (cursor == line.size()) {
|
|
break;
|
|
}
|
|
const auto begin = cursor;
|
|
while (cursor < line.size() && !horizontal(line[cursor])) {
|
|
++cursor;
|
|
}
|
|
tokens.push_back(line.substr(begin, cursor - begin));
|
|
}
|
|
if (tokens.empty() || tokens.front().size() != 1) {
|
|
return command;
|
|
}
|
|
command.kind = tokens.front()[0];
|
|
const int expected = arity(command.kind);
|
|
if (expected < 0 || tokens.size() != static_cast<std::size_t>(expected + 1)) {
|
|
return command;
|
|
}
|
|
for (std::size_t index = 1; index < tokens.size(); ++index) {
|
|
int value = 0;
|
|
if (!decimal(tokens[index], value)) {
|
|
return command;
|
|
}
|
|
command.values.push_back(value);
|
|
}
|
|
command.valid = true;
|
|
return command;
|
|
}
|
|
|
|
Result dispatch(Ledger& ledger, const Command& command) {
|
|
if (!command.valid) {
|
|
Result result;
|
|
result.code = -1;
|
|
return result;
|
|
}
|
|
const auto& values = command.values;
|
|
switch (command.kind) {
|
|
case 'R':
|
|
ledger.reset();
|
|
return Result{};
|
|
case 'Q':
|
|
return Result{};
|
|
case 'A':
|
|
return ledger.add(values[0], values[1], values[2]);
|
|
case 'M':
|
|
return ledger.move(values[0], values[1]);
|
|
case 'W':
|
|
return ledger.set_weight(values[0], values[1]);
|
|
case 'D':
|
|
return ledger.erase(values[0]);
|
|
case 'F':
|
|
return ledger.report(values[0], values[1]);
|
|
case 'U':
|
|
return ledger.adjust(values[0], values[1], values[2], values[3]);
|
|
case 'L':
|
|
case 'X': {
|
|
const bool accepted = command.kind == 'L'
|
|
? ledger.gate().acquire(values[0])
|
|
: ledger.gate().release(values[0]);
|
|
Result result;
|
|
result.code = accepted ? 0 : -2;
|
|
return result;
|
|
}
|
|
default: {
|
|
Result result;
|
|
result.code = -1;
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|
|
int run_wire(std::istream& input, std::ostream& output) {
|
|
Ledger ledger;
|
|
std::string line;
|
|
while (std::getline(input, line)) {
|
|
const auto result = dispatch(ledger, parse_command(line));
|
|
observe(output, result, ledger);
|
|
}
|
|
return 0;
|
|
}
|
|
}
|