/** * The problem catalog, built from the exercise registry (src/exercises/). * Each exercise carries whichever layers the course wrote for it; a speco * spec is extracted from the pseudocode's own contracts when none is given. */ import { EXERCISES, type Exercise } from '../exercises/index.js'; import type { LangId } from './types.js'; import { defaultAreaFor, type AreaId } from './areas.js'; import { emptyState, type PlayState } from './state.js'; import { extractSpec } from '../services/speco.js'; import { MOJO_PROGRAMS, type MojoProgram } from '../mojo/generated/programs.js'; import { MOJO_LISTINGS } from '../mojo/generated/listings.js'; export interface Problem extends Exercise { lang: LangId; /** The area it opens in from the problem list. */ area: AreaId; /** The exercise's Mojo program and godbolt's verdict on it, when it names one. */ mojo?: MojoProgram & { id: string }; } function problem(e: Exercise): Problem { const sources = { ...e.sources }; const mojo = e.mojoProgram ? MOJO_PROGRAMS[e.mojoProgram] : undefined; if (e.mojoProgram && !mojo) throw new Error(`exercise ${e.id} names Mojo program ${e.mojoProgram}, which is not in mojo/local/`); if (mojo && !sources.mojo) sources.mojo = mojo.source; // the compiler's listing for the program, when `pixi run play` has stored one, fills an x86-64 pane the exercise left empty const listing = e.mojoProgram ? MOJO_LISTINGS[e.mojoProgram] : undefined; const fromListing = !!listing && !sources.asm; if (listing && fromListing) sources.asm = listing.asm; if (sources.pseudo && !sources.speco) { const spec = extractSpec(sources.pseudo); if (spec) sources.speco = spec; } return { ...e, sources, area: defaultAreaFor({ id: e.id, sources: e.sources, area: e.area, mojoProgram: e.mojoProgram }), setup: e.setup ?? (fromListing ? listing!.setup : undefined), mojo: mojo && { ...mojo, id: e.mojoProgram! }, lang: e.lang ?? (sources.mojo ? 'mojo' : sources.pseudo ? 'pseudo' : sources.lingo ? 'lingo' : 'asm') }; } export const PROBLEMS: Problem[] = EXERCISES.map(problem); export function problemById(id: string): Problem | undefined { return PROBLEMS.find((p) => p.id === id); } export function stateForProblem(p: Problem): PlayState { const s = emptyState(); s.problem = p.id; s.title = p.title; s.sources = { ...p.sources }; s.lang = p.lang; s.call = p.call ?? null; s.setup = p.setup; s.machine = p.machine; s.view = p.view ?? 'trace'; return s; }