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

119 lines
4.5 KiB
C++

#include "frames.hpp"
#include <limits>
#include <sstream>
namespace frames {
int parse_u32(const char* p,std::size_t n,std::uint32_t* out) {
if (!p || !out || n==0) return ARG;
bool overflow=false;
std::uint32_t value=0;
for (std::size_t i=0;i<n;++i) {
unsigned char c=static_cast<unsigned char>(p[i]);
if (c<'0' || c>'9') return ARG;
unsigned digit=c-'0';
if (!overflow) {
if (value>(std::numeric_limits<std::uint32_t>::max()-digit)/10) overflow=true;
else value=value*10+digit;
}
}
if (overflow) return RANGE;
*out=value;
return OK;
}
int tokenize(const char* p,std::size_t n,std::vector<std::string>* out) {
if (!p || !out || n>8192) return ARG;
for (std::size_t i=0;i<n;++i) {
unsigned char c=static_cast<unsigned char>(p[i]);
if (c!=' ' && c!='\t' && !(c>='!' && c<='~')) return ARG;
}
std::vector<std::string> result;
std::size_t cursor=0;
while (cursor<n) {
while (cursor<n && (p[cursor]==' ' || p[cursor]=='\t')) ++cursor;
if (cursor==n) break;
std::size_t start=cursor;
while (cursor<n && p[cursor]!=' ' && p[cursor]!='\t') ++cursor;
result.emplace_back(p+start,cursor-start);
}
*out=result;
return OK;
}
static std::string response(int rc,const std::vector<std::uint64_t>& values={}) {
std::ostringstream out;
out << "{\"rc\":" << rc << ",\"out\":[";
for (std::size_t i=0;i<values.size();++i) {
if (i) out << ',';
out << values[i];
}
out << "]}";
return out.str();
}
static std::string show(const View& v) {
std::ostringstream out;
out << "{\"rc\":0,\"out\":[" << v.mode << ',' << v.limit << ',' << v.aliases << ',' << v.owner << ',' << v.depth << ',' << v.waiting << ',' << v.top << ',' << v.entered << ',' << v.exited << ',' << v.handoffs << "]}";
return out.str();
}
std::string execute(Bank* b,const char* p,std::size_t n) {
if (!b) return response(ARG);
std::vector<std::string> tokens;
if (tokenize(p,n,&tokens)!=OK || tokens.empty()) return response(ARG);
const std::string command=tokens.front();
unsigned required=0;
if (command=="RESET" || command=="STATS") required=0;
else if (command=="DUP" || command=="CLOSE" || command=="VIEW" || command=="ACTOR") required=1;
else if (command=="NEW" || command=="LEAVE" || command=="CANCEL" || command=="WAIT") required=2;
else if (command=="ENTER" || command=="REQUEST") required=3;
else return response(ARG);
if (tokens.size()!=required+1) return response(ARG);
std::vector<std::uint32_t> args(required);
for (unsigned i=0;i<required;++i) {
int rc=parse_u32(tokens[i+1].data(),tokens[i+1].size(),&args[i]);
if (rc!=OK) return response(rc);
}
if (command=="RESET") return response(reset(b));
if (command=="STATS") {
Stats s;
int rc=stats(b,&s);
return response(rc,{s.handles,s.objects,s.frames,s.waiters,s.creates,s.duplicates,s.closes,s.revision});
}
if (command=="NEW") {
if (args[0]>1 || args[1]<1 || args[1]>MAX_DEPTH) return response(ARG);
Attr attr;
attr_mode(&attr,int(args[0]));
attr_limit(&attr,args[1]);
int h=-1;
int rc=create(b,&attr,&h);
return rc==OK ? response(rc,{std::uint64_t(h)}) : response(rc);
}
if (command=="ACTOR") {
if (args[0]>7) return response(ARG);
ActorView value{};
int rc=actor_view(b,int(args[0]),&value);
return rc==OK ? response(rc,{value.held_objects,value.held_frames,value.pending_objects}) : response(rc);
}
if (args[0]>=MAX_HANDLES) return response(ARG);
int h=int(args[0]);
if (command=="DUP") {
int next=-1;
int rc=duplicate(b,h,&next);
return rc==OK ? response(rc,{std::uint64_t(next)}) : response(rc);
}
if (command=="CLOSE") return response(close(b,h));
if (command=="VIEW") {
View v{};
int rc=view(b,h,&v);
return rc==OK ? show(v) : response(rc);
}
if (command=="WAIT") {
WaitView w{};
int rc=queued(b,h,args[1],&w);
return rc==OK ? response(rc,{std::uint64_t(w.actor),w.note}) : response(rc);
}
if (args[1]>7) return response(ARG);
int actor=int(args[1]);
if (command=="LEAVE") return response(leave(b,h,actor));
if (command=="CANCEL") return response(cancel(b,h,actor));
if (command=="ENTER") return response(enter(b,h,actor,args[2]));
return response(request(b,h,actor,args[2]));
}
}