Add implementation for ifElse

This commit is contained in:
Tim Mickel 2016-06-10 10:40:15 -04:00
parent ca68c55d57
commit 0bf9ab59a1
3 changed files with 25 additions and 6 deletions

View file

@ -16,6 +16,7 @@ Scratch3ControlBlocks.prototype.getPrimitives = function() {
'control_forever': this.forever, 'control_forever': this.forever,
'control_wait': this.wait, 'control_wait': this.wait,
'control_if': this.if, 'control_if': this.if,
'control_if_else': this.ifElse,
'control_stop': this.stop 'control_stop': this.stop
}; };
}; };
@ -55,6 +56,19 @@ Scratch3ControlBlocks.prototype.if = function(args, util) {
} }
}; };
Scratch3ControlBlocks.prototype.ifElse = function(args, util) {
// Only execute one time. `ifElse` will be returned to
// when the substack finishes, but it shouldn't execute again.
if (util.stackFrame.executed === undefined) {
util.stackFrame.executed = true;
if (args.CONDITION) {
util.startSubstack(1);
} else {
util.startSubstack(2);
}
}
};
Scratch3ControlBlocks.prototype.stop = function() { Scratch3ControlBlocks.prototype.stop = function() {
// @todo - don't use this.runtime // @todo - don't use this.runtime
this.runtime.stopAll(); this.runtime.stopAll();

View file

@ -69,8 +69,8 @@ var execute = function (sequencer, thread) {
}, },
timeout: YieldTimers.timeout, timeout: YieldTimers.timeout,
stackFrame: currentStackFrame, stackFrame: currentStackFrame,
startSubstack: function () { startSubstack: function (substackNum) {
sequencer.stepToSubstack(thread); sequencer.stepToSubstack(thread, substackNum);
} }
}); });
} }

View file

@ -104,11 +104,17 @@ Sequencer.prototype.startThread = function (thread) {
/** /**
* Step a thread into a block's substack. * Step a thread into a block's substack.
* @param {!Thread} thread Thread object to step to substack. * @param {!Thread} thread Thread object to step to substack.
* @param {string} currentBlockId Block which owns a substack to step to. * @param {Number} substackNum Which substack to step to (i.e., 1, 2).
*/ */
Sequencer.prototype.stepToSubstack = function (thread) { Sequencer.prototype.stepToSubstack = function (thread, substackNum) {
if (!substackNum) {
substackNum = 1;
}
var currentBlockId = thread.peekStack(); var currentBlockId = thread.peekStack();
var substackId = this.runtime.blocks.getSubstack(currentBlockId); var substackId = this.runtime.blocks.getSubstack(
currentBlockId,
substackNum
);
if (substackId) { if (substackId) {
// Push substack ID to the thread's stack. // Push substack ID to the thread's stack.
thread.pushStack(substackId); thread.pushStack(substackId);
@ -121,7 +127,6 @@ Sequencer.prototype.stepToSubstack = function (thread) {
/** /**
* Finish stepping a thread and proceed it to the next block. * Finish stepping a thread and proceed it to the next block.
* @param {!Thread} thread Thread object to proceed. * @param {!Thread} thread Thread object to proceed.
* @param {string} currentBlockId Block we are finished with.
*/ */
Sequencer.prototype.proceedThread = function (thread) { Sequencer.prototype.proceedThread = function (thread) {
var currentBlockId = thread.peekStack(); var currentBlockId = thread.peekStack();