// Stage 5: pseudo -> lingo. What is written must read (readSteps accepts it), must read as the same // routines, must behave like the original once lowered back to pseudo, and writing it again from that // pseudo must give the same lingo (a fixed point, comments aside). A program with no English form is // reported as unsupported with a reason; the translator never writes lingo the reader would reject. import test from 'node:test'; import assert from 'node:assert/strict'; import { programs, stripComments } from './helpers/sync-programs.mjs'; import { studyOf, runPseudo } from '../dist/services/pseudo.js'; import { readLingo, lowerLingo } from '../dist/services/lingo.js'; import { pseudoToLingo } from '../dist/sync/pseudo-to-lingo.js'; globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } }; const SUPPORTED_AT_LEAST = 31; // raise as English learns more forms; counted below const run = (pseudo, call, input) => { const st = studyOf(pseudo); assert.ok(st.ok, 'pseudo should study'); const t = runPseudo(st, { call, input, maxSteps: 200000 }); return { out: t.output.map((l) => l.replace(/^\w+ returned /, 'returned ')), err: t.error ? t.error.kind : null }; }; function judge(p) { if (!studyOf(p.pseudo).ok) return { verdict: 'no-pseudo' }; const r = pseudoToLingo(p.pseudo); if ('unsupported' in r) return { verdict: 'unsupported', r }; const parsed = readLingo(r.source); if (!parsed.ok) return { verdict: 'unread', detail: parsed.diagnostics.map((d) => d.message).join(' | '), r }; const low = lowerLingo(parsed, 'pseudo'); if (!low) return { verdict: 'unlowered', r }; return { verdict: 'ok', r, low }; } const verdicts = new Map(); test('every program: the lingo reads, lowers to pseudo that behaves like the original, and is a fixed point; or the program is reported unsupported', () => { const problems = []; let supported = 0; for (const p of programs()) { const j = judge(p); verdicts.set(p.id, j); if (j.verdict === 'no-pseudo' || j.verdict === 'unsupported') continue; if (j.verdict !== 'ok') { problems.push(`${p.id}: ${j.verdict}: ${j.detail ?? ''}`); continue; } supported++; // same behavior: the call (or the input-driven main) gives the same output and the same kind of stop const want = run(p.pseudo, p.call, p.input); let got; try { got = run(j.low.source, p.call, p.input); } catch (e) { problems.push(`${p.id}: lowered pseudo does not study: ${e.message}`); continue; } if (JSON.stringify(want) !== JSON.stringify(got)) problems.push(`${p.id}: behavior differs: ${JSON.stringify(want).slice(0, 120)} vs ${JSON.stringify(got).slice(0, 120)}`); // fixed point const again = pseudoToLingo(j.low.source); if ('unsupported' in again) { problems.push(`${p.id}: the lowered pseudo is no longer supported`); continue; } if (stripComments(again.source) !== stripComments(j.r.source)) problems.push(`${p.id}: not a fixed point`); } assert.deepEqual(problems, []); assert.ok(supported >= SUPPORTED_AT_LEAST, `${supported} programs have English, fewer than ${SUPPORTED_AT_LEAST}`); }); test('a program with no English form is unsupported with a located reason, not a crash', () => { let n = 0; for (const p of programs()) { const j = verdicts.get(p.id) ?? judge(p); if (j.verdict !== 'unsupported') continue; n++; assert.ok(j.r.unsupported.length > 0, p.id); for (const d of j.r.unsupported) { assert.ok(Number.isInteger(d.line) && d.line >= 1, `${p.id}: line`); assert.ok(d.message.length > 0, p.id); } } assert.ok(n > 0); }); test('the line map has one entry per output line and points inside the pseudo', () => { for (const p of programs()) { const j = verdicts.get(p.id) ?? judge(p); if (j.verdict !== 'ok') continue; const lines = j.r.source.replace(/\n$/, '').split('\n'); assert.equal(j.r.map.length, lines.length, p.id); const n = p.pseudo.split('\n').length; for (const m of j.r.map) assert.ok(m === null || (Number.isInteger(m) && m >= 1 && m <= n), `${p.id}: ${m}`); } }); test('the English is plain: a counting loop and a sum read the way a student would say them', () => { const r = pseudoToLingo(`FUNCTION SUM_TO(N : INTEGER) RETURNS INTEGER DECLARE Total : INTEGER Total <- 0 FOR I <- 1 TO N Total <- Total + I NEXT I RETURN Total ENDFUNCTION `); assert.ok(!('unsupported' in r), JSON.stringify(r)); assert.ok(readLingo(r.source).ok, r.source); assert.ok(/Total/.test(r.source)); }); test('pseudo that does not parse is unsupported, and records have no English form', () => { const bad = pseudoToLingo('FUNCTION F(\n'); assert.ok('unsupported' in bad && bad.unsupported.length > 0); const rec = pseudoToLingo(`RECORD P X : INTEGER ENDRECORD FUNCTION F(A : INTEGER) RETURNS INTEGER DECLARE R : P R.X <- A RETURN R.X ENDFUNCTION `); assert.ok('unsupported' in rec, 'records are left unsupported by design'); });