Skip to content

Commit 4556d3f

Browse files
xyliew25JoelChanZhiYangRichDom2185
authored
Implement and Integrate java-slang CSEC Visualizer (#2926)
* Update ControlBarChapterSelect with Java * Implement Java CSEC Visualizer * Integrate CSEC Visualizer * Linting errors for java-slang-csec * Fix frontend test: rename Object to Obj to avoid potential name clash? * Update snapshot * Update imports from java-slang * Fix lint * Update snapshot * Fix linting * Fix typo Co-authored-by: Richard Dominick <[email protected]> * Add TODO for err source node location --------- Co-authored-by: joel chan <[email protected]> Co-authored-by: Richard Dominick <[email protected]>
1 parent 612f68b commit 4556d3f

File tree

23 files changed

+1868
-132
lines changed

23 files changed

+1868
-132
lines changed

src/commons/application/ApplicationTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export const javaLanguages: SALanguage[] = [
219219
variant: Variant.DEFAULT,
220220
displayName: 'Java',
221221
mainLanguage: SupportedLanguage.JAVA,
222-
supports: {}
222+
supports: { cseMachine: true }
223223
}
224224
];
225225
export const cLanguages: SALanguage[] = [

src/commons/controlBar/ControlBarChapterSelect.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
fullJSLanguage,
1010
fullTSLanguage,
1111
htmlLanguage,
12+
javaLanguages,
1213
pyLanguages,
1314
SALanguage,
1415
schemeLanguages,
@@ -87,7 +88,8 @@ export const ControlBarChapterSelect: React.FC<ControlBarChapterSelectProps> = (
8788
// See https://github.com/source-academy/frontend/pull/2460#issuecomment-1528759912
8889
...(Constants.playgroundOnly ? [fullJSLanguage, fullTSLanguage, htmlLanguage] : []),
8990
...schemeLanguages,
90-
...pyLanguages
91+
...pyLanguages,
92+
...javaLanguages
9193
];
9294

9395
return (

src/commons/sagas/PlaygroundSaga.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import qs from 'query-string';
55
import { SagaIterator } from 'redux-saga';
66
import { call, delay, put, race, select } from 'redux-saga/effects';
77
import CseMachine from 'src/features/cseMachine/CseMachine';
8+
import { CseMachine as JavaCseMachine } from 'src/features/cseMachine/java/CseMachine';
89

910
import {
1011
changeQueryString,
@@ -100,12 +101,17 @@ export default function* PlaygroundSaga(): SagaIterator {
100101
if (newId !== SideContentType.cseMachine) {
101102
yield put(toggleUsingCse(false, workspaceLocation));
102103
yield call([CseMachine, CseMachine.clearCse]);
104+
yield call([JavaCseMachine, JavaCseMachine.clearCse]);
103105
yield put(updateCurrentStep(-1, workspaceLocation));
104106
yield put(updateStepsTotal(0, workspaceLocation));
105107
yield put(toggleUpdateCse(true, workspaceLocation));
106108
yield put(setEditorHighlightedLines(workspaceLocation, 0, []));
107109
}
108110

111+
if (playgroundSourceChapter === Chapter.FULL_JAVA && newId === SideContentType.cseMachine) {
112+
yield put(toggleUsingCse(true, workspaceLocation));
113+
}
114+
109115
if (
110116
isSourceLanguage(playgroundSourceChapter) &&
111117
(newId === SideContentType.substVisualizer || newId === SideContentType.cseMachine)

src/commons/sagas/WorkspaceSaga/helpers/evalCode.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ export function* evalCode(
250250
let lastDebuggerResult = yield select(
251251
(state: OverallState) => state.workspaces[workspaceLocation].lastDebuggerResult
252252
);
253+
const isUsingCse = yield select((state: OverallState) => state.workspaces['playground'].usingCse);
253254

254255
// Handles `console.log` statements in fullJS
255256
const detachConsole: () => void =
@@ -266,7 +267,7 @@ export function* evalCode(
266267
: isC
267268
? call(cCompileAndRun, entrypointCode, context)
268269
: isJava
269-
? call(javaRun, entrypointCode, context)
270+
? call(javaRun, entrypointCode, context, currentStep, isUsingCse)
270271
: call(
271272
runFilesInContext,
272273
isFolderModeEnabled

src/commons/sagas/WorkspaceSaga/helpers/updateInspector.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
1+
import { Chapter } from 'js-slang/dist/types';
12
import { SagaIterator } from 'redux-saga';
23
import { put, select } from 'redux-saga/effects';
34

45
import { OverallState } from '../../../application/ApplicationTypes';
56
import { actions } from '../../../utils/ActionsHelper';
7+
import { visualizeJavaCseMachine } from '../../../utils/JavaHelper';
68
import { visualizeCseMachine } from '../../../utils/JsSlangHelper';
79
import { WorkspaceLocation } from '../../../workspace/WorkspaceTypes';
810

911
export function* updateInspector(workspaceLocation: WorkspaceLocation): SagaIterator {
1012
try {
11-
const lastDebuggerResult = yield select(
12-
(state: OverallState) => state.workspaces[workspaceLocation].lastDebuggerResult
13-
);
14-
const row = lastDebuggerResult.context.runtime.nodes[0].loc.start.line - 1;
15-
// TODO: Hardcoded to make use of the first editor tab. Rewrite after editor tabs are added.
16-
yield put(actions.setEditorHighlightedLines(workspaceLocation, 0, []));
17-
// We highlight only one row to show the current line
18-
// If we highlight from start to end, the whole program block will be highlighted at the start
19-
// since the first node is the program node
20-
yield put(actions.setEditorHighlightedLines(workspaceLocation, 0, [[row, row]]));
21-
visualizeCseMachine(lastDebuggerResult);
13+
const [lastDebuggerResult, chapter] = yield select((state: OverallState) => [
14+
state.workspaces[workspaceLocation].lastDebuggerResult,
15+
state.workspaces[workspaceLocation].context.chapter
16+
]);
17+
if (chapter === Chapter.FULL_JAVA) {
18+
const controlItem = lastDebuggerResult.context.control.peek();
19+
let start = -1;
20+
let end = -1;
21+
if (controlItem?.srcNode?.location) {
22+
const node = controlItem.srcNode;
23+
start = node.location.startLine - 1;
24+
end = node.location.endLine ? node.location.endLine - 1 : start;
25+
}
26+
yield put(actions.setEditorHighlightedLines(workspaceLocation, 0, []));
27+
yield put(actions.setEditorHighlightedLines(workspaceLocation, 0, [[start, end]]));
28+
visualizeJavaCseMachine(lastDebuggerResult);
29+
} else {
30+
const row = lastDebuggerResult.context.runtime.nodes[0].loc.start.line - 1;
31+
// TODO: Hardcoded to make use of the first editor tab. Rewrite after editor tabs are added.
32+
yield put(actions.setEditorHighlightedLines(workspaceLocation, 0, []));
33+
// We highlight only one row to show the current line
34+
// If we highlight from start to end, the whole program block will be highlighted at the start
35+
// since the first node is the program node
36+
yield put(actions.setEditorHighlightedLines(workspaceLocation, 0, [[row, row]]));
37+
visualizeCseMachine(lastDebuggerResult);
38+
}
2239
} catch (e) {
2340
// TODO: Hardcoded to make use of the first editor tab. Rewrite after editor tabs are added.
2441
yield put(actions.setEditorHighlightedLines(workspaceLocation, 0, []));

src/commons/sideContent/__tests__/__snapshots__/SideContentCseMachine.tsx.snap

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -352,17 +352,19 @@ exports[`CSE Machine component renders correctly 1`] = `
352352
data-testid="cse-machine-default-text"
353353
id="cse-machine-default-text"
354354
>
355-
The CSE machine generates control, stash and environment model diagrams following a notation introduced in
356-
357-
<a
358-
href="https://sourceacademy.org/sicpjs/3.2"
359-
rel="noopener noreferrer"
360-
target="_blank"
361-
>
362-
<i>
363-
Structure and Interpretation of Computer Programs, JavaScript Edition, Chapter 3, Section 2
364-
</i>
365-
</a>
355+
<span>
356+
The CSE machine generates control, stash and environment model diagrams following a notation introduced in
357+
358+
<a
359+
href="https://sourceacademy.org/sicpjs/3.2"
360+
rel="noopener noreferrer"
361+
target="_blank"
362+
>
363+
<i>
364+
Structure and Interpretation of Computer Programs, JavaScript Edition, Chapter 3, Section 2
365+
</i>
366+
</a>
367+
</span>
366368
.
367369
<br />
368370
<br />

0 commit comments

Comments
 (0)