Skip to content

Commit

Permalink
Fix nullary functions on the CSE machine (#1494)
Browse files Browse the repository at this point in the history
* Push env instr even if function has no arguments

* Add tests for nullary functions

* Update test snapshots

---------

Co-authored-by: Martin Henz <[email protected]>
  • Loading branch information
dickongwd and martin-henz authored Oct 23, 2023
1 parent 6aeee1d commit 21a14b3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
38 changes: 38 additions & 0 deletions src/ec-evaluator/__tests__/__snapshots__/ec-evaluator.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 [],
Expand Down
30 changes: 30 additions & 0 deletions src/ec-evaluator/__tests__/ec-evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
})
3 changes: 0 additions & 3 deletions src/ec-evaluator/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -200,7 +199,6 @@ function evaluateImports(
}
})
} catch (error) {
// console.log(error)
handleRuntimeError(context, error)
}
}
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 21a14b3

Please sign in to comment.