Skip to content

Commit cf02879

Browse files
committed
update representation of control
1 parent 732c4ac commit cf02879

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

src/features/cseMachine/CseMachineUtils.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ import {
4545
Primitive,
4646
ReferenceType
4747
} from './CseMachineTypes';
48-
import { isCustomPrimitive } from './utils/altLangs';
48+
import {
49+
getAlternateControlItemComponent,
50+
isCustomPrimitive,
51+
needsNewRepresentation
52+
} from './utils/altLangs';
4953

5054
// TODO: can make use of lodash
5155
/** Returns `true` if `x` is an object */
@@ -531,6 +535,19 @@ export function getControlItemComponent(
531535
? index === Math.min(Layout.control.size() - 1, 9)
532536
: index === Layout.control.size() - 1;
533537
if (!isInstr(controlItem)) {
538+
// there's no reason to provide an alternate representation
539+
// for a instruction.
540+
if (needsNewRepresentation(chapter)) {
541+
return getAlternateControlItemComponent(
542+
controlItem,
543+
stackHeight,
544+
highlightOnHover,
545+
unhighlightOnHover,
546+
topItem,
547+
chapter
548+
);
549+
}
550+
534551
if (isSchemeLanguage(chapter)) {
535552
// use the js-slang decoder on the control item
536553
controlItem = estreeDecode(controlItem as any);
@@ -797,12 +814,8 @@ export function getStashItemComponent(
797814
stashItem: StashValue,
798815
stackHeight: number,
799816
index: number,
800-
chapter: Chapter
817+
_chapter: Chapter
801818
): StashItemComponent {
802-
if (isSchemeLanguage(chapter)) {
803-
// we can use schemeVisualise to get the representation of the value
804-
// TODO
805-
}
806819
if (isClosure(stashItem) || isGlobalFn(stashItem) || isDataArray(stashItem)) {
807820
for (const level of Layout.levels) {
808821
for (const frame of level.frames) {
Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,53 @@
11
// alternate representations of data types
22

3+
import { ControlItem } from 'js-slang/dist/cse-machine/types';
4+
import { Chapter, Node } from 'js-slang/dist/types';
5+
import { isSchemeLanguage } from 'src/commons/application/ApplicationTypes';
6+
7+
import { ControlItemComponent } from '../components/ControlItemComponent';
38
import { Data } from '../CseMachineTypes';
4-
import { isSchemeNumber } from './scheme';
9+
import { convertNodeToScheme, isSchemeNumber } from './scheme';
510

611
// used to define custom primitives from alternate languages.
712
// MAKE SURE the custom primitive has a toString() method!
813
export function isCustomPrimitive(data: Data): boolean {
914
return isSchemeNumber(data);
1015
}
16+
17+
// tells the CSE machine whether a new representation is needed.
18+
// expand when necessary.
19+
export function needsNewRepresentation(chapter: Chapter): boolean {
20+
return isSchemeLanguage(chapter);
21+
}
22+
23+
export function getAlternateControlItemComponent(
24+
controlItem: ControlItem,
25+
stackHeight: number,
26+
highlightOnHover: () => void,
27+
unhighlightOnHover: () => void,
28+
topItem: boolean,
29+
chapter: Chapter
30+
) {
31+
// keep in mind that the controlItem is a node.
32+
// there's no reason to provide an alternate representation
33+
// for a instruction.
34+
const node = controlItem as Node;
35+
switch (chapter) {
36+
case Chapter.SCHEME_1:
37+
case Chapter.SCHEME_2:
38+
case Chapter.SCHEME_3:
39+
case Chapter.SCHEME_4:
40+
case Chapter.FULL_SCHEME:
41+
const text = convertNodeToScheme(node);
42+
return new ControlItemComponent(
43+
text,
44+
text,
45+
stackHeight,
46+
highlightOnHover,
47+
unhighlightOnHover,
48+
topItem
49+
);
50+
default:
51+
throw new Error('Unknown chapter');
52+
}
53+
}

src/features/cseMachine/utils/scheme.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { estreeDecode } from 'js-slang/dist/alt-langs/scheme/scm-slang/src/utils/encoder-visitor';
2+
import { unparse } from 'js-slang/dist/alt-langs/scheme/scm-slang/src/utils/reverse_parser';
3+
import { Node } from 'js-slang/dist/types';
4+
15
import { Data } from '../CseMachineTypes';
26

37
/** Returns `true` if `data` is a scheme number
@@ -6,3 +10,7 @@ import { Data } from '../CseMachineTypes';
610
export function isSchemeNumber(data: Data): boolean {
711
return (data as any)?.numberType !== undefined;
812
}
13+
14+
export function convertNodeToScheme(node: Node): string {
15+
return unparse(estreeDecode(node as any));
16+
}

0 commit comments

Comments
 (0)