source-academy / js-slang

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

Implement continuations through the extension of the CSE-Machine #1547

Closed s-kybound closed 9 months ago

s-kybound commented 9 months ago

This PR implements first-class continuations for the CSE machine by adding two new instructions, GENERATE_CONT and RESUME_CONT, which generate a continuation and consume continuations respectively.

A continuation is a structure capturing a saved state of the program, storing copies of the control, stash as well as a pointer to the environment at the point of time the continuation is captured. When it is called, the program control, stash and environment pointer are forcibly set to that of the continuation, effectively reverting the program back to the point the continuation is generated. Reifying continuations allow us to pass these structures as parameters, assign them and call them at any point of time, perhaps several times, allowing for highly complex program flows.

This is required to implement Scheme through the scm-slang project, but in theory simply by adding the built-in call_with_current_continuation/call/cc to a Source Language's builtins, and by forcing that language to use the explicit control evaluator exclusively, continuations should become first-class for that language.

Changes:

coveralls commented 9 months ago

Pull Request Test Coverage Report for Build 7959703063

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
src/cse-machine/continuations.ts 20 21 95.24%
<!-- Total: 80 81 98.77% -->
Totals Coverage Status
Change from base Build 7959574795: 0.1%
Covered Lines: 10300
Relevant Lines: 12172

💛 - Coveralls