Files
pplus-core/scripts/mojo/new-program.mjs
T
alexm cae5cff55a
Test / test (push) Failing after 1m38s
feat: move corpus to library
2026-10-06 10:27:46 -04:00

60 lines
2.7 KiB
JavaScript

#!/usr/bin/env node
/**
* Start a Mojo program of our own in the corpus: a probe or a stress program.
* (An exercise's program in library/mojo/local/ is made by `pixi run new-exercise <id>`,
* which also writes the exercise that shows it.)
*
* pixi run new-probe int-floordiv-negative "does // floor toward negative infinity?"
* pixi run new-stress ring-buffer "a fixed-size queue with wraparound"
*
* probe library/mojo/probes/<name>.mojo one question about the pinned compiler; godbolt's
* answer is the point (expectation: observe)
* stress library/mojo/stress/<name>.mojo realistic Mojo for the interpreter; its
* `# expect-stdout:` lines are the prediction that
* godbolt confirms or refutes (expectation: pass)
*
* Then: edit the file, `pixi run mojo-verify-item '<kind>/<name>'` (network), and
* `pixi run test`. docs/mojo-corpus.md, "Mojo of our own", has the header keys.
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const ROOT = fileURLToPath(new URL('../..', import.meta.url));
const KINDS = {
probe: { dir: 'library/mojo/probes', corpus: 'probes' },
stress: { dir: 'library/mojo/stress', corpus: 'stress' },
};
function die(msg) { console.error(`new-program: ${msg}`); process.exit(1); }
const [kind, name, ...rest] = process.argv.slice(2);
const about = rest.join(' ').trim();
if (!KINDS[kind]) die('usage: new-program.mjs probe|stress <name> "<one line: the question, or what it exercises>"');
if (!name || !/^[a-z0-9]+(-[a-z0-9]+)*$/.test(name)) die(`"${name ?? ''}" is not a valid name: lowercase letters and digits, joined by single hyphens`);
if (!about) die(`say in one line what the ${kind} is about, after its name`);
const file = path.join(ROOT, KINDS[kind].dir, `${name}.mojo`);
if (fs.existsSync(file)) die(`${path.relative(ROOT, file)} already exists; nothing was changed`);
const text = kind === 'probe'
? `# probe: ${about}
def main():
print("replace this with the smallest program that answers the question")
`
: `# probe: ${about}
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: replace with the first line the program should print
def main():
print("replace with the first line the program should print")
`;
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, text);
console.log(`created ${path.relative(ROOT, file)}
next:
edit it (the header block is read by corpus.py verify; the program starts at the first other line)
pixi run mojo-verify-item '${KINDS[kind].corpus}/${name}' # ask godbolt (network)
pixi run test`);