What programs mean (the languages, translators, Mojo checker and
interpreter, prover, fuzzer, machine model, catalog problems and the Mojo
corpus with godbolt's stored answers) was mixed into the site's tree, so a
change to the prover and a change to a button went through the same gate
and the same docs. This moves all of it under core/, laid out as its own
repository, ready to be extracted with its history and pinned here as a
git submodule.
- core/: src/{engines,services,sync,mojo,exercises,curriculum,common},
mojo/, syntax/mojo.xml, its tests (with Workspace and workForProblem in
place of the site's Session), scripts (corpus pipeline, play, the
exercise and probe starters, engines export/diff), and its docs
(engines, trust, mojo-corpus, exercises, coverage). Its own pixi.toml,
package.json, README, AGENTS.md and CI workflow.
- core/tests/boundary.test.mjs: library modules use no DOM, import no
page widget, and import nothing outside src/.
- core/scripts/cli.mjs: pixi run cli check|run|prove|fuzz|translate.
- core/scripts/dafny.py: Dafny 4.11.0 pinned by version and SHA-256 per
platform, fetched into .tools/ (pixi run dafny-fetch, pixi run dafny).
- The site imports core only as #core/<path>.js: an import map in
index.html, "imports" in package.json for node, core's declarations for
tsc. The site's TypeScript builds core first (npm run build).
- assemble.sh, the Quarto engine copy, the render cache key, check-pages,
quarto-export and quarto-css read core/dist and core/mojo; quarto-css
copies core's syntax/mojo.xml into the extension.
- Site tests that covered engines moved to core; the rest keep Session.
- README, AGENTS.md, docs/app.md, docs/deploy.md and the CI workflow
describe the split (checkout with submodules; core's tests and corpus
status run against the pinned commit).
Gate: site 110 pass; core 307 pass; corpus status --strict passes.
Manual check: the assembled site opens Bridge (count-base-proved),
Developer and Hardware (count-base) with the editor, the stepper and the
board, and no module errors.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
103 lines
4.6 KiB
TypeScript
103 lines
4.6 KiB
TypeScript
/**
|
|
* lingo: the controlled-English service. Wraps `readSteps` from the course's
|
|
* english.js. Today the engine lowers English to pseudocode *text* and
|
|
* re-parses it; this adapter exposes that as `lower(..., 'pseudo')` with the
|
|
* engine's own line map, and runs English programs through the pseudocode
|
|
* service so the trace is the same shape.
|
|
*/
|
|
import { readSteps, SENTENCE_FORMS } from '../engines/pseudo/english.js';
|
|
import type { Diagnostic, LanguageService, Lowered, Parsed, RunOptions, Trace } from '../common/types.js';
|
|
import { toDiagnostic, studyOf, runPseudo, defaultCall as pseudoDefaultCall } from './pseudo.js';
|
|
|
|
export type Reading = any;
|
|
|
|
export function readLingo(source: string): Parsed<Reading> {
|
|
const r = readSteps(source);
|
|
const diagnostics: Diagnostic[] = r.diags.map((d: any) => toDiagnostic(d, 'lingo'));
|
|
return { program: r, diagnostics, ok: !!r.ok };
|
|
}
|
|
|
|
export function lowerLingo(parsed: Parsed<Reading>, target: string): Lowered | null {
|
|
const r = parsed.program;
|
|
if (!r || !r.pseudo) return null;
|
|
const lowered: Lowered = {
|
|
target: 'pseudo',
|
|
source: r.pseudo,
|
|
// lineMap[i] describes pseudocode line i+1 and names the English line it came from
|
|
map: (r.lineMap || []).map((m: any, i: number) => ({ from: m?.line ?? 0, to: i + 1 })).filter((m: { from: number }) => m.from > 0),
|
|
notes: [],
|
|
};
|
|
if (target === 'pseudo') return lowered;
|
|
return null;
|
|
}
|
|
|
|
/** Run an English program: lower it, then run the pseudocode with the English line numbers mapped back. */
|
|
export function runLingo(parsed: Parsed<Reading>, options: RunOptions): Trace {
|
|
const low = lowerLingo(parsed, 'pseudo');
|
|
if (!low) throw new Error('The steps do not read yet; answer the questions in the problems panel first.');
|
|
const st = studyOf(low.source);
|
|
if (!st.ok) throw new Error('The pseudocode these steps became has an error: ' + (st.diagnostics.find((d) => d.severity === 'error')?.message || ''));
|
|
// The call in the toolbar usually names the pseudocode pane's routine; aim it at the English routine instead.
|
|
let call = options.call ?? null;
|
|
const subs: Map<string, any> = st.program.program.subs;
|
|
const main = parsed.program.main;
|
|
const m = call ? /^\s*([A-Za-z_]\w*)\s*\((.*)\)\s*$/s.exec(call) : null;
|
|
if (m && main && !subs.has(m[1].toUpperCase())) {
|
|
const sub = subs.get(String(main.pseudo).toUpperCase());
|
|
const argc = m[2].trim() ? m[2].split(/,(?![^\[\]{}()]*[\]})])/).length : 0;
|
|
call = sub && sub.params.length === argc ? `${sub.name}(${m[2]})` : null;
|
|
}
|
|
const tr = runPseudo(st, { ...options, call: call ?? pseudoDefaultCall(st.program) });
|
|
const toEnglish = new Map(low.map.map((m) => [m.to, m.from]));
|
|
const hits = new Map<number, number>();
|
|
for (const [line, n] of tr.hits) { const e = toEnglish.get(line); if (e) hits.set(e, (hits.get(e) || 0) + n); }
|
|
return {
|
|
...tr,
|
|
lang: 'lingo',
|
|
hits,
|
|
steps: tr.steps.map((s: any) => ({ ...s, pseudoLine: s.line, line: toEnglish.get(s.line) ?? s.line })),
|
|
error: tr.error ? { ...tr.error, line: tr.error.line != null ? toEnglish.get(tr.error.line) ?? tr.error.line : null } : null,
|
|
};
|
|
}
|
|
|
|
export function defaultCallLingo(parsed: Parsed<Reading>): string | null {
|
|
const low = lowerLingo(parsed, 'pseudo');
|
|
if (!low) return null;
|
|
const st = studyOf(low.source);
|
|
return st.ok ? pseudoDefaultCall(st.program) : null;
|
|
}
|
|
|
|
const STEP_WORDS = /^(to|start|let|set|make|add|append|put|insert|subtract|increase|decrease|multiply|double|halve|count|remove|sort|swap|show|print|check|if|otherwise|for|repeat|while|until|stop|skip|the answer|example)\b/i;
|
|
|
|
export function tokenizeLingo(line: string): Array<{ text: string; cls: string }> {
|
|
const out: Array<{ text: string; cls: string }> = [];
|
|
const re = /("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)?')|(\b\d+(?:\.\d+)?\b)|(\b[A-Za-z][A-Za-z0-9_]*\b)|(\s+)|(.)/g;
|
|
let m: RegExpExecArray | null;
|
|
let first = true;
|
|
while ((m = re.exec(line))) {
|
|
const [text, s, n, w] = m;
|
|
let cls = '';
|
|
if (s) cls = 'tok-string';
|
|
else if (n) cls = 'tok-number';
|
|
else if (w) {
|
|
if (first && STEP_WORDS.test(line.trim())) cls = 'tok-keyword';
|
|
else if (/^[A-Z]/.test(w) || w.length === 1) cls = 'tok-name';
|
|
first = false;
|
|
}
|
|
out.push({ text, cls });
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export const sentenceForms: Array<{ job: string; example: string }> = (SENTENCE_FORMS as any[]).map((f) => ({ job: f.job || f.label || '', example: f.example || f.text || String(f) }));
|
|
|
|
export const lingoService: LanguageService<Reading> = {
|
|
id: 'lingo',
|
|
name: 'lingo',
|
|
parse: readLingo,
|
|
run: runLingo,
|
|
lower: lowerLingo,
|
|
defaultCall: defaultCallLingo,
|
|
tokenize: tokenizeLingo,
|
|
};
|