Baseline: tree as shared
This commit is contained in:
commit
659c03bbf8
1471 files changed
+990535
No files matched your search
@@ -0,0 +1,268 @@
|
||||
// Stage 4: pseudo -> Mojo. The generated Mojo must be accepted by the checker, run in the interpreter, and
|
||||
// print what the pseudo prints; its style is pinned by a snapshot per exercise; every standard-library name
|
||||
// it writes is in the harvested index. Programs it cannot yet hold to that are listed below, each with its reason.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { EXERCISES } from '../dist/exercises/index.js';
|
||||
import { studyOf, runPseudo } from '../dist/services/pseudo.js';
|
||||
import { checkMojo } from '../dist/mojo/check.js';
|
||||
import { runMojo } from '../dist/mojo/interp.js';
|
||||
import { pseudoToMojo, GENERATED_HEADER, USED_STDLIB, USED_PRELUDE } from '../dist/sync/pseudo-to-mojo.js';
|
||||
import { formatAsMojo, sameOutput } from '../dist/sync/compare.js';
|
||||
import { MOJO_STD_EXPORTS } from '../dist/mojo/generated/stdlib.js';
|
||||
import { MOJO_STD_STRUCTS } from '../dist/mojo/generated/types.js';
|
||||
|
||||
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
|
||||
|
||||
const here = path.dirname(fileURLToPath(import.meta.url));
|
||||
const corpusDir = path.join(here, 'corpus');
|
||||
const snapDir = path.join(here, 'snapshots', 'sync-mojo');
|
||||
|
||||
/**
|
||||
* Programs that do not (yet) get the full check, and why. A program leaves this list when it passes;
|
||||
* the test fails if a listed program passes, so the list can only shrink.
|
||||
* declined: the Mojo interpreter declines the construct (it declines what it has not been verified
|
||||
* against godbolt on); the translation is still checked, and the run is not.
|
||||
* differs: the notation and Mojo disagree by design, and a literal translation keeps Mojo's behavior.
|
||||
* checker: the checker's error is the right answer for what the pseudo says.
|
||||
*/
|
||||
const KNOWN_GAPS = {
|
||||
'ex:match-off-by-one': ['declined', 'the pseudo stops with an index error; the interpreter declines an out-of-range codepoint index'],
|
||||
'32-stops.pseudo': ['declined', 'the pseudo stops with an index error; the interpreter declines an out-of-range codepoint index'],
|
||||
'08-caesar.pseudo': ['declined', 'chr(...) is not run by the interpreter yet'],
|
||||
'24-string-builtins.pseudo': ['declined', 'ord(...) is not run by the interpreter yet'],
|
||||
'23-dice.pseudo': ['declined', 'random_si64 is not run by the interpreter yet'],
|
||||
'42-numerics.pseudo': ['declined', 'sin and the other math functions are not run by the interpreter yet'],
|
||||
'46-projectile.pseudo': ['declined', 'pi is not run by the interpreter yet'],
|
||||
'47-binomial-and-ph.pseudo': ['declined', 'comb is not run by the interpreter yet'],
|
||||
'33-stops-div.pseudo': ['differs', 'dividing by zero stops the notation; a Float64 division in Mojo gives nan'],
|
||||
'44-parallel-race.pseudo': ['differs', 'the notation detects a data race in PARALLEL FOR; Mojo runs the passes in order and prints a result'],
|
||||
'45-parallel-neighbour-race.pseudo': ['differs', 'the notation detects a data race in PARALLEL FOR; Mojo runs the passes in order and prints a result'],
|
||||
'35-stops-unset.pseudo': ['checker', 'the pseudo reads a variable that was never given a value; the checker reports exactly that, as the compiler would'],
|
||||
'36-stops-counter.pseudo': ['checker', 'the pseudo reads a FOR counter after its loop, where it has no value; the checker reports exactly that'],
|
||||
};
|
||||
|
||||
/** Programs whose pseudo does not parse, by design. The translator must say so, not crash. */
|
||||
const NO_PSEUDO = new Set(['ex:five-bugs', '37-static-errors.pseudo', '41-reserved-name.pseudo']);
|
||||
|
||||
const AGREE_AT_LEAST = 48; // raise as the interpreter learns more; counted below
|
||||
|
||||
function programs() {
|
||||
const out = [];
|
||||
for (const e of EXERCISES) if (e.sources.pseudo) out.push({ id: `ex:${e.id}`, name: e.id, pseudo: e.sources.pseudo, call: e.call ?? null, input: [] });
|
||||
for (const f of fs.readdirSync(corpusDir).filter((x) => x.endsWith('.pseudo')).sort()) {
|
||||
const src = fs.readFileSync(path.join(corpusDir, f), 'utf8');
|
||||
out.push({ id: f, name: f, pseudo: src, call: null, input: [...src.matchAll(/^\/\/ INPUT: (.*)$/gm)].map((m) => m[1]) });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** How one program fares: what the translator, the checker, the interpreter, and the pseudo run say. */
|
||||
function judge(p) {
|
||||
const st = studyOf(p.pseudo);
|
||||
if (!st.ok) return { verdict: 'no-pseudo', result: pseudoToMojo(p.pseudo, { call: p.call }) };
|
||||
const tr = runPseudo(st, { call: p.call, input: p.input, maxSteps: 200000 });
|
||||
const result = pseudoToMojo(p.pseudo, { call: p.call });
|
||||
assert.ok(!('unsupported' in result), `${p.id}: ${JSON.stringify(result.unsupported)}`);
|
||||
const gaps = result.notes.filter((n) => n.id === 'translate-gap');
|
||||
const checked = checkMojo(result.source);
|
||||
const errors = checked.diagnostics.filter((d) => d.severity === 'error');
|
||||
if (gaps.length) return { verdict: 'gap', detail: gaps.map((g) => `line ${g.line}: ${g.message}`).join('; '), result };
|
||||
if (errors.length) return { verdict: 'checker', detail: errors.map((d) => `L${d.line} ${d.message}`).join(' | '), result };
|
||||
const run = runMojo(checked.module, result.source, { input: p.input, maxSteps: 400000 });
|
||||
if (run.refused) return { verdict: 'declined', detail: run.refused, result };
|
||||
const stops = run.exitCode !== 0 || run.limited;
|
||||
if (tr.error) return stops ? { verdict: 'agree', result } : { verdict: 'differs', detail: `pseudo stops (${tr.error.kind}); Mojo ran to the end and printed ${JSON.stringify(run.stdout.slice(0, 80))}`, result };
|
||||
if (stops) return { verdict: 'differs', detail: `Mojo stopped (exit ${run.exitCode}); pseudo printed ${JSON.stringify(tr.output.slice(0, 2))}`, result };
|
||||
const want = p.call && tr.raw.result !== undefined
|
||||
? [...tr.raw.output, formatAsMojo(tr.raw.result)].join('\n')
|
||||
: tr.raw.output.join('\n');
|
||||
return sameOutput(want, run.stdout)
|
||||
? { verdict: 'agree', result }
|
||||
: { verdict: 'differs', detail: `pseudo printed ${JSON.stringify(want.slice(0, 100))}, Mojo printed ${JSON.stringify(run.stdout.slice(0, 100))}`, result };
|
||||
}
|
||||
|
||||
const verdicts = new Map();
|
||||
test('every exercise and corpus program: the generated Mojo checks, runs, and agrees with the pseudo, or is a listed gap', () => {
|
||||
const unexpected = [];
|
||||
let agree = 0;
|
||||
for (const p of programs()) {
|
||||
const j = judge(p);
|
||||
verdicts.set(p.id, j);
|
||||
if (j.verdict === 'no-pseudo') {
|
||||
if (!NO_PSEUDO.has(p.id)) unexpected.push(`${p.id}: the pseudo does not parse, and the program is not listed in NO_PSEUDO`);
|
||||
continue;
|
||||
}
|
||||
if (NO_PSEUDO.has(p.id)) { unexpected.push(`${p.id}: listed in NO_PSEUDO but its pseudo parses`); continue; }
|
||||
const known = KNOWN_GAPS[p.id];
|
||||
if (j.verdict === 'agree') {
|
||||
agree++;
|
||||
if (known) unexpected.push(`${p.id}: now agrees; take it off KNOWN_GAPS (${known[1]})`);
|
||||
continue;
|
||||
}
|
||||
if (!known) { unexpected.push(`${p.id}: ${j.verdict}: ${j.detail}`); continue; }
|
||||
if (known[0] !== j.verdict) unexpected.push(`${p.id}: listed as ${known[0]} but is ${j.verdict}: ${j.detail}`);
|
||||
}
|
||||
assert.deepEqual(unexpected, []);
|
||||
assert.ok(agree >= AGREE_AT_LEAST, `${agree} programs agree, fewer than ${AGREE_AT_LEAST}`);
|
||||
});
|
||||
|
||||
test('the pseudo that does not parse gets a reason, not a crash', () => {
|
||||
for (const id of NO_PSEUDO) {
|
||||
const p = programs().find((x) => x.id === id);
|
||||
const r = pseudoToMojo(p.pseudo, { call: p.call });
|
||||
assert.ok('unsupported' in r, id);
|
||||
assert.ok(r.unsupported.length > 0 && r.unsupported.every((d) => d.severity === 'error'), id);
|
||||
}
|
||||
});
|
||||
|
||||
test('every exercise with pseudo has its generated Mojo pinned by a snapshot (UPDATE_SNAPSHOTS=1 rewrites them for review)', () => {
|
||||
fs.mkdirSync(snapDir, { recursive: true });
|
||||
const wanted = new Set();
|
||||
for (const p of programs().filter((x) => x.id.startsWith('ex:'))) {
|
||||
const r = pseudoToMojo(p.pseudo, { call: p.call });
|
||||
if ('unsupported' in r) continue; // five-bugs: its pseudo has mistakes by design
|
||||
const file = path.join(snapDir, `${p.name}.mojo`);
|
||||
wanted.add(`${p.name}.mojo`);
|
||||
if (process.env.UPDATE_SNAPSHOTS) fs.writeFileSync(file, r.source);
|
||||
assert.ok(fs.existsSync(file), `no snapshot for ${p.name}; run with UPDATE_SNAPSHOTS=1 and review the new file`);
|
||||
assert.equal(r.source, fs.readFileSync(file, 'utf8'), `the generated Mojo for ${p.name} changed; if the change is wanted, run with UPDATE_SNAPSHOTS=1 and review the diff`);
|
||||
}
|
||||
for (const f of fs.readdirSync(snapDir)) assert.ok(wanted.has(f), `snapshot ${f} belongs to no exercise`);
|
||||
});
|
||||
|
||||
test('the translation is literal: the declare-then-assign, index-loop, and total = total + 1 habits are kept', () => {
|
||||
const ex = EXERCISES.find((e) => e.id === 'count-base');
|
||||
const r = pseudoToMojo(ex.sources.pseudo, { call: ex.call });
|
||||
for (const line of [' var n: Int', ' var total: Int', ' n = t.count_codepoints()', ' total = 0', ' for i in range(n):', ' if String(t[codepoint=i]) == b:', ' total = total + 1', ' return total']) {
|
||||
assert.ok(r.source.split('\n').includes(line), `missing: ${line}`);
|
||||
}
|
||||
assert.ok(!r.source.includes('+='), 'the translator never rewrites total = total + 1');
|
||||
});
|
||||
|
||||
test('the translator does not import or mention the best-practices helper', () => {
|
||||
const text = fs.readFileSync(path.join(here, '..', 'src', 'sync', 'pseudo-to-mojo.ts'), 'utf8');
|
||||
assert.ok(!/practices/i.test(text), 'src/sync/pseudo-to-mojo.ts refers to the helper');
|
||||
});
|
||||
|
||||
test('every generated source starts with the generated-code header and has a line map that fits', () => {
|
||||
for (const p of programs()) {
|
||||
const r = pseudoToMojo(p.pseudo, { call: p.call });
|
||||
if ('unsupported' in r) continue;
|
||||
const lines = r.source.replace(/\n$/, '').split('\n');
|
||||
assert.equal(lines[0], GENERATED_HEADER, p.id);
|
||||
assert.equal(r.map.length, lines.length, `${p.id}: one map entry per line`);
|
||||
assert.equal(r.map[0], null, p.id);
|
||||
const pseudoLines = p.pseudo.split('\n').length;
|
||||
for (const m of r.map) assert.ok(m === null || (Number.isInteger(m) && m >= 1 && m <= pseudoLines), `${p.id}: map entry ${m}`);
|
||||
}
|
||||
});
|
||||
|
||||
test('the map links Mojo lines to the pseudo lines they came from', () => {
|
||||
const ex = EXERCISES.find((e) => e.id === 'count-base');
|
||||
const r = pseudoToMojo(ex.sources.pseudo, { call: ex.call });
|
||||
const lines = r.source.split('\n');
|
||||
const pseudo = ex.sources.pseudo.split('\n');
|
||||
const at = lines.findIndex((l) => l.includes('total = total + 1'));
|
||||
assert.ok(pseudo[r.map[at] - 1].includes('Total <- Total + 1'), pseudo[r.map[at] - 1]);
|
||||
});
|
||||
|
||||
test('grounding: every standard-library name the translator writes is in the harvested index, with the module it imports from', () => {
|
||||
const structs = MOJO_STD_STRUCTS;
|
||||
const exported = (n) => MOJO_STD_EXPORTS[n] ?? (structs[n] ? 'std' : undefined);
|
||||
for (const [name, mod] of Object.entries(USED_STDLIB)) assert.equal(MOJO_STD_EXPORTS[name], mod, `${name} should come from ${mod}`);
|
||||
// names the index leaves out are named here, with why: Float64 is a SIMD alias, abs is exported from two modules
|
||||
const NOT_INDEXED = { Float64: 'an alias of SIMD', abs: 'exported from two modules, so the index leaves it out', Int: 'a builtin type' };
|
||||
for (const name of USED_PRELUDE) assert.ok(exported(name) || NOT_INDEXED[name], `${name} is not in the harvested index`);
|
||||
});
|
||||
|
||||
test('every import in a generated source is one the translator knows, and no import is unused', () => {
|
||||
for (const p of programs()) {
|
||||
const r = pseudoToMojo(p.pseudo, { call: p.call });
|
||||
if ('unsupported' in r) continue;
|
||||
const body = r.source.split('\n').filter((l) => !l.startsWith('from ')).join('\n');
|
||||
for (const m of r.source.matchAll(/^from (\S+) import (.+)$/gm)) {
|
||||
for (const name of m[2].split(', ')) {
|
||||
assert.equal(USED_STDLIB[name], m[1], `${p.id}: ${m[0]}`);
|
||||
assert.ok(new RegExp(`\\b${name}\\b`).test(body), `${p.id}: ${name} is imported and never used`);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('main is written from the call: literals follow the parameter types (TRUE, character literals, floats, lists)', () => {
|
||||
const pseudo = `FUNCTION F(B : BOOL, C : CHAR, X : FLOAT, L : LIST OF INTEGER) RETURNS BOOL
|
||||
RETURN B AND C == 'A' AND X > 0.5 AND LENGTH(L) > 1
|
||||
ENDFUNCTION
|
||||
`;
|
||||
const r = pseudoToMojo(pseudo, { call: "F(TRUE, 'A', 1, [3, 4])" });
|
||||
assert.ok(r.source.includes(' print(f(True, "A", 1.0, List[Int](3, 4)))'), r.source);
|
||||
assert.equal(checkMojo(r.source).diagnostics.filter((d) => d.severity === 'error').length, 0);
|
||||
const run = runMojo(checkMojo(r.source).module, r.source, {});
|
||||
assert.equal(run.stdout, 'True\n');
|
||||
});
|
||||
|
||||
test('main follows the pseudo\'s own lines, and a procedure call is not printed', () => {
|
||||
const r = pseudoToMojo('PROCEDURE SHOW(N : INTEGER)\n OUTPUT N\nENDPROCEDURE\n', { call: 'SHOW(4)' });
|
||||
assert.ok(r.source.includes('def main():\n show(4)\n'), r.source);
|
||||
const none = pseudoToMojo('FUNCTION F(N : INTEGER) RETURNS INTEGER\n RETURN N\nENDFUNCTION\n');
|
||||
assert.ok(!none.source.includes('def main'), 'with no call and no lines of its own there is nothing to run');
|
||||
});
|
||||
|
||||
test('raises is inferred through dictionary reads and through callers, and nothing else raises', () => {
|
||||
const pseudo = `FUNCTION GET(M : HASHMAP OF STRING : INTEGER, K : STRING) RETURNS INTEGER
|
||||
RETURN M[K]
|
||||
ENDFUNCTION
|
||||
FUNCTION TWICE(M : HASHMAP OF STRING : INTEGER, K : STRING) RETURNS INTEGER
|
||||
RETURN GET(M, K) * 2
|
||||
ENDFUNCTION
|
||||
FUNCTION PLAIN(N : INTEGER) RETURNS INTEGER
|
||||
RETURN N + 1
|
||||
ENDFUNCTION
|
||||
`;
|
||||
const r = pseudoToMojo(pseudo, { call: null });
|
||||
assert.ok(r.source.includes('def get(m: Dict[String, Int], k: String) raises -> Int:'), r.source);
|
||||
assert.ok(r.source.includes('def twice(m: Dict[String, Int], k: String) raises -> Int:'), r.source);
|
||||
assert.ok(r.source.includes('def plain(n: Int) -> Int:'), r.source);
|
||||
const withMain = pseudoToMojo(pseudo + 'OUTPUT PLAIN(1)\n');
|
||||
assert.ok(withMain.source.includes('def main():'), 'main does not raise when nothing it calls does');
|
||||
const raising = pseudoToMojo(pseudo + 'DECLARE M : HASHMAP OF STRING : INTEGER\nM["a"] <- 1\nOUTPUT TWICE(M, "a")\n');
|
||||
assert.ok(raising.source.includes('def main() raises:'), raising.source);
|
||||
});
|
||||
|
||||
test('a set needs its import, and the import comes from the harvested module', () => {
|
||||
const r = pseudoToMojo('TYPE Bases = SET OF CHAR\nDEFINE Purines (\'A\', \'G\') : Bases\nOUTPUT \'A\' IN Purines\n');
|
||||
assert.ok(r.source.includes('from std.collections import Set'), r.source);
|
||||
assert.equal(checkMojo(r.source).diagnostics.filter((d) => d.severity === 'error').length, 0);
|
||||
});
|
||||
|
||||
test('what has no faithful Mojo becomes a visible raise and a warning, never a guess', () => {
|
||||
const r = pseudoToMojo('DECLARE N : FLOAT\nN <- 1.0\nN <- RANDN()\nOUTPUT N\n');
|
||||
const gap = r.notes.find((n) => n.id === 'translate-gap');
|
||||
assert.ok(gap && gap.severity === 'warning' && gap.line === 3, JSON.stringify(r.notes ?? r));
|
||||
assert.ok(r.source.includes('raise Error("not translated: pseudo line 3")'), r.source);
|
||||
assert.ok(r.source.includes('def main() raises:'), 'the routine that raises says so');
|
||||
assert.equal(checkMojo(r.source).diagnostics.filter((d) => d.severity === 'error').length, 0, 'the result still checks');
|
||||
});
|
||||
|
||||
test('float literals are not folded the way Mojo folds them: 0.1 + 0.2 stays a Float64 sum', () => {
|
||||
const r = pseudoToMojo('OUTPUT 0.1 + 0.2 == 0.3\n');
|
||||
assert.ok(r.source.includes('Float64(0.1) + 0.2 == 0.3'), r.source);
|
||||
const run = runMojo(checkMojo(r.source).module, r.source, {});
|
||||
assert.equal(run.stdout, 'FALSE\n');
|
||||
});
|
||||
|
||||
test('quantifiers become loops written before the line that uses them, and agree with the pseudo', () => {
|
||||
const src = fs.readFileSync(path.join(corpusDir, '27-quantifiers.pseudo'), 'utf8');
|
||||
const r = pseudoToMojo(src);
|
||||
assert.ok(/var _all\d+ = True/.test(r.source) && /var _any\d+ = False/.test(r.source) && /var _sum\d+ = 0/.test(r.source), r.source);
|
||||
assert.equal(verdicts.get('27-quantifiers.pseudo')?.verdict, 'agree');
|
||||
});
|
||||
|
||||
test('a quantifier inside a loop condition is a gap, not a wrong answer', () => {
|
||||
const r = pseudoToMojo('DECLARE L : LIST OF INTEGER\nL <- [1, 2]\nWHILE EXISTS K IN [0, LENGTH(L)) : L[K] > 5\n L[0] <- 9\nENDWHILE\n');
|
||||
assert.ok(r.notes.some((n) => n.id === 'translate-gap'), JSON.stringify(r.notes));
|
||||
});
|
||||
Reference in new issue
Block a user