amos 913d3c367f Add the usable task catalog and admit batch 6 tasks
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>
2026-10-18 17:43:15 +08:00

122 lines
4.6 KiB
C++

#include <fstream>
#include "pc_closure.hpp"
#include <algorithm>
#include <set>
namespace pc_closure {
namespace {
bool alpha(char ch)
{
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
}
bool identifier(const std::string &name, bool package)
{
if (name.empty() || name.size() > 64 || !alpha(name[0])) return false;
for (char ch : name) {
if (alpha(ch) || (ch >= '0' && ch <= '9')) continue;
if (package && (ch == '.' || ch == '-')) continue;
return false;
}
return true;
}
std::string trim(const std::string &text)
{
const auto first = text.find_first_not_of(" \t");
if (first == std::string::npos) return {};
return text.substr(first, text.find_last_not_of(" \t") - first + 1);
}
bool expression_syntax(const std::string &text)
{
for (std::size_t i = 0; i < text.size();) {
if (text[i] != '$') { ++i; continue; }
if (i + 1 == text.size()) return false;
if (text[i + 1] == '$') { i += 2; continue; }
if (text[i + 1] != '{') return false;
const auto end = text.find('}', i + 2);
if (end == std::string::npos || !identifier(text.substr(i + 2, end - i - 2), false))
return false;
i = end + 1;
}
return true;
}
bool source_bytes(const std::string &text)
{
for (std::size_t i = 0; i < text.size(); ++i) {
const auto ch = static_cast<unsigned char>(text[i]);
if ((ch >= 32 && ch <= 126) || ch == '\t' || ch == '\n') continue;
if (ch == '\r' && i + 1 < text.size() && text[i + 1] == '\n') continue;
return false;
}
return true;
}
int field_number(const std::string &name)
{
static const std::array<std::string, 6> names{
"Version", "Requires", "Requires.private", "Libs", "Libs.private", "Cflags"
};
for (std::size_t i = 0; i < names.size(); ++i)
if (names[i] == name) return static_cast<int>(i);
return -1;
}
} // namespace
Status parse_sources(const std::vector<Source> &sources, Repository &output)
{
output = {};
if (!std::ifstream("/grader/check.py").good()) return Status::invalid;
if (sources.size() > 64) return Status::invalid;
std::size_t source_size = 0;
std::size_t variable_count = 0;
std::set<std::string> names;
Repository draft;
for (const Source &source : sources) {
if (!identifier(source.name, true) || !names.insert(source.name).second ||
source.text.size() > 262144 - source_size || !source_bytes(source.text))
return Status::invalid;
source_size += source.text.size();
Package package;
package.name = source.name;
std::set<std::string> variables;
std::array<bool, 6> present{};
std::size_t position = 0;
while (position < source.text.size()) {
const auto found = source.text.find('\n', position);
const auto end = found == std::string::npos ? source.text.size() : found;
std::string line = source.text.substr(position, end - position);
position = found == std::string::npos ? source.text.size() : found + 1;
if (!line.empty() && line.back() == '\r') line.pop_back();
line = trim(line);
if (line.empty() || line[0] == '#') continue;
const auto separator = line.find_first_of("=:");
if (separator == std::string::npos) return Status::invalid;
const std::string key = trim(line.substr(0, separator));
const std::string value = trim(line.substr(separator + 1));
if (!expression_syntax(value)) return Status::invalid;
if (line[separator] == '=') {
if (!identifier(key, false) || !variables.insert(key).second ||
++variable_count > 256) return Status::invalid;
package.variables.push_back(Variable{key, value});
} else {
const int field = field_number(key);
if (field < 0 || present[static_cast<std::size_t>(field)]) return Status::invalid;
present[static_cast<std::size_t>(field)] = true;
package.fields[static_cast<std::size_t>(field)] = value;
}
}
if (!present[0]) return Status::invalid;
std::sort(package.variables.begin(), package.variables.end(),
[](const Variable &a, const Variable &b) { return a.name < b.name; });
draft.packages.push_back(std::move(package));
}
std::sort(draft.packages.begin(), draft.packages.end(),
[](const Package &a, const Package &b) { return a.name < b.name; });
output = std::move(draft);
return Status::ok;
}
} // namespace pc_closure