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:
8 files changed
+94
-60
No files matched your search
+2
-1
@@ -8,7 +8,8 @@ import type { LangId } from './types.js';
|
|||||||
import type { DockTab, PlayState } from './state.js';
|
import type { DockTab, PlayState } from './state.js';
|
||||||
import { LINKED } from '../sync/hub.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. */
|
/** The tabs beside the editor: the views that read a run. */
|
||||||
export type InspectId = 'trace' | 'table' | 'flow' | 'data' | 'life' | 'proof' | 'tests';
|
export type InspectId = 'trace' | 'table' | 'flow' | 'data' | 'life' | 'proof' | 'tests';
|
||||||
|
|||||||
+5
-29
@@ -1,41 +1,17 @@
|
|||||||
/**
|
/**
|
||||||
* The problem catalog, built from the exercise registry (src/exercises/).
|
* The app's problem catalog: core's problems (core/problems.ts), each with the
|
||||||
* Each exercise carries whichever layers the course wrote for it; a speco
|
* area it opens in, and the saved state a problem opens with.
|
||||||
* spec is extracted from the pseudocode's own contracts when none is given.
|
|
||||||
*/
|
*/
|
||||||
import { EXERCISES, type Exercise } from '../exercises/index.js';
|
import { PROBLEMS as CORE_PROBLEMS, type Problem as CoreProblem } from './problems.js';
|
||||||
import type { LangId } from './types.js';
|
|
||||||
import { defaultAreaFor, type AreaId } from './areas.js';
|
import { defaultAreaFor, type AreaId } from './areas.js';
|
||||||
import { emptyState, type PlayState } from './state.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 {
|
export interface Problem extends CoreProblem {
|
||||||
lang: LangId;
|
|
||||||
/** The area it opens in from the problem list. */
|
/** The area it opens in from the problem list. */
|
||||||
area: AreaId;
|
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 {
|
export const PROBLEMS: Problem[] = CORE_PROBLEMS.map((p) => ({ ...p, area: defaultAreaFor({ id: p.id, sources: p.sources, area: p.area, mojoProgram: p.mojoProgram }) }));
|
||||||
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 {
|
export function problemById(id: string): Problem | undefined {
|
||||||
return PROBLEMS.find((p) => p.id === id);
|
return PROBLEMS.find((p) => p.id === id);
|
||||||
|
|||||||
+3
-27
@@ -5,35 +5,11 @@
|
|||||||
* here runs under `node --test`, so the board's data steps are tested without a browser.
|
* here runs under `node --test`, so the board's data steps are tested without a browser.
|
||||||
*/
|
*/
|
||||||
import { REGS } from '../engines/asm86/interp.js';
|
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 { MACHINE_PRESETS, parseMachine, machineName, machineSpec, machineLabel, type MachineChoice } from './machine.js';
|
||||||
export const MACHINE_PRESETS: readonly string[] = Object.keys(PRESETS);
|
import { parseMachine, type MachineChoice } from './machine.js';
|
||||||
|
|
||||||
/** 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; };
|
|
||||||
|
|
||||||
/* ---------- the board ---------- */
|
/* ---------- the board ---------- */
|
||||||
|
|
||||||
|
|||||||
@@ -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; };
|
||||||
@@ -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 };
|
||||||
|
}
|
||||||
@@ -144,3 +144,6 @@ export type ViewId = 'problems' | 'trace' | 'proof' | 'tests' | 'asm' | 'flow' |
|
|||||||
export const LANG_NAMES: Record<LangId, string> = {
|
export const LANG_NAMES: Record<LangId, string> = {
|
||||||
lingo: 'lingo', matho: 'matho', pseudo: 'pseudo', speco: 'speco', mojo: 'Mojo', asm: 'x86-64',
|
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';
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
* its pseudocode parses, its call runs, and its expected answer matches.
|
* its pseudocode parses, its call runs, and its expected answer matches.
|
||||||
*/
|
*/
|
||||||
import type { LangId, ViewId } from '../core/types.js';
|
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 {
|
export interface Exercise {
|
||||||
/** URL id: lowercase, hyphens. */
|
/** URL id: lowercase, hyphens. */
|
||||||
|
|||||||
+2
-2
@@ -6,7 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
import { assemble, run as runAsm, AsmError } from '../engines/asm86/interp.js';
|
import { assemble, run as runAsm, AsmError } from '../engines/asm86/interp.js';
|
||||||
import { hardwareFor } from '../engines/asm86/machine.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';
|
import type { Diagnostic, LanguageService, Parsed, RunOptions, Trace } from '../core/types.js';
|
||||||
|
|
||||||
export type AsmProgram = any;
|
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 const MACHINES = MACHINE_PRESETS;
|
||||||
|
|
||||||
export function runAsmProgram(parsed: Parsed<AsmProgram>, options: RunOptions): Trace<AsmState, AsmStep> {
|
export function runAsmProgram(parsed: Parsed<AsmProgram>, options: RunOptions): Trace<AsmState, AsmStep> {
|
||||||
|
|||||||
Reference in new issue
Block a user