Skip to content

Commit

Permalink
Further improvements to environment visualizer (#1454)
Browse files Browse the repository at this point in the history
* Env instruction no longer pushed onto an agenda as the last item

* Avoid unnecessary block statements

* Refactoring context.runtime.envsteps to an option paramter to be in line with the notion that the context shouldn't be updated by the frontend

* * Use the step limit option to allow users to limit the number of steps to the ec-evaluator
* Fixed bug with ec-evaluator: conditional statements now produce undefined if no value is produced since they are value producing.

* Add check for simple functions and bypass to return argument if so

* Add reset instruction only if there is no marker next

* Check for arguments in the application instruction before pushing environment instruction

* Avoid unnecessary break and continue markers

* Add support for rest syntax in ec-evaluator

---------

Co-authored-by: Dick Ong <[email protected]>
  • Loading branch information
vansh284 and dickongwd authored Jul 30, 2023
1 parent a8b7263 commit 29a852e
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 52 deletions.
31 changes: 31 additions & 0 deletions src/ec-evaluator/__tests__/__snapshots__/ec-evaluator.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Conditional statements are value producing always: expectResult 1`] = `
Object {
"alertResult": Array [],
"code": "function fact(n) {
if (n === 0) {
2;
return 1;
}
if (true) {
let i = 1;
i = i - 1;
} else {
2;
}
if (false) {
2;
} else {
const i = 1;
}
return n * fact(n - 1);
}
fact(5);",
"displayResult": Array [],
"numErrors": 0,
"parsedErrors": "",
"result": 120,
"resultStatus": "finished",
"visualiseListResult": Array [],
}
`;

exports[`Imports are properly handled: expectResult 1`] = `
Object {
"alertResult": Array [],
Expand Down
8 changes: 4 additions & 4 deletions src/ec-evaluator/__tests__/ec-evaluator-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ test('Infinite recursion with a block bodied function', () => {
function i(n) {
return n === 0 ? 0 : 1 + i(n-1);
}
i(30000);
i(300000);
`,
optionEC4
).toEqual(expect.stringMatching(/Maximum call stack size exceeded\n *(i\(\d*\)[^i]{2,4}){3}/))
Expand All @@ -166,7 +166,7 @@ test('Infinite recursion with function calls in argument', () => {
function r() {
return 1;
}
i(30000, 1);
i(300000, 1);
`,
optionEC4
).toEqual(
Expand All @@ -183,7 +183,7 @@ test('Infinite recursion of mutually recursive functions', () => {
function g(n) {
return 1 + f(n);
}
f(20000);
f(200000);
`,
optionEC4
).toEqual(
Expand Down Expand Up @@ -937,7 +937,7 @@ test('Check that stack is at most 10k in size', () => {
return 1 + f(x-1);
}
}
f(30000);
f(300000);
`,
optionEC
).toEqual(expect.stringMatching(/Maximum call stack size exceeded\n([^f]*f){3}/))
Expand Down
27 changes: 27 additions & 0 deletions src/ec-evaluator/__tests__/ec-evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,30 @@ test('Imports are properly handled', () => {
optionEC
).toEqual('foo')
})

test('Conditional statements are value producing always', () => {
return expectResult(
stripIndent`
function fact(n) {
if (n === 0) {
2;
return 1;
}
if (true) {
let i = 1;
i = i - 1;
} else {
2;
}
if (false) {
2;
} else {
const i = 1;
}
return n * fact(n - 1);
}
fact(5);
`,
optionEC3
).toMatchInlineSnapshot(`120`)
})
Loading

0 comments on commit 29a852e

Please sign in to comment.