From 27d053acffe461864c463ebf5adad29eb6f83da8 Mon Sep 17 00:00:00 2001 From: Florrie Date: Sat, 7 Apr 2018 13:51:29 -0300 Subject: [PATCH 1/3] Add doWhile -> control_while to specmap --- src/serialization/sb2_specmap.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/serialization/sb2_specmap.js b/src/serialization/sb2_specmap.js index 637ccbab..933c4d6a 100644 --- a/src/serialization/sb2_specmap.js +++ b/src/serialization/sb2_specmap.js @@ -768,6 +768,19 @@ const specMap = { } ] }, + 'doWhile': { + opcode: 'control_while', + argMap: [ + { + type: 'input', + inputName: 'CONDITION' + }, + { + type: 'input', + inputName: 'SUBSTACK' + } + ] + }, 'doForLoop': { opcode: 'control_for_each', argMap: [ From c57d0c94eb45b393112530a32f3aac0e9a2f2a0f Mon Sep 17 00:00:00 2001 From: Florrie Date: Sat, 7 Apr 2018 13:56:28 -0300 Subject: [PATCH 2/3] Add implementation for while --- src/blocks/scratch3_control.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/blocks/scratch3_control.js b/src/blocks/scratch3_control.js index 15a43fb6..76d077b1 100644 --- a/src/blocks/scratch3_control.js +++ b/src/blocks/scratch3_control.js @@ -17,6 +17,7 @@ class Scratch3ControlBlocks { return { control_repeat: this.repeat, control_repeat_until: this.repeatUntil, + control_while: this.repeatWhile, control_for_each: this.forEach, control_forever: this.forever, control_wait: this.wait, @@ -56,12 +57,20 @@ class Scratch3ControlBlocks { repeatUntil (args, util) { const condition = Cast.toBoolean(args.CONDITION); - // If the condition is true, start the branch. + // If the condition is false (repeat UNTIL), start the branch. if (!condition) { util.startBranch(1, true); } } + repeatWhile (args, util) { + const condition = Cast.toBoolean(args.CONDITION); + // If the condition is true (repeat WHILE), start the branch. + if (condition) { + util.startBranch(1, true); + } + } + forEach (args, util) { const variable = util.target.lookupOrCreateVariable( args.VARIABLE.id, args.VARIABLE.name); From 466a3b62efa7c9c8c7b89a5d8577c2505f64c9bb Mon Sep 17 00:00:00 2001 From: Florrie Date: Sat, 7 Apr 2018 14:07:15 -0300 Subject: [PATCH 3/3] Add test for repeatWhile --- test/unit/blocks_control.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/unit/blocks_control.js b/test/unit/blocks_control.js index 3e89ab95..9d46fc01 100644 --- a/test/unit/blocks_control.js +++ b/test/unit/blocks_control.js @@ -52,6 +52,28 @@ test('repeatUntil', t => { t.end(); }); +test('repeatWhile', t => { + const rt = new Runtime(); + const c = new Control(rt); + + // Test harness (mocks `util`) + let i = 0; + const repeat = 10; + const util = { + stackFrame: Object.create(null), + startBranch: function () { + i++; + // Note !== instead of === + c.repeatWhile({CONDITION: (i !== repeat)}, util); + } + }; + + // Execute test + c.repeatWhile({CONDITION: (i !== repeat)}, util); + t.strictEqual(i, repeat); + t.end(); +}); + test('forEach', t => { const rt = new Runtime(); const c = new Control(rt);