"Repeat until" implementation

This commit is contained in:
Tim Mickel 2016-07-01 11:25:26 -04:00
parent dda4fc8332
commit e4f6c9e90c

View file

@ -15,6 +15,7 @@ function Scratch3ControlBlocks(runtime) {
Scratch3ControlBlocks.prototype.getPrimitives = function() { Scratch3ControlBlocks.prototype.getPrimitives = function() {
return { return {
'control_repeat': this.repeat, 'control_repeat': this.repeat,
'control_repeat_until': this.repeatUntil,
'control_forever': this.forever, 'control_forever': this.forever,
'control_wait': this.wait, 'control_wait': this.wait,
'control_if': this.if, 'control_if': this.if,
@ -45,15 +46,31 @@ Scratch3ControlBlocks.prototype.repeat = function(args, util) {
} }
}; };
Scratch3ControlBlocks.prototype.repeatUntil = function(args, util) {
// Only execute once per frame.
// When the substack finishes, `repeat` will be executed again and
// the second branch will be taken, yielding for the rest of the frame.
if (!util.stackFrame.executedInFrame) {
util.stackFrame.executedInFrame = true;
// If the condition is true, start the substack.
if (!args.CONDITION) {
util.startSubstack();
}
} else {
util.stackFrame.executedInFrame = false;
util.yieldFrame();
}
};
Scratch3ControlBlocks.prototype.forever = function(args, util) { Scratch3ControlBlocks.prototype.forever = function(args, util) {
// Only execute once per frame. // Only execute once per frame.
// When the substack finishes, `forever` will be executed again and // When the substack finishes, `forever` will be executed again and
// the second branch will be taken, yielding for the rest of the frame. // the second branch will be taken, yielding for the rest of the frame.
if (!util.stackFrame.executed) { if (!util.stackFrame.executedInFrame) {
util.stackFrame.executed = true; util.stackFrame.executedInFrame = true;
util.startSubstack(); util.startSubstack();
} else { } else {
util.stackFrame.executed = false; util.stackFrame.executedInFrame = false;
util.yieldFrame(); util.yieldFrame();
} }
}; };
@ -69,8 +86,8 @@ Scratch3ControlBlocks.prototype.wait = function(args) {
Scratch3ControlBlocks.prototype.if = function(args, util) { Scratch3ControlBlocks.prototype.if = function(args, util) {
// Only execute one time. `if` will be returned to // Only execute one time. `if` will be returned to
// when the substack finishes, but it shouldn't execute again. // when the substack finishes, but it shouldn't execute again.
if (util.stackFrame.executed === undefined) { if (util.stackFrame.executedInFrame === undefined) {
util.stackFrame.executed = true; util.stackFrame.executedInFrame = true;
if (args.CONDITION) { if (args.CONDITION) {
util.startSubstack(); util.startSubstack();
} }