Initial commit
Test / gate (push) Failing after 19m0s

This commit is contained in:
alexm committed 2026-11-12 22:23:54 -05:00
commit 401084bab3
265 files changed
+69224

No files matched your search

+240
View File
@@ -0,0 +1,240 @@
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
// SPDX-License-Identifier: Apache-2.0
// The reference is only trustworthy if its examples run through the real parsers.
import test from 'node:test';
import assert from 'node:assert/strict';
import { REFERENCE, PSEUDO_REF, LINGO_REF, SPECO_REF, ASM_REF, MATHO_REF, explain, SPECO_SYMBOLS } from '../dist/common/reference.js';
import { parseMatho, lowerMatho } from '../dist/services/matho.js';
import { studyOf, runPseudo } from '../dist/services/pseudo.js';
import { run as runProgram } from '../dist/engines/pseudo/interp.js';
import { readLingo } from '../dist/services/lingo.js';
import { parseSpeco, toAscii, toMath } from '../dist/services/speco.js';
import { parseAsm } from '../dist/services/asm.js';
import { KEYWORDS, BUILTINS, SOFT_BUILTINS } from '../dist/engines/pseudo/lang.js';
import { OPS } from '../dist/engines/asm86/interp.js';
test('every pseudocode statement example parses and checks', () => {
const sec = PSEUDO_REF.sections.find((s) => s.id === 'pseudo-statements');
for (const e of sec.entries) {
const st = studyOf(e.example);
const errs = st.diagnostics.filter((d) => d.severity === 'error');
assert.equal(errs.length, 0, `${e.term}: ${errs.map((d) => `L${d.line} ${d.message}`).join(' | ')}`);
}
});
test('every keyword in the engine has a meaning in the reference, and vice versa', () => {
const sec = PSEUDO_REF.sections.find((s) => s.id === 'pseudo-keywords');
const listed = new Set(sec.entries.map((e) => e.term));
for (const k of KEYWORDS) assert.ok(listed.has(k), `keyword ${k} is missing from the reference`);
for (const e of sec.entries) assert.ok(e.meaning.length > 10, `${e.term} has no meaning`);
for (const b of Object.keys(BUILTINS)) assert.ok(explain('pseudo', b), `built-in ${b} has no hover`);
});
test('every lingo sentence example reads (inside a minimal routine where needed)', () => {
const failures = [];
// kinds for every name the examples mention, so a fragment reads the same way it does on the page
const kinds = 'T is a text. P is a text. S is a text. w is a text. A is a whole number. Score is a whole number.';
const setup = [
'Start with Count at 0.', 'Start with Best at 0.', 'Start with Run at 0.', 'Start with Left at 3.', 'Start with window at "AC".',
'Start with an empty list called Starts.', 'Start with an empty tally called Counts.', 'Start with an empty text called R.',
];
const defs = "The complement of b is 'T' when b is 'A', and 'N' otherwise.";
for (const sec of LINGO_REF.sections.filter((s) => s.id.startsWith('lingo-forms'))) {
for (const e of sec.entries) {
if (!e.example) continue;
const ex = e.example.trim();
const examples = ex.includes(' ') && !/^To /.test(ex) && !/ when /.test(ex) ? ex.split(/\s{2,}/) : [ex];
for (const one of examples) {
let src;
const head = 'To test T, P, S, w, A, B and Score:';
const pre = setup.map((l) => ' ' + l).join('\n');
if (/^The answer is/.test(one)) src = `${head}\n${pre}\n Start with i at 0.\n ${one}\n\n${kinds}`;
else if (/^To /.test(one)) src = `${one}\n${pre}\n The answer is Count.\n\n${kinds}`;
else if (/ when /.test(one) || /^The .* is /.test(one)) src = `To test P, T, S and b:\n Start with i at 0.\n If b is 'A', add 1 to i.\n The answer is the length of T plus the length of P plus the length of S plus i.\n\n${one}${/[.:]$/.test(one) ? '' : '.'}\n\nP is a text. T is a text. S is a text. b is a letter.`;
else if (/^(For|Repeat)/.test(one)) src = `${head}\n${pre}\n${/^For (each|every) .*\bi\b/.test(one) ? '' : ' Start with i at 0.\n'} ${one}\n Add 1 to Count.\n The answer is Count.\n\n${kinds}`;
else if (/^If /.test(one) && /:$/.test(one)) src = `${head}\n${pre}\n ${one}\n Add 1 to Count.\n The answer is Count.\n\n${kinds}`;
else if (sec.title === 'Tests') src = `${head}\n${pre}\n For each position i of T:\n Set b to the letter of T at i.\n ${/^If /.test(one) ? one : `If ${one}, add 1 to Count.`}\n The answer is Count.\n\n${kinds}`;
else if (sec.title === 'Values') src = `${head}\n${pre}\n For each position i of T:\n Set X to ${one}.\n The answer is Count.\n\n${kinds}`;
else src = `${head}\n${pre}\n Start with i at 0.\n${/\bb\b/.test(one) && !/\bB\b/.test(one) ? ' Set b to the letter of T at i.\n' : ''} ${one}\n The answer is Count.\n\n${/complement/.test(one) ? defs + '\n\n' : ''}${kinds}${/^Swap/.test(one) ? ' B is a whole number.' : ''}`;
const r = readLingo(src);
const tolerated = ['pronoun', 'kind-clash', 'unset-read', 'borrowed-input', 'which-letter', 'never-read', 'unreachable'];
const errs = r.diagnostics.filter((d) => d.severity === 'error' && !tolerated.includes(d.id));
if (errs.length) failures.push(`${e.term} → "${one}": ${errs.map((d) => d.id + ': ' + d.message.slice(0, 100)).join(' | ')}`);
}
}
}
assert.deepEqual(failures, []);
});
test('every lingo ambiguity example raises exactly the question it documents', () => {
const sec = LINGO_REF.sections.find((s) => s.id === 'lingo-questions');
for (const e of sec.entries) {
const r = readLingo(e.example);
assert.ok(r.diagnostics.some((d) => d.id === e.term), `${e.term}: got ${r.diagnostics.map((d) => d.id).join(', ')}`);
}
});
test('every speco example parses, and the symbol table round-trips', () => {
const sec = SPECO_REF.sections.find((s) => s.id === 'speco-forms');
for (const e of sec.entries) {
const m = parseSpeco(e.example);
assert.ok(m.ok, `${e.term}: ${m.diagnostics.map((d) => d.message).join(' | ')}`);
}
for (const s of SPECO_SYMBOLS) {
if (s.math.length !== 1) continue;
assert.equal(toAscii(`A ${s.math} B`), `A ${s.ascii} B`);
assert.equal(toMath(`A ${s.ascii} B`), `A ${s.math} B`);
}
});
test('every instruction has a gloss and a hover; setup examples parse', () => {
for (const op of Object.keys(OPS)) assert.ok(explain('asm', op)?.body, `${op} has no gloss`);
for (const e of ASM_REF.sections.find((s) => s.id === 'asm-registers').entries) assert.ok(explain('asm', e.term), e.term);
assert.ok(parseAsm(' mov rax, [rbx]\n ret\n').ok);
assert.ok(REFERENCE.length === 5);
});
test('every pseudo idiom runs and prints what its note says', async () => {
const { runPseudo } = await import('../dist/services/pseudo.js');
const { KEYWORDS: KW, BUILTINS: BI, CONTEXTUAL } = await import('../dist/engines/pseudo/lang.js');
const sec = PSEUDO_REF.sections.find((s) => s.id === 'pseudo-idioms');
assert.ok(sec.entries.length >= 8);
for (const e of sec.entries) {
const st = studyOf(e.example);
const errs = st.diagnostics.filter((d) => d.severity === 'error');
assert.equal(errs.length, 0, `${e.term}: ${errs.map((d) => `L${d.line} ${d.message}`).join(' | ')}`);
const tr = runPseudo(st, { input: [], maxSteps: 200000 });
assert.equal(tr.error, null, `${e.term}: ${tr.error?.message}`);
const out = tr.output.filter((l) => !/^\w+ returned /.test(l));
assert.deepEqual(out, [e.note], e.term);
}
const reserved = PSEUDO_REF.sections.find((s) => s.id === 'pseudo-reserved');
const words = new Set(reserved.entries.map((e) => e.term));
for (const k of KW) assert.ok(words.has(k), `reserved list misses keyword ${k}`);
for (const k of Object.keys(CONTEXTUAL)) assert.ok(words.has(k), `reserved list misses ${k}`);
// the built-ins added for numerical work stay usable as names (a variable E, a FUNCTION GCD), so they are not listed
for (const k of Object.keys(BI)) assert.ok(SOFT_BUILTINS.has(k) ? !words.has(k) : words.has(k), SOFT_BUILTINS.has(k) ? `${k} can be a name, so it is not reserved` : `reserved list misses built-in ${k}`);
});
test('every matho example translates to pseudo that checks, and every refusal is refused', () => {
const forms = MATHO_REF.sections.find((s) => s.id === 'matho-forms');
for (const e of forms.entries) {
const r = parseMatho(e.example);
const errs = r.diagnostics.filter((d) => d.severity === 'error');
assert.ok(r.ok, `${e.term}: ${errs.map((d) => `L${d.line} ${d.id}: ${d.message}`).join(' | ')}\n${r.program.pseudo}`);
const st = studyOf(r.program.pseudo);
assert.ok(st.ok, `${e.term}: the pseudo does not check: ${st.diagnostics.filter((d) => d.severity === 'error').map((d) => d.message).join(' | ')}`);
}
for (const src of ['y = \\int_0^1 x dx', 's = \\sum_{i=0}^{\\infty} x_i', 'y = \\lim_{x \\to 0} x', 'y = \\partial f', 'y = a \\pm b', 'a = b = c']) {
const r = parseMatho(src);
assert.equal(r.ok, false, `${src} should be refused`);
assert.ok(r.diagnostics.some((d) => d.severity === 'error' && /matho-(refused|pm|chain)/.test(d.id)), src);
}
});
/** A run of `src` (which must check), with runPseudo's defaults. */
const runOf = (src, opts = {}) => {
const st = studyOf(src);
assert.ok(st.ok, `${src}: ${st.diagnostics.filter((d) => d.severity === 'error').map((d) => d.message).join(' | ')}`);
return runPseudo(st, opts);
};
const errorIds = (src) => studyOf(src).diagnostics.filter((d) => d.severity === 'error').map((d) => d.id);
test('every row of "What stops a run" is a stop a run makes', () => {
// [program, the kind of stop, run options], one or more per row
const SHOWN = {
'Position does not exist': [['DECLARE L : LIST OF INTEGER <- [1]\nOUTPUT L[1]', 'index']],
'No value yet': [['DECLARE X : INTEGER\nOUTPUT X', 'unset']],
'Missing key': [['DECLARE H : HASHMAP OF STRING : INTEGER\nOUTPUT H["a"]', 'key']],
'Division by zero': [['DECLARE Y : INTEGER <- 0\nOUTPUT 1 DIV Y', 'divide'], ['DECLARE Y : FLOAT <- 0.0\nOUTPUT 1.0 / Y', 'divide'], ['OUTPUT 0.0 ^ -1', 'value']],
'No answer': [['OUTPUT 2 ^ -1', 'value'], ['OUTPUT (-8.0) ^ 0.5', 'value'], ['OUTPUT SQRT(-1.0)', 'value'], ['OUTPUT TO_FLOAT("0x10")', 'value'], ['DECLARE L : LIST OF INTEGER\nOUTPUT MIN(L)', 'value']],
'Wrong length': [['FUNCTION F(A : ARRAY[3] OF INTEGER) RETURNS INTEGER\n RETURN A[0]\nENDFUNCTION\nDECLARE L : LIST OF INTEGER <- [1, 2]\nOUTPUT F(L)', 'value'], ['OUTPUT DOT([1, 2], [3])', 'value']],
'STEP 0': [['DECLARE S : INTEGER <- 0\nFOR I <- 0 TO 3 STEP S\n OUTPUT I\nNEXT I', 'value']],
'Too large': [['OUTPUT 9007199254740991 + 1', 'overflow'], ['DECLARE X : INT8\nFOR X <- 120 TO 130\n OUTPUT X\nNEXT X', 'overflow'], ['DECLARE X : FLOAT32 <- 3e38\nX <- X * 10.0', 'overflow'], ['OUTPUT DOT([94906267, 1], [94906267, -9007199254740991])', 'overflow']],
'Too much to hold': [['DECLARE L : LIST OF INTEGER <- FILL(2000001, 0)', 'value'], ['DECLARE S : STRING <- "ab"\nFOR I <- 0 TO 30\n S <- S & S\nNEXT I', 'value']],
'Input': [['DECLARE X : FLOAT\nINPUT X', 'input', { input: ['0x1A'] }], ['DECLARE X : INTEGER\nINPUT X', 'input']],
'A value from outside': [['PROCEDURE P(X : FLOAT)\n OUTPUT X\nENDPROCEDURE', 'value', { call: 'P(TRUE)' }]],
'A broken promise': [['ASSERT 1 > 2', 'assert']],
'No end in sight': [['WHILE TRUE\n OUTPUT 1\nENDWHILE', 'limit'], ['OUTPUT SUM I IN [0, 2000000) : SUM J IN [0, 2000000) : 1', 'limit']],
'Too deep': [['FUNCTION F(N : INTEGER) RETURNS INTEGER\n RETURN F(N + 1)\nENDFUNCTION\nOUTPUT F(0)', 'depth']],
'A data race': [['DECLARE T : INTEGER <- 0\nPARALLEL FOR I <- 0 TO 4\n T <- T + I\nNEXT I', 'race']],
};
const sec = PSEUDO_REF.sections.find((s) => s.id === 'pseudo-stops');
for (const e of sec.entries) {
assert.ok(SHOWN[e.term], `"${e.term}" has no program in this test`);
for (const [src, kind, opts] of SHOWN[e.term]) {
const r = opts?.call ? (() => { try { return runOf(src, opts); } catch (err) { return { error: { kind: 'value', message: err.message } }; } })() : runOf(src, opts);
assert.equal(r.error?.kind, kind, `${e.term}: ${src}`);
}
}
assert.deepEqual(Object.keys(SHOWN).sort(), sec.entries.map((e) => e.term).sort());
// a value from outside that is NaN or an infinity, through the engine's own run()
const p = studyOf('PROCEDURE P(X : FLOAT)\n OUTPUT X\nENDPROCEDURE').program.program;
assert.equal(runProgram(p, { call: { name: 'P', args: [NaN] } }).error?.kind, 'value');
});
test('the limits the reference lists are the ones the lab applies', () => {
const sec = PSEUDO_REF.sections.find((s) => s.id === 'pseudo-limits');
const terms = sec.entries.map((e) => e.term);
for (const t of ['200 levels of nesting', '400 operands in a chain', '2,000,000 values', '10,000,000 characters', '20,000 steps', '400 calls deep', '10,000,000 recorded values']) assert.ok(terms.includes(t), t);
assert.ok(errorIds(`${'IF TRUE THEN\n'.repeat(201)}OUTPUT 1\n${'ENDIF\n'.repeat(201)}`).includes('too-deep'));
assert.deepEqual(errorIds(`${'IF TRUE THEN\n'.repeat(150)}OUTPUT 1\n${'ENDIF\n'.repeat(150)}`), []);
assert.ok(errorIds(`OUTPUT 1${' + 1'.repeat(400)}`).includes('too-deep'));
assert.deepEqual(runOf(`OUTPUT 1${' + 1'.repeat(399)}`).output, ['400']);
assert.ok(errorIds(`OUTPUT ((1${' + 1'.repeat(398)})${' + 1'.repeat(398)})${' + 1'.repeat(398)}`).includes('too-deep'), 'brackets that each hold a long chain');
assert.equal(runOf('DECLARE A : ARRAY[2000001] OF INTEGER').error?.kind, 'value');
assert.equal(runOf('DECLARE A : ARRAY[2000000] OF BOOL\nOUTPUT LENGTH(A)').error, null);
assert.equal(runOf('FOR I <- 0 TO 30000\nNEXT I').error?.kind, 'limit');
assert.match(runOf('FOR I <- 0 TO 30000\nNEXT I').error.message, /20,000 steps/);
const deep = (n) => runOf(`FUNCTION F(N : INTEGER) RETURNS INTEGER\n IF N == 0 THEN\n RETURN 0\n ENDIF\n RETURN F(N - 1)\nENDFUNCTION\nOUTPUT F(${n})`);
assert.equal(deep(398).error, null);
assert.equal(deep(400).error?.kind, 'depth');
});
test('the glosses the claims ledger found a run contradicting now say what a run does', () => {
const out = (src) => { const r = runOf(src); assert.equal(r.error, null, r.error?.message); return r.output; };
const meaning = (id, term) => PSEUDO_REF.sections.flatMap((s) => s.entries).find((e) => e.term === term)?.meaning ?? (BUILTINS[term]?.doc || '');
// CONSTANT: worked out once, built-ins and a random draw included; a variable is refused
assert.match(meaning('', 'CONSTANT <name> = <value>'), /worked out once/);
assert.deepEqual(out('CONSTANT A = 2 + 3\nCONSTANT B = LENGTH("ACGT")\nCONSTANT R = RAND(1.0)\nOUTPUT A, " ", B, " ", R == R'), ['5 4 TRUE']);
assert.ok(errorIds('DECLARE X : INTEGER <- 1\nCONSTANT A = X').length > 0);
// RAND: between 0 and X, never X; 0.0 for 0
assert.match(BUILTINS.RAND.doc, /\(X, 0\] for X below 0/);
assert.deepEqual(out('OUTPUT RAND(0.0), " ", RAND(-5.0) <= 0.0, " ", RAND(-5.0) > -5.0'), ['0.0 TRUE TRUE']);
// the ranges of ATAN2, ATAN and TANH include their ends
assert.match(BUILTINS.ATAN2.doc, /\[-PI\(\), PI\(\)\]/);
assert.match(BUILTINS.ATAN.doc, /\[-PI\(\)\/2, PI\(\)\/2\]/);
assert.match(BUILTINS.TANH.doc, /\[-1, 1\]/);
assert.deepEqual(out('OUTPUT ATAN2(-0.0, -1.0) == -PI(), " ", ATAN(1.0e300) == PI() / 2, " ", TANH(20.0) == 1.0'), ['TRUE TRUE TRUE']);
// POW: 0 to a negative power stops
assert.match(BUILTINS.POW.doc, /nor 0 when Y is negative/);
assert.equal(runOf('OUTPUT POW(0.0, -1.0)').error?.kind, 'value');
// TO_INTEGER of a CHAR digit is the digit; TO_FLOAT reads decimal text
assert.match(BUILTINS.TO_INTEGER.doc, /TO_INTEGER\('7'\) is 7/);
assert.deepEqual(out("OUTPUT TO_INTEGER('7'), \" \", TO_FLOAT(\"-1e3\")"), ['7 -1000.0']);
// UPPER and LOWER: a STRING may grow, a CHAR whose other case is two characters stays
assert.match(BUILTINS.UPPER.doc, /STRASSE/);
assert.deepEqual(out("DECLARE C : CHAR <- UPPER('ß')\nOUTPUT C, \" \", UPPER(\"straße\")"), ['ß STRASSE']);
// FLOAT32: arithmetic and stores round to a single; SQRT and its kind give a FLOAT
assert.match(meaning('', 'FLOAT32'), /SQRT, EXP and the other functions give a FLOAT/);
assert.match(meaning('', 'INTEGER'), /from -9,007,199,254,740,991 to 9,007,199,254,740,991/);
assert.match(meaning('', 'Division by zero'), /with USE IEEE, a FLOAT \/ gives an infinity/);
assert.deepEqual(out('DECLARE X : FLOAT32 <- 1.1\nOUTPUT EXACT(X ^ 3), " ", EXACT(SQRT(FLOAT32(2.0)))'), ['1.3310000896453857421875 1.4142135623730951454746218587388284504413604736328125']);
assert.ok(errorIds('DECLARE X : FLOAT32 <- 2.0\nDECLARE Y : FLOAT32\nY <- SQRT(X)').includes('type'));
// INTEGER: the run's range, both ends
assert.equal(runOf('OUTPUT -9007199254740991 - 1').error?.kind, 'overflow');
// Division by zero: with USE IEEE a FLOAT / gives an infinity; DIV still stops
assert.deepEqual(out('USE IEEE\nOUTPUT 1.0 / 0.0'), ['infinity']);
assert.equal(runOf('USE IEEE\nOUTPUT 1 DIV 0').error?.kind, 'divide');
// speco INVARIANT: in FOR EACH, a claim that names the loop's variable is not checked after the last pass; in a
// FOR the exit check sees the counter at the value that ended the loop
const inv = SPECO_REF.sections.flatMap((s) => s.entries).find((e) => e.term === 'INVARIANT <claim>').meaning;
assert.match(inv, /except in FOR EACH/);
assert.deepEqual(out('DECLARE T : INTEGER <- 0\nFOR EACH X IN [1, 2, 3]\n INVARIANT X > 0 AND T < 4\n T <- T + X\nNEXT X\nOUTPUT T'), ['6']);
assert.equal(runOf('DECLARE T : INTEGER <- 0\nFOR EACH X IN [1, 2, 3]\n INVARIANT T < 4\n T <- T + X\nNEXT X\nOUTPUT T').error?.kind, 'invariant');
assert.equal(runOf('FOR I <- 0 TO 3\n INVARIANT I < 3\nNEXT I').error?.kind, 'invariant');
// matho: FLOOR and CEIL do not make their operand INTEGER; MOD does
const lowered = (tex) => lowerMatho(parseMatho(tex), 'pseudo').source;
assert.match(lowered('y = \\lfloor n \\rfloor'), /N : FLOAT/);
assert.match(lowered('r = a \\bmod b'), /A : INTEGER/);
});