amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

144 lines
4.0 KiB
C++

#include "activation.hpp"
#include <array>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
namespace {
using activation::Command;
using activation::Op;
bool integer(const std::string& text, int& value)
{
if (text.empty()) {
return false;
}
std::size_t cursor = 0;
if (text[0] == '-' || text[0] == '+') {
cursor = 1;
}
if (cursor == text.size()) {
return false;
}
for (std::size_t index = cursor; index < text.size(); ++index) {
if (text[index] < '0' || text[index] > '9') {
return false;
}
}
try {
const long long parsed = std::stoll(text);
if (parsed < std::numeric_limits<int>::min() ||
parsed > std::numeric_limits<int>::max()) {
return false;
}
value = static_cast<int>(parsed);
return true;
} catch (...) {
return false;
}
}
Command parse(const std::vector<std::string>& words)
{
Command command;
struct Entry {
const char* word;
Op op;
int arguments;
};
const std::array<Entry, 11> entries = {{
{"open", Op::open, 2},
{"close", Op::close, 1},
{"reserve", Op::reserve, 2},
{"gate", Op::gate, 2},
{"ungate", Op::ungate, 2},
{"release", Op::release, 2},
{"activate", Op::activate, 2},
{"deactivate", Op::deactivate, 2},
{"arm-p", Op::arm_p, 2},
{"arm-e", Op::arm_e, 2},
{"clear", Op::clear, 1}
}};
for (const Entry& entry : entries) {
if (words.front() != entry.word) {
continue;
}
if (words.size() != static_cast<std::size_t>(entry.arguments + 1)) {
return command;
}
int first = 0;
int second = 0;
if (!integer(words[1], first)) {
return command;
}
if (entry.arguments == 2 && !integer(words[2], second)) {
return command;
}
command.op = entry.op;
command.a = first;
command.b = second;
return command;
}
return command;
}
void print(const activation::Observation& observation)
{
std::cout << "{\"rc\":" << observation.rc;
std::cout << ",\"token\":" << observation.token;
std::cout << ",\"providers\":[";
bool comma = false;
for (const auto& view : observation.providers) {
if (comma) {
std::cout << ',';
}
comma = true;
std::cout << '[' << view.handles << ',' << view.prepared << ',';
std::cout << view.enabled << ',' << view.prepare_on << ',';
std::cout << view.prepare_off << ',' << view.enable_on << ',';
std::cout << view.enable_off << ',' << view.prepare_calls << ',';
std::cout << view.enable_calls << ',' << view.pending_prepare << ',';
std::cout << view.pending_enable << ']';
}
std::cout << "],\"handles\":[";
comma = false;
for (const auto& view : observation.handles) {
if (comma) {
std::cout << ',';
}
comma = true;
std::cout << '[' << view.token << ',' << view.client << ',';
std::cout << view.provider << ',' << view.prepared << ',';
std::cout << view.enabled << ']';
}
std::cout << "],\"events\":[";
comma = false;
for (const auto& event : observation.events) {
if (comma) {
std::cout << ',';
}
comma = true;
std::cout << '[' << event.kind << ',' << event.provider << ',';
std::cout << event.token << ']';
}
std::cout << "]}\n";
}
}
int main()
{
activation::Engine engine;
std::string line;
while (std::getline(std::cin, line)) {
std::istringstream stream(line);
std::vector<std::string> words;
std::string word;
while (stream >> word) {
words.push_back(word);
}
if (words.empty()) {
continue;
}
engine.execute(parse(words));
print(engine.observe());
}
return 0;
}