source-academy / js-slang

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

Minimal change to expose steppable functionality from runCSEMachine() #1572

Closed JoelChanZhiYang closed 6 months ago

JoelChanZhiYang commented 6 months ago

Previously, the runCSEMachine function runs until the Control is empty or until the step limit is reached.

It would be useful to extract each step in a generator function and export this functionality. This allows modules to run their own internal "steppable" CSE machine.

The functionality of the runCSEMachine function is unchanged.

// Example use case for generateCSEMachineStateStream

const code = context.unTypecheckedCode[0];
const context = createContext(...)

const program = parse(code, context);

context.runtime.isRunning = true;
context.runtime.control = new Control(program);
context.runtime.stash = new Stash();

const options = ... // Create options

const iterator = generateCSEMachineStateStream(
      context,
      context.runtime.control,
      context.runtime.stash,
      options.envSteps,
      options.stepLimit,
      options.isPrelude,
)

// Use a for loop to access the state at each step. For more control, use iterator.next()
for (const {stash, control, steps} of iterator) {
    // Do something with the state
}
coveralls commented 6 months ago

Pull Request Test Coverage Report for Build 8166526859

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
src/cse-machine/interpreter.ts 5 7 71.43%
<!-- Total: 7 9 77.78% -->
Totals Coverage Status
Change from base Build 8157096221: -0.003%
Covered Lines: 10374
Relevant Lines: 12242

💛 - Coveralls