From 9f82165fd75da8c79c526c8cc44b99264733f00e Mon Sep 17 00:00:00 2001 From: NhatMinh0208 Date: Sun, 18 Feb 2024 17:15:28 +0800 Subject: [PATCH] CSE Machine: Fix environment reset instruction insertion (#1542) * fix environment reset instruction insertion * Update nodejs.yml * remove unneccesary semiclolon in .github/workflows/nodejs.yml Co-authored-by: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> --------- Co-authored-by: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> --- .../__tests__/__snapshots__/cse-machine.ts.snap | 13 +++++++++++++ src/cse-machine/__tests__/cse-machine.ts | 9 +++++++++ src/cse-machine/interpreter.ts | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/cse-machine/__tests__/__snapshots__/cse-machine.ts.snap b/src/cse-machine/__tests__/__snapshots__/cse-machine.ts.snap index 9fd807f01..4a58dff15 100644 --- a/src/cse-machine/__tests__/__snapshots__/cse-machine.ts.snap +++ b/src/cse-machine/__tests__/__snapshots__/cse-machine.ts.snap @@ -97,6 +97,19 @@ fact(5);", } `; +exports[`Environment reset is inserted when only instructions are in control stack: expectResult 1`] = ` +Object { + "alertResult": Array [], + "code": "const a = (v => v)(0);", + "displayResult": Array [], + "numErrors": 0, + "parsedErrors": "", + "result": undefined, + "resultStatus": "finished", + "visualiseListResult": Array [], +} +`; + exports[`Imports are properly handled: expectResult 1`] = ` Object { "alertResult": Array [], diff --git a/src/cse-machine/__tests__/cse-machine.ts b/src/cse-machine/__tests__/cse-machine.ts index 0f55abbb0..4cab099c4 100644 --- a/src/cse-machine/__tests__/cse-machine.ts +++ b/src/cse-machine/__tests__/cse-machine.ts @@ -460,3 +460,12 @@ test('Breaks, continues and returns are detected properly inside loops', () => { optionEC3 ).toMatchInlineSnapshot(`3`) }) + +test('Environment reset is inserted when only instructions are in control stack', () => { + return expectResult( + stripIndent` + const a = (v => v)(0); + `, + optionEC3 + ).toMatchInlineSnapshot(`undefined`) +}) diff --git a/src/cse-machine/interpreter.ts b/src/cse-machine/interpreter.ts index 50f6480b8..56fce22f2 100644 --- a/src/cse-machine/interpreter.ts +++ b/src/cse-machine/interpreter.ts @@ -827,7 +827,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = { if ( next && !(isInstr(next) && next.instrType === InstrType.ENVIRONMENT) && - control.some(isNode) + !control.isEmpty() ) { control.push(instr.envInstr(currentEnvironment(context), command.srcNode)) }