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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
This commit is contained in:
alexmandClaude committed 2026-10-05 05:16:27 +00:00
1 parent f400391a89
commit d4f2d92ece
5 files changed
+84 -3

No files matched your search

+33
View File
@@ -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; };
+45
View File
@@ -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 };
}
+3
View File
@@ -144,3 +144,6 @@ export type ViewId = 'problems' | 'trace' | 'proof' | 'tests' | 'asm' | 'flow' |
export const LANG_NAMES: Record<LangId, string> = {
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';
+1 -1
View File
@@ -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. */
+2 -2
View File
@@ -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<AsmProgram> {
}
}
/** 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<AsmProgram>, options: RunOptions): Trace<AsmState, AsmStep> {