From 977117504fb954443638d813601b10b41f6e46fb Mon Sep 17 00:00:00 2001 From: Alex Maldonado Date: Mon, 5 Oct 2026 05:16:27 +0000 Subject: [PATCH] Give the catalog, the machine presets and AreaId homes on the core side Two engine modules reached into app modules: the exercise type named the app's AreaId, and the assembly service read the machine presets from the Hardware area's board module. And the catalog mixed core's problems with the app's areas and saved state. - core/machine.ts holds the machine presets and their names (parseMachine, machineSpec, machineLabel); hardware.ts re-exports them for the board. - AreaId lives in core/types.ts; areas.ts re-exports it. - core/problems.ts builds the problems from the exercises and the corpus (PROBLEMS, problemById, workForProblem for a Workspace). core/catalog.ts adds each problem's area and stateForProblem. No behavior changes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ --- src/core/areas.ts | 3 ++- src/core/catalog.ts | 34 +++++-------------------------- src/core/hardware.ts | 30 +++------------------------- src/core/machine.ts | 33 +++++++++++++++++++++++++++++++ src/core/problems.ts | 45 ++++++++++++++++++++++++++++++++++++++++++ src/core/types.ts | 3 +++ src/exercises/types.ts | 2 +- src/services/asm.ts | 4 ++-- 8 files changed, 94 insertions(+), 60 deletions(-) create mode 100644 src/core/machine.ts create mode 100644 src/core/problems.ts diff --git a/src/core/areas.ts b/src/core/areas.ts index 1d43b7b..718d9b9 100644 --- a/src/core/areas.ts +++ b/src/core/areas.ts @@ -8,7 +8,8 @@ import type { LangId } from './types.js'; import type { DockTab, PlayState } from './state.js'; import { LINKED } from '../sync/hub.js'; -export type AreaId = 'bridge' | 'dev' | 'hw'; +export type { AreaId } from './types.js'; +import type { AreaId } from './types.js'; /** The tabs beside the editor: the views that read a run. */ export type InspectId = 'trace' | 'table' | 'flow' | 'data' | 'life' | 'proof' | 'tests'; diff --git a/src/core/catalog.ts b/src/core/catalog.ts index a8f5f53..163fa46 100644 --- a/src/core/catalog.ts +++ b/src/core/catalog.ts @@ -1,41 +1,17 @@ /** - * 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. + * The app's problem catalog: core's problems (core/problems.ts), each with the + * area it opens in, and the saved state a problem opens with. */ -import { EXERCISES, type Exercise } from '../exercises/index.js'; -import type { LangId } from './types.js'; +import { PROBLEMS as CORE_PROBLEMS, type Problem as CoreProblem } from './problems.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; +export interface Problem extends CoreProblem { /** 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 const PROBLEMS: Problem[] = CORE_PROBLEMS.map((p) => ({ ...p, area: defaultAreaFor({ id: p.id, sources: p.sources, area: p.area, mojoProgram: p.mojoProgram }) })); export function problemById(id: string): Problem | undefined { return PROBLEMS.find((p) => p.id === id); diff --git a/src/core/hardware.ts b/src/core/hardware.ts index 0c093de..1a2d93d 100644 --- a/src/core/hardware.ts +++ b/src/core/hardware.ts @@ -5,35 +5,11 @@ * here runs under `node --test`, so the board's data steps are tested without a browser. */ import { REGS } from '../engines/asm86/interp.js'; -import { PRESETS } from '../engines/asm86/machine.js'; -/* ---------- the machine ---------- */ +/* ---------- the machine (core/machine.ts) ---------- */ -/** The machine models a run can use: `teaching` (small caches, so misses happen on small data), `realistic` (shaped like a desktop core), `ideal`. */ -export const MACHINE_PRESETS: readonly string[] = Object.keys(PRESETS); - -/** A machine as the state names it: a preset, with virtual memory (paging and a TLB) on or off. `teaching+paging` is the teaching machine with paging on. */ -export interface MachineChoice { preset: string; paging: boolean } - -const PAGING = '+paging'; - -export function parseMachine(name: string | null | undefined): MachineChoice { - const raw = String(name ?? '').trim(); - const paging = raw.endsWith(PAGING); - const preset = paging ? raw.slice(0, -PAGING.length) : raw; - return { preset: MACHINE_PRESETS.includes(preset) ? preset : 'teaching', paging }; -} - -export const machineName = (c: MachineChoice): string => `${c.preset}${c.paging ? PAGING : ''}`; - -/** What the engine's hardware model is given (`trace.spec.machine`): the preset's name, or the preset with paging switched on. */ -export function machineSpec(name: string | null | undefined): string | { preset: string; paging: { enabled: true } } { - const c = parseMachine(name); - return c.paging ? { preset: c.preset, paging: { enabled: true } } : c.preset; -} - -/** The machine in a few words, for captions. */ -export const machineLabel = (name: string | null | undefined): string => { const c = parseMachine(name); return c.paging ? `${c.preset}, with virtual memory` : c.preset; }; +export { MACHINE_PRESETS, parseMachine, machineName, machineSpec, machineLabel, type MachineChoice } from './machine.js'; +import { parseMachine, type MachineChoice } from './machine.js'; /* ---------- the board ---------- */ diff --git a/src/core/machine.ts b/src/core/machine.ts new file mode 100644 index 0000000..af347fe --- /dev/null +++ b/src/core/machine.ts @@ -0,0 +1,33 @@ +/** + * The machine models a run of x86-64 can use, as the state names them: a preset, with virtual memory on or off. + * DOM-free and part of core: the assembly service hands the model its machine from here, and the app's board + * (hardware.ts) reads the same names. + */ +import { PRESETS } from '../engines/asm86/machine.js'; + + +/** The machine models a run can use: `teaching` (small caches, so misses happen on small data), `realistic` (shaped like a desktop core), `ideal`. */ +export const MACHINE_PRESETS: readonly string[] = Object.keys(PRESETS); + +/** A machine as the state names it: a preset, with virtual memory (paging and a TLB) on or off. `teaching+paging` is the teaching machine with paging on. */ +export interface MachineChoice { preset: string; paging: boolean } + +const PAGING = '+paging'; + +export function parseMachine(name: string | null | undefined): MachineChoice { + const raw = String(name ?? '').trim(); + const paging = raw.endsWith(PAGING); + const preset = paging ? raw.slice(0, -PAGING.length) : raw; + return { preset: MACHINE_PRESETS.includes(preset) ? preset : 'teaching', paging }; +} + +export const machineName = (c: MachineChoice): string => `${c.preset}${c.paging ? PAGING : ''}`; + +/** What the engine's hardware model is given (`trace.spec.machine`): the preset's name, or the preset with paging switched on. */ +export function machineSpec(name: string | null | undefined): string | { preset: string; paging: { enabled: true } } { + const c = parseMachine(name); + return c.paging ? { preset: c.preset, paging: { enabled: true } } : c.preset; +} + +/** The machine in a few words, for captions. */ +export const machineLabel = (name: string | null | undefined): string => { const c = parseMachine(name); return c.paging ? `${c.preset}, with virtual memory` : c.preset; }; diff --git a/src/core/problems.ts b/src/core/problems.ts new file mode 100644 index 0000000..00b820a --- /dev/null +++ b/src/core/problems.ts @@ -0,0 +1,45 @@ +/** + * The problem catalog, built from the exercise registry (src/exercises/). Each exercise carries whichever layers + * the course wrote for it; its Mojo program and the compiler's listing come from the corpus (mojo/local/), and a + * speco spec is extracted from the pseudocode's own contracts when none is given. The app adds where each problem + * opens (its area) and the state it opens with (core/catalog.ts). + */ +import { EXERCISES, type Exercise } from '../exercises/index.js'; +import type { LangId } from './types.js'; +import type { WorkState } from './workspace.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 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, 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); +} + +/** What a workspace opens a problem with: its sources, the pane it starts in, its call, setup and machine. */ +export function workForProblem(p: Problem): WorkState { + return { sources: { ...p.sources }, lang: p.lang, call: p.call ?? null, setup: p.setup, machine: p.machine }; +} diff --git a/src/core/types.ts b/src/core/types.ts index b3f5e1d..1f166d1 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -144,3 +144,6 @@ export type ViewId = 'problems' | 'trace' | 'proof' | 'tests' | 'asm' | 'flow' | export const LANG_NAMES: Record = { lingo: 'lingo', matho: 'matho', pseudo: 'pseudo', speco: 'speco', mojo: 'Mojo', asm: 'x86-64', }; + +/** The app's three areas. Core names them only so an exercise can say where it opens (exercises/types.ts). */ +export type AreaId = 'bridge' | 'dev' | 'hw'; diff --git a/src/exercises/types.ts b/src/exercises/types.ts index a946c6f..625465c 100644 --- a/src/exercises/types.ts +++ b/src/exercises/types.ts @@ -5,7 +5,7 @@ * its pseudocode parses, its call runs, and its expected answer matches. */ import type { LangId, ViewId } from '../core/types.js'; -import type { AreaId } from '../core/areas.js'; +import type { AreaId } from '../core/types.js'; export interface Exercise { /** URL id: lowercase, hyphens. */ diff --git a/src/services/asm.ts b/src/services/asm.ts index 69996d1..1f4a5a1 100644 --- a/src/services/asm.ts +++ b/src/services/asm.ts @@ -6,7 +6,7 @@ */ import { assemble, run as runAsm, AsmError } from '../engines/asm86/interp.js'; import { hardwareFor } from '../engines/asm86/machine.js'; -import { MACHINE_PRESETS, machineName, machineSpec, parseMachine } from '../core/hardware.js'; +import { MACHINE_PRESETS, machineName, machineSpec, parseMachine } from '../core/machine.js'; import type { Diagnostic, LanguageService, Parsed, RunOptions, Trace } from '../core/types.js'; export type AsmProgram = any; @@ -86,7 +86,7 @@ export function parseAsm(source: string): Parsed { } } -/** The machine presets; a state's `machine` may add `+paging` to one (core/hardware.ts). */ +/** The machine presets; a state's `machine` may add `+paging` to one (core/machine.ts). */ export const MACHINES = MACHINE_PRESETS; export function runAsmProgram(parsed: Parsed, options: RunOptions): Trace {