100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth range given to batch 22), each with at least one independent contract-edge defect and plain public docs. Claude reviewed 100/100 USABLE against current digests; no ID or source overlap with main or batch 22. Difficulty is unmeasured. progress.md records the wrap-up: all Codex sessions and Qwen schedulers stopped for the user's rollout, Qwen results so far (57 HARD of 373), and what is left on each batch branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
158 lines
4.2 KiB
C++
158 lines
4.2 KiB
C++
#include "handshake.hpp"
|
|
#include <array>
|
|
#include <limits>
|
|
#include <sstream>
|
|
namespace readyack {
|
|
namespace {
|
|
bool separator(char value)
|
|
{
|
|
return value == ' ' || value == '\t' || value == '\r';
|
|
}
|
|
bool tokenize(const std::string& line, std::vector<std::string>& tokens)
|
|
{
|
|
if (line.size() > 1024) {
|
|
return false;
|
|
}
|
|
std::size_t offset = 0;
|
|
while (offset < line.size()) {
|
|
while (offset < line.size() && separator(line[offset])) {
|
|
++offset;
|
|
}
|
|
if (offset == line.size()) {
|
|
break;
|
|
}
|
|
std::size_t start = offset;
|
|
while (offset < line.size() && !separator(line[offset])) {
|
|
unsigned char value = static_cast<unsigned char>(line[offset]);
|
|
if (value < 33 || value > 126) {
|
|
return false;
|
|
}
|
|
++offset;
|
|
}
|
|
tokens.push_back(line.substr(start, offset - start));
|
|
if (tokens.size() > 5) {
|
|
return false;
|
|
}
|
|
}
|
|
return !tokens.empty();
|
|
}
|
|
bool number(const std::string& token, Number& output)
|
|
{
|
|
if (token.empty()) {
|
|
return false;
|
|
}
|
|
bool negative = token[0] == '-';
|
|
std::size_t offset = negative ? 1 : 0;
|
|
if (offset == token.size()) {
|
|
return false;
|
|
}
|
|
std::uint64_t value = 0;
|
|
std::uint64_t limit = static_cast<std::uint64_t>(std::numeric_limits<Number>::max());
|
|
if (negative) {
|
|
++limit;
|
|
}
|
|
for (; offset < token.size(); ++offset) {
|
|
char ch = token[offset];
|
|
if (ch < '0' || ch > '9') {
|
|
return false;
|
|
}
|
|
unsigned digit = static_cast<unsigned>(ch - '0');
|
|
if (value > (limit - digit) / 10) {
|
|
return false;
|
|
}
|
|
value = value * 10 + digit;
|
|
}
|
|
if (!negative) {
|
|
output = static_cast<Number>(value);
|
|
} else if (value == limit) {
|
|
output = std::numeric_limits<Number>::min();
|
|
} else {
|
|
output = -static_cast<Number>(value);
|
|
}
|
|
return true;
|
|
}
|
|
int arity(const std::string& verb)
|
|
{
|
|
if (verb == "C" || verb == "W") {
|
|
return 4;
|
|
}
|
|
if (verb == "P" || verb == "V" || verb == "T" || verb == "R") {
|
|
return 3;
|
|
}
|
|
if (verb == "D" || verb == "S" || verb == "M" || verb == "Q" || verb == "I") {
|
|
return 2;
|
|
}
|
|
if (verb == "K") {
|
|
return 1;
|
|
}
|
|
return -1;
|
|
}
|
|
Result invoke(System& system, const std::string& verb, const std::array<Number, 4>& args)
|
|
{
|
|
if (verb == "C") {
|
|
return system.configure(args[0], args[1], args[2], args[3]);
|
|
}
|
|
if (verb == "W") {
|
|
return system.stage(args[0], args[1], args[2], args[3]);
|
|
}
|
|
if (verb == "P") {
|
|
return system.publish(args[0], args[1], args[2]);
|
|
}
|
|
if (verb == "D") {
|
|
return system.withdraw(args[0], args[1]);
|
|
}
|
|
if (verb == "S") {
|
|
return system.step(args[0], args[1]);
|
|
}
|
|
if (verb == "M") {
|
|
return system.set_mask(args[0], args[1]);
|
|
}
|
|
if (verb == "K") {
|
|
return system.get_mask(args[0]);
|
|
}
|
|
if (verb == "V") {
|
|
return system.word(args[0], args[1], args[2]);
|
|
}
|
|
if (verb == "Q") {
|
|
return system.peek(args[0], args[1]);
|
|
}
|
|
if (verb == "T") {
|
|
return system.drain(args[0], args[1], args[2]);
|
|
}
|
|
if (verb == "I") {
|
|
return system.inspect(args[0], args[1]);
|
|
}
|
|
return system.reset(args[0], args[1], args[2]);
|
|
}
|
|
std::string render(const Result& result)
|
|
{
|
|
std::ostringstream stream;
|
|
stream << "R " << result.code;
|
|
for (Number value : result.values) {
|
|
stream << ' ' << value;
|
|
}
|
|
stream << '\n';
|
|
return stream.str();
|
|
}
|
|
}
|
|
std::string execute_line(System& system, const std::string& line)
|
|
{
|
|
std::vector<std::string> tokens;
|
|
if (!tokenize(line, tokens)) {
|
|
return "R 8\n";
|
|
}
|
|
int required = arity(tokens[0]);
|
|
if (required < 0 || tokens.size() != static_cast<std::size_t>(required + 1)) {
|
|
return "R 8\n";
|
|
}
|
|
std::array<Number, 4> args{};
|
|
for (int index = 0; index < required; ++index) {
|
|
if (!number(tokens[static_cast<std::size_t>(index + 1)], args[static_cast<std::size_t>(index)])) {
|
|
return "R 8\n";
|
|
}
|
|
}
|
|
Result result = invoke(system, tokens[0], args);
|
|
return render(result) + "\n";
|
|
}
|
|
}
|