Change the values runVM returns that relate to cycle limit

This commit is contained in:
Juhani Krekelä 2018-05-24 23:20:52 +03:00
parent 31d5bbbdeb
commit d2e4da1939
1 changed files with 9 additions and 8 deletions

17
gir.js
View File

@ -457,9 +457,9 @@ function newVM(program, input) {
};
}
// (girVMState, int) → {state: girVMState, reachedLimit: bool}
// reachedLimit is set to true if the program ran for maxCycles and did not
// terminate
// (girVMState, int) → {state: girVMState, complete: bool, cycles: int}
// complete is set to true is the program completed its execution
// cycles is the number of cycles the VM ran
// If maxCycles is null, the program runs until completion
function runVM(state, maxCycles = null) {
let program = state.program;
@ -477,12 +477,13 @@ function runVM(state, maxCycles = null) {
let input = state.input;
let output = state.output;
let reachedLimit = true;
for(let cycle = 0; maxCycles === null || cycle < maxCycles; cycle++) {
let complete = false;
let cycle = 0;
for(; maxCycles === null || cycle < maxCycles; cycle++) {
// Exit the loop if we run to the end of the program
if(ip >= program.length) {
// Did not reach limit, program finished
reachedLimit = false;
// Program completed
complete = true;
break;
}
@ -585,7 +586,7 @@ function runVM(state, maxCycles = null) {
output
};
return {state: newState, reachedLimit: reachedLimit};
return {state: newState, complete: complete, cycles: cycle};
}
// ------------------------------------------------------------------