Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 10 additions & 59 deletions src/components/PartDescription.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Box, Typography } from "@mui/material";
import { bitLabel } from "../common/bitLabel";
import { type Messages, useMessages } from "../i18n";
import type { CCComponent } from "../store/component";
import { definitionByComponentId } from "../store/intrinsics/definitions";
import { truthTableOf } from "../store/intrinsics/truthTable";
import { theme } from "../style";
import { TruthTableView } from "./TruthTableView";

type Summaries = Messages["parts"]["summaries"];

Expand All @@ -23,68 +22,20 @@ export function PartDescription({ component }: { component: CCComponent }) {
const summary = partSummary(messages, component);
const definition = definitionByComponentId.get(component.id);
const table = definition && truthTableOf(definition);
const columns = table
? [
...table.inputs.map((name) => ({ key: `in-${name}`, name })),
...table.outputs.map((name) => ({ key: `out-${name}`, name })),
]
: [];
const cellSx = (column: number) =>
({
px: 1,
py: 0.25,
textAlign: "center",
...(table &&
table.inputs.length > 0 &&
column === table.inputs.length && {
borderLeft: 1,
borderColor: "divider",
}),
}) as const;

return (
<div>
<Typography variant="subtitle2">{component.name}</Typography>
<Typography
variant="subtitle1"
component="h4"
sx={{ fontWeight: "bold" }}
>
{component.name}
</Typography>
{summary && <Typography variant="body2">{summary}</Typography>}
{table && (
<Box
component="table"
sx={{ mt: 0.5, borderCollapse: "collapse", fontSize: "0.875rem" }}
>
<thead>
<tr>
{columns.map(({ key, name }, i) => (
<Box
key={key}
component="th"
sx={{ ...cellSx(i), borderBottom: 1, borderColor: "divider" }}
>
{name}
</Box>
))}
</tr>
</thead>
<tbody>
{table.rows.map((row) => (
<tr key={row.inputs.join()}>
{[...row.inputs, ...row.outputs].map((value, i) => (
<Box
key={columns[i]?.key}
component="td"
sx={{
...cellSx(i),
...(value && {
color: theme.colors.primary,
fontWeight: "bold",
}),
}}
>
{bitLabel(value)}
</Box>
))}
</tr>
))}
</tbody>
<Box sx={{ mt: 0.5 }}>
<TruthTableView table={table} />
</Box>
)}
</div>
Expand Down
64 changes: 64 additions & 0 deletions src/components/TruthTableView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Box } from "@mui/material";
import { bitLabel } from "../common/bitLabel";
import type { TruthTable } from "../store/intrinsics/truthTable";
import { theme } from "../style";

export function TruthTableView({ table }: { table: TruthTable }) {
const columns = [
...table.inputs.map((name) => ({ key: `in-${name}`, name })),
...table.outputs.map((name) => ({ key: `out-${name}`, name })),
];
const cellSx = (column: number) =>
({
px: 1,
py: 0.25,
textAlign: "center",
...(table.inputs.length > 0 &&
column === table.inputs.length && {
borderLeft: 1,
borderColor: "divider",
}),
}) as const;

return (
<Box
component="table"
sx={{ borderCollapse: "collapse", fontSize: "0.875rem" }}
>
<thead>
<tr>
{columns.map(({ key, name }, i) => (
<Box
key={key}
component="th"
sx={{ ...cellSx(i), borderBottom: 1, borderColor: "divider" }}
>
{name}
</Box>
))}
</tr>
</thead>
<tbody>
{table.rows.map((row) => (
<tr key={row.inputs.join()}>
{[...row.inputs, ...row.outputs].map((value, i) => (
<Box
key={columns[i]?.key}
component="td"
sx={{
...cellSx(i),
...(value && {
color: theme.colors.primary,
fontWeight: "bold",
}),
}}
>
{bitLabel(value)}
</Box>
))}
</tr>
))}
</tbody>
</Box>
);
}
3 changes: 2 additions & 1 deletion src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const en = {
FALSE: "Always OFF",
},
about: (name: string) => `About ${name}`,
ofStage: "Parts you can use in this stage",
ofStage: "Available parts",
},
header: {
language: "Language",
Expand Down Expand Up @@ -106,6 +106,7 @@ export const en = {
startOverFailed: "Could not start the tutorial over.",
stages: "Stages",
parts: "Parts",
goalHeading: "Goal",
check: "Check",
passed: "Cleared! The circuit works for every combination of the inputs.",
next: "Next stage",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/ja.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const ja: Messages = {
FALSE: "いつも OFF",
},
about: (name) => `${name} について`,
ofStage: "この課題で使える部品",
ofStage: "使える部品",
},
header: {
language: "言語",
Expand Down Expand Up @@ -101,6 +101,7 @@ export const ja: Messages = {
startOverFailed: "チュートリアルをやり直せませんでした。",
stages: "課題の一覧",
parts: "部品",
goalHeading: "目標",
check: "確かめる",
passed: "クリア!どの入力の組み合わせでも正しく動きます。",
next: "次の課題へ",
Expand Down
39 changes: 32 additions & 7 deletions src/pages/tutorial/stage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Typography,
} from "@mui/material";
import nullthrows from "nullthrows";
import { useEffect, useState } from "react";
import { type ReactNode, useEffect, useState } from "react";
import {
Navigate,
Link as RouterLink,
Expand All @@ -19,13 +19,18 @@ import {
import { valueLabel } from "../../common/bitLabel";
import { ConfirmDialog } from "../../components/ConfirmDialog";
import { PartDescription } from "../../components/PartDescription";
import { TruthTableView } from "../../components/TruthTableView";
import { type Messages, useMessages } from "../../i18n";
import type CCStore from "../../store";
import { definitionByComponentId } from "../../store/intrinsics/definitions";
import { SimulationNodeError } from "../../store/netlist";
import { StoreScope } from "../../store/react";
import { theme } from "../../style";
import { type CheckResult, checkCombinational } from "../../tutorial/check";
import {
type CheckResult,
checkCombinational,
truthTableOfCheck,
} from "../../tutorial/check";
import { markStageCleared } from "../../tutorial/progress";
import {
getTutorialStageStore,
Expand Down Expand Up @@ -72,13 +77,27 @@ function describeFailure(
}
}

function SectionHeading({ children }: { children: ReactNode }) {
return (
<Typography
variant="h3"
typography="h6"
sx={{ fontWeight: "bold" }}
gutterBottom
>
{children}
</Typography>
);
}

function StageView({ stage }: { stage: TutorialStage }) {
const messages = useMessages();
const texts = messages.tutorial;
const navigate = useNavigate();
const stageIndex = tutorialStages.indexOf(stage);
const nextStage = tutorialStages[stageIndex + 1];
const componentName = texts.stageTexts[stage.id].title;
const goalTable = truthTableOfCheck(stage.check);

const [store, setStore] = useState(() =>
getTutorialStageStore(stage, componentName),
Expand Down Expand Up @@ -150,11 +169,19 @@ function StageView({ stage }: { stage: TutorialStage }) {
<Typography color="textSecondary" variant="body2">
{texts.stageNumber(stageIndex + 1)}
</Typography>
<Typography variant="h2" typography="h5">
<Typography variant="h2" typography="h5" sx={{ fontWeight: "bold" }}>
{texts.stageTexts[stage.id].title}
</Typography>
</div>
<Typography>{texts.stageTexts[stage.id].goal}</Typography>
<div>
<SectionHeading>{texts.goalHeading}</SectionHeading>
<Typography>{texts.stageTexts[stage.id].goal}</Typography>
{goalTable && (
<Box sx={{ mt: 1 }}>
<TruthTableView table={goalTable} />
</Box>
)}
</div>
<Button
variant="contained"
onClick={check}
Expand All @@ -180,9 +207,7 @@ function StageView({ stage }: { stage: TutorialStage }) {
</Alert>
)}
<div>
<Typography color="textSecondary" variant="body2" gutterBottom>
{messages.parts.ofStage}
</Typography>
<SectionHeading>{messages.parts.ofStage}</SectionHeading>
<Stack spacing={1.5}>
{stage.parts.map((partId) => (
<PartDescription
Expand Down
48 changes: 47 additions & 1 deletion src/tutorial/check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
registerTestNode,
testNodePin,
} from "../store/testUtils";
import { checkCombinational, inputCombinations } from "./check";
import {
type CombinationalCheck,
checkCombinational,
inputCombinations,
truthTableOfCheck,
} from "./check";
import {
setUpTutorialStage,
type TutorialStage,
Expand Down Expand Up @@ -59,6 +64,47 @@ describe("inputCombinations", () => {
});
});

describe("truthTableOfCheck", () => {
it("lists what the stage asks for, in the order the check tries the inputs", () => {
expect(truthTableOfCheck(xorStage.check)).toEqual({
inputs: ["A", "B"],
outputs: ["Out"],
rows: [
{ inputs: [false, false], outputs: [false] },
{ inputs: [false, true], outputs: [true] },
{ inputs: [true, false], outputs: [true] },
{ inputs: [true, true], outputs: [false] },
],
});
});

it("gives no table when a pin carries more than one bit", () => {
const wideInput: CombinationalCheck = {
inputs: [{ name: "A", bitWidth: 2 }],
outputs: ["Out"],
expected: () => ({ Out: [false] }),
};
const wideOutput: CombinationalCheck = {
inputs: [{ name: "A", bitWidth: 1 }],
outputs: ["Out"],
expected: () => ({ Out: [false, false] }),
};

expect(truthTableOfCheck(wideInput)).toBeNull();
expect(truthTableOfCheck(wideOutput)).toBeNull();
});

it("refuses a check that gives no value for an output", () => {
const check: CombinationalCheck = {
inputs: [{ name: "A", bitWidth: 1 }],
outputs: ["Out"],
expected: () => ({}),
};

expect(() => truthTableOfCheck(check)).toThrow("Out");
});
});

describe("checkCombinational", () => {
it("passes a circuit that outputs what the stage asks for", () => {
const store = wireGate(andStage, and);
Expand Down
34 changes: 30 additions & 4 deletions src/tutorial/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import nullthrows from "nullthrows";
import type CCStore from "../store";
import type { CCComponentId } from "../store/component";
import type { CCComponentPinId } from "../store/componentPin";
import type { TruthTable } from "../store/intrinsics/truthTable";
import type { CCNodePinId } from "../store/nodePin";
import simulateComponent, {
type SimulationFrame,
Expand Down Expand Up @@ -48,6 +49,34 @@ export function* inputCombinations(
}
}

function expectedValueOf(expected: PinValues, name: string) {
return nullthrows(expected[name], `The check gives no value for ${name}`);
}

/** null when a pin carries more than one bit, which a table of ON and OFF cannot show */
export function truthTableOfCheck(
check: CombinationalCheck,
): TruthTable | null {
if (check.inputs.some(({ bitWidth }) => bitWidth !== 1)) return null;
const rows: TruthTable["rows"] = [];
for (const inputs of inputCombinations(check.inputs)) {
const expected = check.expected(inputs);
const outputs = check.outputs.map((name) =>
expectedValueOf(expected, name),
);
if (outputs.some((value) => value.length !== 1)) return null;
rows.push({
inputs: check.inputs.map(({ name }) => nullthrows(inputs[name]?.[0])),
outputs: outputs.map((value) => nullthrows(value[0])),
});
}
return {
inputs: check.inputs.map(({ name }) => name),
outputs: [...check.outputs],
rows,
};
}

function isSameValue(a: SimulationValue, b: SimulationValue | undefined) {
return (
b !== undefined &&
Expand Down Expand Up @@ -93,10 +122,7 @@ export function checkCombinational(
}
const expected = check.expected(inputs);
for (const name of check.outputs) {
const expectedValue = nullthrows(
expected[name],
`The check gives no value for ${name}`,
);
const expectedValue = expectedValueOf(expected, name);
const actual = frame.getNodePinValue(
nullthrows(outputNodePinIds.get(name)),
);
Expand Down
Loading