From 21a14b3466eec2f8a21209dcbb9b107f0b221638 Mon Sep 17 00:00:00 2001 From: Dick Ong Date: Mon, 23 Oct 2023 10:39:07 +0800 Subject: [PATCH] Fix nullary functions on the CSE machine (#1494) * Push env instr even if function has no arguments * Add tests for nullary functions * Update test snapshots --------- Co-authored-by: Martin Henz --- .../__snapshots__/ec-evaluator.ts.snap | 38 +++++++++++++++++++ src/ec-evaluator/__tests__/ec-evaluator.ts | 30 +++++++++++++++ src/ec-evaluator/interpreter.ts | 3 -- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/ec-evaluator/__tests__/__snapshots__/ec-evaluator.ts.snap b/src/ec-evaluator/__tests__/__snapshots__/ec-evaluator.ts.snap index ee0c51fad..de50c488d 100644 --- a/src/ec-evaluator/__tests__/__snapshots__/ec-evaluator.ts.snap +++ b/src/ec-evaluator/__tests__/__snapshots__/ec-evaluator.ts.snap @@ -45,6 +45,44 @@ foo();", } `; +exports[`Nullary functions properly restore environment 1: expectResult 1`] = ` +Object { + "alertResult": Array [], + "code": "function f() { + function g(t) { + return 0; + } + return g; +} +const h = f(); +h(100);", + "displayResult": Array [], + "numErrors": 0, + "parsedErrors": "", + "result": 0, + "resultStatus": "finished", + "visualiseListResult": Array [], +} +`; + +exports[`Nullary functions properly restore environment 2: expectResult 1`] = ` +Object { + "alertResult": Array [], + "code": "function f() { + const a = 1; + return a; +} +const a = f(); +a;", + "displayResult": Array [], + "numErrors": 0, + "parsedErrors": "", + "result": 1, + "resultStatus": "finished", + "visualiseListResult": Array [], +} +`; + exports[`Simple tail call returns work: expectResult 1`] = ` Object { "alertResult": Array [], diff --git a/src/ec-evaluator/__tests__/ec-evaluator.ts b/src/ec-evaluator/__tests__/ec-evaluator.ts index 66ab16ea0..961511aff 100644 --- a/src/ec-evaluator/__tests__/ec-evaluator.ts +++ b/src/ec-evaluator/__tests__/ec-evaluator.ts @@ -374,3 +374,33 @@ test('Conditional statements are value producing always', () => { optionEC3 ).toMatchInlineSnapshot(`120`) }) + +test('Nullary functions properly restore environment 1', () => { + return expectResult( + stripIndent` + function f() { + function g(t) { + return 0; + } + return g; + } + const h = f(); + h(100); + `, + optionEC3 + ).toMatchInlineSnapshot(`0`) +}) + +test('Nullary functions properly restore environment 2', () => { + return expectResult( + stripIndent` + function f() { + const a = 1; + return a; + } + const a = f(); + a; + `, + optionEC3 + ).toMatchInlineSnapshot(`1`) +}) diff --git a/src/ec-evaluator/interpreter.ts b/src/ec-evaluator/interpreter.ts index 6e450797c..739654e0e 100644 --- a/src/ec-evaluator/interpreter.ts +++ b/src/ec-evaluator/interpreter.ts @@ -145,7 +145,6 @@ export function evaluate(program: es.Program, context: Context, options: IOption options.isPrelude ) } catch (error) { - // console.error('ecerror:', error) return new ECError(error) } finally { context.runtime.isRunning = false @@ -200,7 +199,6 @@ function evaluateImports( } }) } catch (error) { - // console.log(error) handleRuntimeError(context, error) } } @@ -816,7 +814,6 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = { if ( next && !(isInstr(next) && next.instrType === InstrType.ENVIRONMENT) && - args.length !== 0 && agenda.some(isNode) ) { agenda.push(instr.envInstr(currentEnvironment(context), command.srcNode))