source-academy / js-slang

Implementations of sublanguages of JavaScript, TypeScript, Scheme and Python
https://source-academy.github.io/source/
Apache License 2.0
64 stars 102 forks source link

CSE: Avoid unnecessary env instructions #1682

Closed martin-henz closed 2 months ago

martin-henz commented 2 months ago

Consider:

Screenshot 2024-04-17 at 1 08 56 PM

The env instructions are not needed because there are only * instructions on control. More generally, an env instruction can be avoided if there are no statements or expressions on the control from the top of the control stack until the next env instruction. "Statements or expressions" includes instructions that have statements or expressions inside, such as branch instructions.

We should also avoid environment instructions when the environment does not change: application of nullary function that is declared in the current environment.

Screenshot 2024-04-23 at 7 56 43 AM Screenshot 2024-04-23 at 7 56 58 AM

The pedagogical strategy is to introduce "complications" such as env instructions only when needed. Simple programs have simple explanations and complex programs may require more complex explanations.

martin-henz commented 2 months ago

Good to see that https://github.com/source-academy/js-slang/pull/1687 implements a version that captures both specifi cases. The generalizations that I mentioned need a bit more thought. Let's keep this issue open, to serve as inspiration later.