Skip to content

Commit 21a14b3

Browse files
dickongwdmartin-henz
andauthoredOct 23, 2023
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 <[email protected]>
1 parent 6aeee1d commit 21a14b3

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed
 

‎src/ec-evaluator/__tests__/__snapshots__/ec-evaluator.ts.snap

+38
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,44 @@ foo();",
4545
}
4646
`;
4747

48+
exports[`Nullary functions properly restore environment 1: expectResult 1`] = `
49+
Object {
50+
"alertResult": Array [],
51+
"code": "function f() {
52+
function g(t) {
53+
return 0;
54+
}
55+
return g;
56+
}
57+
const h = f();
58+
h(100);",
59+
"displayResult": Array [],
60+
"numErrors": 0,
61+
"parsedErrors": "",
62+
"result": 0,
63+
"resultStatus": "finished",
64+
"visualiseListResult": Array [],
65+
}
66+
`;
67+
68+
exports[`Nullary functions properly restore environment 2: expectResult 1`] = `
69+
Object {
70+
"alertResult": Array [],
71+
"code": "function f() {
72+
const a = 1;
73+
return a;
74+
}
75+
const a = f();
76+
a;",
77+
"displayResult": Array [],
78+
"numErrors": 0,
79+
"parsedErrors": "",
80+
"result": 1,
81+
"resultStatus": "finished",
82+
"visualiseListResult": Array [],
83+
}
84+
`;
85+
4886
exports[`Simple tail call returns work: expectResult 1`] = `
4987
Object {
5088
"alertResult": Array [],

‎src/ec-evaluator/__tests__/ec-evaluator.ts

+30
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,33 @@ test('Conditional statements are value producing always', () => {
374374
optionEC3
375375
).toMatchInlineSnapshot(`120`)
376376
})
377+
378+
test('Nullary functions properly restore environment 1', () => {
379+
return expectResult(
380+
stripIndent`
381+
function f() {
382+
function g(t) {
383+
return 0;
384+
}
385+
return g;
386+
}
387+
const h = f();
388+
h(100);
389+
`,
390+
optionEC3
391+
).toMatchInlineSnapshot(`0`)
392+
})
393+
394+
test('Nullary functions properly restore environment 2', () => {
395+
return expectResult(
396+
stripIndent`
397+
function f() {
398+
const a = 1;
399+
return a;
400+
}
401+
const a = f();
402+
a;
403+
`,
404+
optionEC3
405+
).toMatchInlineSnapshot(`1`)
406+
})

‎src/ec-evaluator/interpreter.ts

-3
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ export function evaluate(program: es.Program, context: Context, options: IOption
145145
options.isPrelude
146146
)
147147
} catch (error) {
148-
// console.error('ecerror:', error)
149148
return new ECError(error)
150149
} finally {
151150
context.runtime.isRunning = false
@@ -200,7 +199,6 @@ function evaluateImports(
200199
}
201200
})
202201
} catch (error) {
203-
// console.log(error)
204202
handleRuntimeError(context, error)
205203
}
206204
}
@@ -816,7 +814,6 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
816814
if (
817815
next &&
818816
!(isInstr(next) && next.instrType === InstrType.ENVIRONMENT) &&
819-
args.length !== 0 &&
820817
agenda.some(isNode)
821818
) {
822819
agenda.push(instr.envInstr(currentEnvironment(context), command.srcNode))

0 commit comments

Comments
 (0)
Please sign in to comment.