#!/usr/bin/env node /** * The library's tools: check every routine of pplus-library (the submodule library/) at every level, record * what was found in the library, and generate core's registry from it. * * node scripts/library.mjs check [--dir library] [--only stats.Mean] every check; writes status.json, STATUS.md, goldens * node scripts/library.mjs emit [--check] library/ -> src/library/generated.ts * node scripts/library.mjs harness print the program a reference is run as * node scripts/library.mjs mojo-sources [--out dir] write each Mojo reference's harness, for godbolt * * The checks of one routine: * structure routine.pseudo defines exactly the routine routine.json names, and nothing else (USE lines aside) * pseudo every case runs; its result is recorded and becomes what the references are held to * prover every obligation of the routine: pass when all are proved, open when some are not, fail when broken * fuzz the fuzzer finds no input that breaks a contract or stops the run * mojo the Mojo generated from the pseudo checks, runs in core's interpreter and prints what the pseudo prints * and for each reference implementation, per way of running it: * cpython python3 runs it on every case cc -O0, cc -O2 cc compiles and runs it on every case * interpreter core's Mojo interpreter runs it godbolt the stored answer of the pinned compiler * and the proof, if there is one: the pinned Dafny verifies it (or its stored verdict for this exact text). * * Needs a build (dist/). Python and a C compiler are used when they are on the PATH; without them those checks * are recorded as skipped, never as passed. */ import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import crypto from 'node:crypto'; import { execFileSync, spawnSync } from 'node:child_process'; import { fileURLToPath } from 'node:url'; const CORE = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const args = process.argv.slice(2); const cmd = args[0]; const opt = (name, dflt = null) => { const i = args.indexOf(`--${name}`); return i >= 0 ? args[i + 1] : dflt; }; const flag = (name) => args.includes(`--${name}`); const LIB = path.resolve(opt('dir', path.join(CORE, 'library'))); const GENERATED = path.join(CORE, 'src', 'library', 'generated.ts'); const sha256 = (s) => crypto.createHash('sha256').update(s).digest('hex'); const readJson = (p) => JSON.parse(fs.readFileSync(p, 'utf8')); const writeIfChanged = (p, text) => { if (!fs.existsSync(p) || fs.readFileSync(p, 'utf8') !== text) { fs.mkdirSync(path.dirname(p), { recursive: true }); fs.writeFileSync(p, text); return true; } return false; }; /* ------------------------------------------------------------------ */ /* Reading the library */ /* ------------------------------------------------------------------ */ /** Every module and routine on disk, with its sources, and the status last recorded for it. */ export function load(dir = LIB) { if (!fs.existsSync(path.join(dir, 'modules'))) throw new Error(`no library at ${dir}: check out the submodule (git submodule update --init) or pass --dir`); const status = fs.existsSync(path.join(dir, 'status.json')) ? readJson(path.join(dir, 'status.json')).routines || {} : {}; const modules = []; for (const m of fs.readdirSync(path.join(dir, 'modules')).sort()) { const mdir = path.join(dir, 'modules', m); if (!fs.existsSync(path.join(mdir, 'module.json'))) continue; const meta = readJson(path.join(mdir, 'module.json')); const routines = []; for (const r of fs.readdirSync(mdir).sort()) { const rdir = path.join(mdir, r); if (!fs.existsSync(path.join(rdir, 'routine.json'))) continue; const rm = readJson(path.join(rdir, 'routine.json')); const id = `${meta.name}.${rm.name}`; const st = status[id] || {}; routines.push({ id, module: meta.name, name: rm.name, dir: path.relative(dir, rdir).split(path.sep).join('/'), summary: rm.summary || '', tags: rm.tags || [], sources: rm.sources || [], source: fs.readFileSync(path.join(rdir, 'routine.pseudo'), 'utf8'), cases: (rm.cases || []).map((c, i) => ({ ...c, ...(st.cases?.[i]?.call === c.call ? { result: st.cases[i].result } : {}) })), references: (rm.references || []).map((ref) => ({ lang: ref.lang, level: ref.level, file: ref.file, note: ref.note || '', ...(ref.rel !== undefined ? { rel: ref.rel } : {}), ...(ref.abs !== undefined ? { abs: ref.abs } : {}), source: fs.readFileSync(path.join(rdir, ref.file), 'utf8'), checks: st.references?.[`${ref.lang}/${ref.level}`] || {}, })), ...(rm.proof ? { proof: { lang: rm.proof.lang, file: rm.proof.file, note: rm.proof.note || '', source: fs.readFileSync(path.join(rdir, rm.proof.file), 'utf8'), check: st.proof || { status: 'unverified', detail: 'not checked yet' } } } : {}), checks: st.checks || {}, }); } modules.push({ name: meta.name, title: meta.title, summary: meta.summary, routines }); } return modules; } /* ------------------------------------------------------------------ */ /* Values in other languages */ /* ------------------------------------------------------------------ */ const floatText = (v) => { const s = String(v); return /[.e]/.test(s) || !Number.isFinite(v) ? s : `${s}.0`; }; const items = (v) => (Array.isArray(v) ? v : v?.data ? v.data : []); function pyLiteral(v, t) { switch (t?.k) { case 'FLOAT': return floatText(v).replace('e+', 'e'); case 'INTEGER': return String(v); case 'BOOL': return v ? 'True' : 'False'; case 'CHAR': case 'STRING': return JSON.stringify(v); case 'LIST': case 'ARRAY': return `[${items(v).map((x) => pyLiteral(x, t.of)).join(', ')}]`; default: throw new Error(`no Python literal for ${t?.k}`); } } function mojoType(t) { switch (t?.k) { case 'FLOAT': return t.bits === 32 ? 'Float32' : 'Float64'; case 'INTEGER': return t.bits ? `${t.signed ? 'Int' : 'UInt'}${t.bits}` : 'Int'; case 'BOOL': return 'Bool'; case 'CHAR': case 'STRING': return 'String'; case 'LIST': case 'ARRAY': return `List[${mojoType(t.of)}]`; default: throw new Error(`no Mojo type for ${t?.k}`); } } function mojoLiteral(v, t) { switch (t?.k) { case 'FLOAT': return floatText(v).replace('e+', 'e'); case 'INTEGER': return String(v); case 'BOOL': return v ? 'True' : 'False'; case 'CHAR': case 'STRING': return JSON.stringify(v); case 'LIST': case 'ARRAY': return `${mojoType(t)}(${items(v).map((x) => mojoLiteral(x, t.of)).join(', ')})`; default: throw new Error(`no Mojo literal for ${t?.k}`); } } /** A C argument list for one call, with the declarations it needs first: a list becomes (array, length). */ function cArgs(vals, params, tag) { const decls = []; const out = []; vals.forEach((v, i) => { const t = params[i].type; switch (t?.k) { case 'FLOAT': out.push(floatText(v)); break; case 'INTEGER': out.push(`${v}L`); break; case 'BOOL': out.push(v ? '1' : '0'); break; case 'CHAR': out.push(`'${v.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`); break; case 'STRING': out.push(JSON.stringify(v)); break; case 'LIST': case 'ARRAY': { const xs = items(v); if (t.of?.k !== 'FLOAT' && t.of?.k !== 'INTEGER') throw new Error('C lists hold numbers only'); const ct = t.of.k === 'FLOAT' ? 'double' : 'long'; const name = `a${tag}_${i}`; decls.push(`${ct} ${name}[${Math.max(1, xs.length)}] = {${xs.length ? xs.map((x) => (t.of.k === 'FLOAT' ? floatText(x) : `${x}L`)).join(', ') : '0'}};`); out.push(name, `${xs.length}L`); break; } default: throw new Error(`no C argument for ${t?.k}`); } }); return { decls, args: out }; } /* ------------------------------------------------------------------ */ /* The checks */ /* ------------------------------------------------------------------ */ const ok = (detail) => ({ status: 'pass', detail }); const bad = (detail) => ({ status: 'fail', detail }); const skip = (detail) => ({ status: 'skipped', detail }); const open = (detail) => ({ status: 'open', detail }); const unverified = (detail) => ({ status: 'unverified', detail }); function have(cmdName) { return spawnSync(cmdName, ['--version'], { encoding: 'utf8' }).status === 0; } /** Does a printed value agree with the pseudo's, within the reference's tolerance? */ function agrees(printed, want, t, rel = 0, abs = 0) { const s = String(printed).trim(); if (t?.k === 'FLOAT' || t?.k === 'INTEGER') { const got = Number(s.replace(/^inf$/i, 'Infinity').replace(/^-inf$/i, '-Infinity').replace(/^nan$/i, 'NaN')); if (Number.isNaN(got) && Number.isNaN(want)) return true; if (Number.isNaN(got)) return false; if (got === want) return true; return Math.abs(got - want) <= Math.max(rel * Math.max(Math.abs(got), Math.abs(want)), abs); } if (t?.k === 'BOOL') return ['true', '1'].includes(s.toLowerCase()) === Boolean(want); return s === String(want); } function compareLines(stdout, wants, t, rel, abs) { const got = stdout.replace(/\n$/, '').split('\n'); if (got.length !== wants.length) return `printed ${got.length} line(s) for ${wants.length} case(s)`; for (let i = 0; i < wants.length; i++) if (!agrees(got[i], wants[i].value, t, rel, abs)) return `case ${i + 1} (${wants[i].call}) printed ${got[i]}, and the pseudo gives ${wants[i].text}`; return null; } /** The program a reference implementation is run as: its source, plus a main that prints each case. */ function harness(lang, ref, fname, sub, calls) { const params = sub.params; if (lang === 'python') { return `import importlib.util\nspec = importlib.util.spec_from_file_location("ref", __REF__)\nref = importlib.util.module_from_spec(spec)\nspec.loader.exec_module(ref)\n${calls.map((c) => `print(repr(ref.${fname}(${c.args.map((v, i) => pyLiteral(v, params[i].type)).join(', ')})))`).join('\n')}\n`; } if (lang === 'mojo') { return `${ref.source.replace(/\s+$/, '')}\n\n\ndef main() raises:\n${calls.map((c) => ` print(${fname}(${c.args.map((v, i) => mojoLiteral(v, params[i].type)).join(', ')}))`).join('\n')}\n`; } if (lang === 'c') { const fmt = sub.returns?.k === 'FLOAT' ? '%.17g' : '%ld'; const body = calls.map((c, k) => { const a = cArgs(c.args, params, k); return ` { ${a.decls.join(' ')} printf("${fmt}\\n", ${fname}(${a.args.join(', ')})); }`; }).join('\n'); return `#include \n\n${ref.source.replace(/\s+$/, '')}\n\nint main(void) {\n${body}\n return 0;\n}\n`; } throw new Error(`no harness for ${lang}`); } export async function checkLibrary(dir = LIB, only = null) { const { setLibrary } = await import(path.join(CORE, 'dist/library/registry.js')); const svc = await import(path.join(CORE, 'dist/services/pseudo.js')); const { parseExpression } = await import(path.join(CORE, 'dist/engines/pseudo/parse.js')); const { evaluateConstant } = await import(path.join(CORE, 'dist/engines/pseudo/interp.js')); const { formatValue } = await import(path.join(CORE, 'dist/engines/pseudo/lang.js')); const { pseudoToMojo, snake } = await import(path.join(CORE, 'dist/sync/pseudo-to-mojo.js')); const { checkMojo } = await import(path.join(CORE, 'dist/mojo/check.js')); const { runMojo } = await import(path.join(CORE, 'dist/mojo/interp.js')); const { sameOutput } = await import(path.join(CORE, 'dist/sync/compare.js')); const modules = load(dir); setLibrary(modules); const pin = readJson(path.join(CORE, 'mojo', 'pin.json')); const dafny = (() => { try { const p = execFileSync('python3', [path.join(CORE, 'scripts', 'dafny.py'), 'path'], { encoding: 'utf8' }).trim(); return fs.existsSync(p) ? p : null; } catch { return null; } })(); const dafnyVersion = (() => { try { return readJson(path.join(CORE, 'scripts', 'dafny.json')).version; } catch { return null; } })() || (() => { const m = /VERSION = "([^"]+)"/.exec(fs.readFileSync(path.join(CORE, 'scripts', 'dafny.py'), 'utf8')); return m ? m[1] : 'unknown'; })(); const python = have('python3'); const cc = have('cc'); const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'pplus-library-')); const out = {}; for (const m of modules) { for (const r of m.routines) { if (only && r.id !== only) { continue; } const rdir = path.join(dir, r.dir); const rec = { checks: {}, cases: [], references: {}, proof: undefined }; out[r.id] = rec; const st = svc.studyOf(r.source); const program = st.program?.program; // structure const subs = program ? [...program.subs.values()] : []; if (!st.ok) { rec.checks.structure = bad(`the pseudo does not check: ${st.diagnostics.filter((d) => d.severity === 'error').map((d) => `line ${d.line}: ${d.message}`).join('; ')}`); continue; } if (subs.length !== 1 || subs[0].name.toUpperCase() !== r.name.toUpperCase()) { rec.checks.structure = bad(`routine.pseudo defines ${subs.map((s) => s.name).join(', ') || 'nothing'}; it must define exactly ${r.name} (helpers are routines of their own, brought in with USE)`); continue; } if (program.main.body.length) { rec.checks.structure = bad(`routine.pseudo has lines outside ${r.name} (line ${program.main.body[0].line}); a routine file holds USE lines and the routine only`); continue; } const sub = subs[0]; rec.checks.structure = ok(`defines ${sub.kind === 'Function' ? 'FUNCTION' : 'PROCEDURE'} ${sub.name}${program.libs?.size ? `, using ${[...program.libs.values()].map((x) => x.lib.id).join(', ')}` : ''}`); // pseudo: every case const calls = []; let caseFail = null; for (const c of r.cases) { const p = parseExpression(c.call); let entry = { call: c.call }; try { if (p.diags.some((d) => d.severity === 'error') || p.expr.kind !== 'CallExpr' || p.expr.name.toUpperCase() !== r.name.toUpperCase()) throw new Error(`"${c.call}" is not a call of ${r.name}`); const vals = p.expr.args.map((a) => evaluateConstant(program, a)); const tr = svc.runPseudo(st, { call: c.call, maxSteps: 2000000 }); if (tr.error) throw new Error(`${c.call} stops: ${tr.error.message}`); const result = tr.raw.result; entry = { call: c.call, result: formatValue(result, sub.returns, { max: 1000 }) }; calls.push({ call: c.call, args: vals, value: result, text: entry.result }); } catch (e) { caseFail = caseFail || e.message; } rec.cases.push(entry); } rec.checks.pseudo = caseFail ? bad(caseFail) : ok(`${calls.length} case(s) run`); // prover const pr = svc.provePseudo(st); const obs = pr.units.flatMap((u) => u.obligations); const broken = obs.filter((o) => o.status === 'broken'); const notProved = obs.filter((o) => o.status !== 'proved'); rec.checks.prover = broken.length ? bad(`${broken.length} obligation(s) broken: ${broken.map((o) => `${o.kind}@${o.line} (${o.label})`).join('; ')}`) : notProved.length ? open(`${obs.length - notProved.length} of ${obs.length} obligation(s) proved; open: ${notProved.map((o) => `${o.kind}@${o.line} (${o.label})`).join('; ')}`) : ok(`all ${obs.length} obligation(s) proved`); // fuzz const props = svc.propertiesPseudo(st, r.name)[0]; rec.checks.fuzz = !props ? skip('no routine to fuzz') : props.problem ? skip(`the fuzzer cannot generate inputs: ${props.problem}`) : props.failures.length ? bad(`${props.failures.length} failing input(s), first: ${props.failures[0].call}: ${props.failures[0].message}`) : ok(`no failure in ${props.checked} generated input(s)`); // the Mojo generated from the pseudo, on every case if (calls.length) { const prog = `USE ${r.module}.${r.name}\n${calls.map((c) => `OUTPUT ${c.call}`).join('\n')}\n`; const want = svc.runPseudo(svc.studyOf(prog), {}).output.join('\n'); const mj = pseudoToMojo(prog); if ('unsupported' in mj) rec.checks.mojo = bad(`the translator refused: ${mj.unsupported.map((d) => d.message).join('; ')}`); else { const gaps = mj.notes.filter((n) => n.id === 'translate-gap'); const ck = checkMojo(mj.source); const errs = ck.diagnostics.filter((d) => d.severity === 'error'); if (gaps.length) rec.checks.mojo = skip(`the translator has no Mojo for: ${gaps.map((g) => g.message.replace(/^.*?\(|\)\..*$/g, '')).join('; ')}`); else if (errs.length) rec.checks.mojo = bad(`the generated Mojo has checker errors: ${errs.map((d) => `L${d.line} ${d.message}`).join(' | ')}`); else { const run = runMojo(ck.module, mj.source, { maxSteps: 4000000 }); rec.checks.mojo = run.refused ? skip(`core's Mojo interpreter declines ${run.refused}; the compiler's answer is needed`) : run.exitCode !== 0 ? bad(`the generated Mojo stopped (exit ${run.exitCode})`) : sameOutput(want, run.stdout) ? ok('the generated Mojo prints what the pseudo prints on every case, in core\'s interpreter') : bad(`the generated Mojo printed ${JSON.stringify(run.stdout.slice(0, 120))}; the pseudo printed ${JSON.stringify(want.slice(0, 120))}`); } } } // references const fname = snake(r.name); for (const ref of r.references) { const checks = {}; rec.references[`${ref.lang}/${ref.level}`] = checks; if (!calls.length) { checks.cases = skip('no case ran in pseudo, so there is nothing to hold it to'); continue; } const rel = ref.rel ?? 0; const abs = ref.abs ?? 0; const tol = rel || abs ? ` (within rel ${rel}, abs ${abs})` : ' (exactly)'; try { if (ref.lang === 'python') { if (!python) { checks.cpython = skip('python3 is not on the PATH'); continue; } const h = harness('python', ref, fname, sub, calls).replace('__REF__', JSON.stringify(path.join(rdir, ref.file))); const p = spawnSync('python3', ['-c', h], { encoding: 'utf8', timeout: 60000 }); if (p.status !== 0) { checks.cpython = bad(`python3 stopped: ${(p.stderr || '').trim().split('\n').pop()}`); continue; } const diff = compareLines(p.stdout, calls, sub.returns, rel, abs); checks.cpython = diff ? bad(diff) : ok(`agrees with the pseudo on ${calls.length} case(s)${tol}`); } else if (ref.lang === 'c') { if (!cc) { checks['cc -O0'] = skip('no C compiler (cc) on the PATH'); continue; } const src = harness('c', ref, fname, sub, calls); const file = path.join(tmp, `${fname}-${ref.level}.c`); fs.writeFileSync(file, src); for (const level of ['-O0', '-O2']) { const bin = path.join(tmp, `${fname}-${ref.level}${level}`); const c = spawnSync('cc', ['-std=c11', level, '-o', bin, file, '-lm'], { encoding: 'utf8' }); if (c.status !== 0) { checks[`cc ${level}`] = bad(`cc ${level} failed: ${(c.stderr || '').trim().split('\n')[0]}`); continue; } const p = spawnSync(bin, [], { encoding: 'utf8', timeout: 60000 }); const diff = p.status !== 0 ? `exited ${p.status}` : compareLines(p.stdout, calls, sub.returns, rel, abs); checks[`cc ${level}`] = diff ? bad(diff) : ok(`agrees with the pseudo on ${calls.length} case(s)${tol}`); } } else if (ref.lang === 'mojo') { const src = harness('mojo', ref, fname, sub, calls); const ck = checkMojo(src); const errs = ck.diagnostics.filter((d) => d.severity === 'error'); if (errs.length) checks.interpreter = bad(`core's Mojo checker reports: ${errs.map((d) => `L${d.line} ${d.message}`).join(' | ')}`); else { const run = runMojo(ck.module, src, { maxSteps: 4000000 }); const diff = run.refused ? null : run.exitCode !== 0 ? `stopped (exit ${run.exitCode})` : compareLines(run.stdout, calls, sub.returns, rel, abs); checks.interpreter = run.refused ? skip(`core's Mojo interpreter declines ${run.refused}`) : diff ? bad(diff) : ok(`agrees with the pseudo on ${calls.length} case(s) in core's interpreter${tol}`); } // the compiler's stored answer for this exact program const gfile = path.join(rdir, 'golden', `mojo-${ref.level}.json`); const g = fs.existsSync(gfile) ? readJson(gfile) : null; if (!g || g.source_sha256 !== sha256(src) || g.compiler !== pin.godbolt.compiler) checks.godbolt = unverified(`no stored answer from ${pin.godbolt.compiler} for this program: pixi run library-verify-mojo`); else if (!g.result?.compiled) checks.godbolt = bad(`${pin.godbolt.compiler} did not compile it: ${(g.result?.buildStderr || '').split('\n')[0]}`); else { const diff = g.result.exitCode !== 0 ? `exited ${g.result.exitCode}` : compareLines(g.result.stdout, calls, sub.returns, rel, abs); checks.godbolt = diff ? bad(diff) : ok(`${pin.godbolt.compiler} agrees with the pseudo on ${calls.length} case(s)${tol}`); } } else checks.cases = skip(`no runner for ${ref.lang} yet`); } catch (e) { checks.harness = bad(`could not build the harness: ${e.message}`); } } // proof if (r.proof) { const gfile = path.join(rdir, 'golden', 'dafny.json'); const src = r.proof.source; const flags = ['verify', '--resource-limit', '20000000']; if (dafny) { const p = spawnSync(dafny, [...flags, path.join(rdir, r.proof.file)], { encoding: 'utf8', timeout: 600000 }); const summary = /Dafny program verifier finished with (.*)/.exec(p.stdout || '')?.[1] || (p.stdout || p.stderr || '').trim().split('\n').pop(); const pass = p.status === 0 && / 0 errors/.test(summary); rec.proof = pass ? ok(`Dafny ${dafnyVersion}: ${summary}`) : bad(`Dafny ${dafnyVersion}: ${summary}`); writeIfChanged(gfile, `${JSON.stringify({ tool: 'dafny', version: dafnyVersion, flags, source_sha256: sha256(src), verdict: pass ? 'verified' : 'failed', summary }, null, 2)}\n`); } else { const g = fs.existsSync(gfile) ? readJson(gfile) : null; rec.proof = g && g.source_sha256 === sha256(src) ? (g.verdict === 'verified' ? ok(`Dafny ${g.version} (stored answer): ${g.summary}`) : bad(`Dafny ${g.version} (stored answer): ${g.summary}`)) : unverified('the pinned Dafny is not fetched (pixi run dafny-fetch) and there is no stored verdict for this text'); } } } } fs.rmSync(tmp, { recursive: true, force: true }); return { modules, results: out }; } const ORDER = ['structure', 'pseudo', 'prover', 'fuzz', 'mojo']; function statusMarkdown(modules, results) { const mark = (c) => (!c ? '·' : { pass: 'pass', fail: '**fail**', open: 'open', skipped: 'skipped', unverified: 'unverified' }[c.status] || c.status); const lines = ['# Library status', '', '', '', 'Each routine, at each level. *pass*: checked and agrees; *open*: the prover could not finish every obligation (nothing is wrong, nothing is proved); *skipped*: the tool is not available here, or it declines the construct; *unverified*: no stored answer for this exact text yet.', '']; for (const m of modules) { lines.push(`## ${m.title} (\`${m.name}\`)`, '', '| Routine | Cases | Prover | Fuzz | Generated Mojo | References | Proof |', '| --- | --- | --- | --- | --- | --- | --- |'); for (const r of m.routines) { const res = results[r.id]; if (!res) continue; const refs = Object.entries(res.references).map(([k, checks]) => `${k}: ${Object.entries(checks).map(([how, c]) => `${how} ${mark(c)}`).join(', ')}`).join('
'); lines.push(`| \`${r.id}\` | ${mark(res.checks.pseudo)} | ${mark(res.checks.prover)} | ${mark(res.checks.fuzz)} | ${mark(res.checks.mojo)} | ${refs || '·'} | ${res.proof ? `dafny ${mark(res.proof)}` : '·'} |`); } lines.push(''); } lines.push('## Details', ''); for (const m of modules) for (const r of m.routines) { const res = results[r.id]; if (!res) continue; lines.push(`### ${r.id}`, ''); for (const k of ORDER) if (res.checks[k]) lines.push(`- ${k}: ${res.checks[k].status}. ${res.checks[k].detail}`); for (const [k, checks] of Object.entries(res.references)) for (const [how, c] of Object.entries(checks)) lines.push(`- ${k}, ${how}: ${c.status}. ${c.detail}`); if (res.proof) lines.push(`- proof: ${res.proof.status}. ${res.proof.detail}`); lines.push(''); } return lines.join('\n'); } /* ------------------------------------------------------------------ */ /* Emitting core's registry */ /* ------------------------------------------------------------------ */ function emitText(dir = LIB) { const modules = load(dir); let commit = ''; try { commit = execFileSync('git', ['-C', dir, 'rev-parse', '--short=12', 'HEAD'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim(); } catch { /* not a checkout */ } return `// Generated by scripts/library.mjs emit from library/ (pplus-library). Do not edit; run pixi run library-emit.\nimport type { LibraryModule } from './types.js';\n\nexport const LIBRARY_COMMIT = ${JSON.stringify(commit)};\nexport const LIBRARY_MODULES: LibraryModule[] = ${JSON.stringify(modules, null, 2)};\n`; } /* ------------------------------------------------------------------ */ /* Command line */ /* ------------------------------------------------------------------ */ if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { if (cmd === 'check') { const only = opt('only'); const { modules, results } = await checkLibrary(LIB, only); const prior = fs.existsSync(path.join(LIB, 'status.json')) ? readJson(path.join(LIB, 'status.json')).routines || {} : {}; const merged = only ? { ...prior, ...results } : results; const sorted = Object.fromEntries(Object.keys(merged).sort().map((k) => [k, merged[k]])); writeIfChanged(path.join(LIB, 'status.json'), `${JSON.stringify({ comment: 'Written by pixi run library-check in pplus-core. Do not edit.', routines: sorted }, null, 2)}\n`); writeIfChanged(path.join(LIB, 'STATUS.md'), `${statusMarkdown(modules, sorted)}\n`); let failed = 0; for (const [id, res] of Object.entries(results)) { const all = [...Object.entries(res.checks), ...Object.entries(res.references).flatMap(([k, c]) => Object.entries(c).map(([h, v]) => [`${k} ${h}`, v])), ...(res.proof ? [['proof', res.proof]] : [])]; for (const [k, c] of all) if (c.status === 'fail') { failed++; console.log(`FAIL ${id} ${k}: ${c.detail}`); } console.log(`${id}: ${all.map(([k, c]) => `${k}=${c.status}`).join(' ')}`); } console.log(failed ? `${failed} failing check(s)` : 'no failing check'); process.exit(failed ? 1 : 0); } else if (cmd === 'emit') { const text = emitText(LIB); if (flag('check')) { const cur = fs.existsSync(GENERATED) ? fs.readFileSync(GENERATED, 'utf8') : ''; if (cur !== text) { console.error('src/library/generated.ts is out of date with library/: run pixi run library-emit'); process.exit(1); } console.log('src/library/generated.ts is up to date'); } else console.log(writeIfChanged(GENERATED, text) ? 'wrote src/library/generated.ts' : 'src/library/generated.ts unchanged'); } else if (cmd === 'harness' || cmd === 'mojo-sources') { const svc = await import(path.join(CORE, 'dist/services/pseudo.js')); const { setLibrary } = await import(path.join(CORE, 'dist/library/registry.js')); const { parseExpression } = await import(path.join(CORE, 'dist/engines/pseudo/parse.js')); const { evaluateConstant } = await import(path.join(CORE, 'dist/engines/pseudo/interp.js')); const { snake } = await import(path.join(CORE, 'dist/sync/pseudo-to-mojo.js')); const modules = load(LIB); setLibrary(modules); const programs = []; for (const m of modules) for (const r of m.routines) { const st = svc.studyOf(r.source); if (!st.ok) continue; const sub = [...st.program.program.subs.values()][0]; const calls = r.cases.map((c) => ({ args: parseExpression(c.call).expr.args.map((a) => evaluateConstant(st.program.program, a)) })); for (const ref of r.references) programs.push({ id: r.id, ref, dir: path.join(LIB, r.dir), make: () => harness(ref.lang, ref, snake(r.name), sub, calls) }); } if (cmd === 'harness') { const [id, lang, level] = args.slice(1); const p = programs.find((x) => x.id === id && x.ref.lang === lang && x.ref.level === level); if (!p) { console.error(`no reference ${lang}/${level} for ${id}`); process.exit(2); } process.stdout.write(p.make()); } else { // one JSON line per Mojo reference: what scripts/library_godbolt.py sends, and where its answer goes const list = programs.filter((p) => p.ref.lang === 'mojo').map((p) => ({ id: `${p.id} mojo/${p.ref.level}`, source: p.make(), golden: path.join(p.dir, 'golden', `mojo-${p.ref.level}.json`) })); process.stdout.write(`${JSON.stringify(list, null, 2)}\n`); } } else { console.error('usage: node scripts/library.mjs check [--only id] | emit [--check] | harness | mojo-sources'); process.exit(2); } }