Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/lib/ruby-generator/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export default function (Generator) {
Generator.control_repeat = function (block) {
const times = Generator.valueToCode(block, 'TIMES', Generator.ORDER_ATOMIC) || 0;
const branch = Generator.statementToCode(block, 'SUBSTACK') || '';
return `${times}.times do\n${branch}${Generator.INDENT}wait\nend\n`;
return `${times}.times do\n${branch}end\n`;
};

Generator.control_forever = function (block) {
const branch = Generator.statementToCode(block, 'SUBSTACK') || '';
return `loop do\n${branch}${Generator.INDENT}wait\nend\n`;
return `loop do\n${branch}end\n`;
};

Generator.control_if = function (block) {
Expand All @@ -41,7 +41,7 @@ export default function (Generator) {
Generator.control_repeat_until = function (block) {
const operator = Generator.valueToCode(block, 'CONDITION', Generator.ORDER_NONE) || false;
const branch = Generator.statementToCode(block, 'SUBSTACK') || '';
return `until ${operator}\n${branch} wait\nend\n`;
return `until ${operator}\n${branch}end\n`;
};

Generator.control_stop = function (block) {
Expand Down
37 changes: 8 additions & 29 deletions src/lib/ruby-to-blocks-converter/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,16 @@ const ControlConverter = {
case 'repeat':
if (args.length === 1 && this._isNumberOrBlock(args[0]) &&
rubyBlockArgs && rubyBlockArgs.length === 0) {
rubyBlock = this._removeWaitBlocks(rubyBlock);
block = createControlRepeatBlock.call(this, args[0], rubyBlock);
}
break;
case 'loop':
case 'forever':
if (args.length === 0 && rubyBlockArgs && rubyBlockArgs.length === 0 && rubyBlock) {
let found;
if (name === 'loop') {
const b = this._popWaitBlock(rubyBlock);
if (b) {
if (b.id === rubyBlock.id) {
rubyBlock = null;
}
found = true;
}
} else {
found = true;
}
if (found) {
block = this._createBlock('control_forever', 'terminate');
this._addSubstack(block, rubyBlock);
}
rubyBlock = this._removeWaitBlocks(rubyBlock);
block = this._createBlock('control_forever', 'terminate');
this._addSubstack(block, rubyBlock);
}
break;
case 'stop':
Expand Down Expand Up @@ -97,13 +85,8 @@ const ControlConverter = {
case 'times':
if (args.length === 0 &&
rubyBlockArgs && rubyBlockArgs.length === 0 && rubyBlock) {
const b = this._popWaitBlock(rubyBlock);
if (b) {
if (b.id === rubyBlock.id) {
rubyBlock = null;
}
block = createControlRepeatBlock.call(this, receiver, rubyBlock);
}
rubyBlock = this._removeWaitBlocks(rubyBlock);
block = createControlRepeatBlock.call(this, receiver, rubyBlock);
}
break;
}
Expand All @@ -125,14 +108,10 @@ const ControlConverter = {
},

onUntil: function (cond, statement) {
const b = this._popWaitBlock(statement);
if (!b) {
return null;
}
statement = this._removeWaitBlocks(statement);

let opcode;
if (b.id === statement.id) {
statement = null;
if (statement === null) {
opcode = 'control_wait_until';
} else {
opcode = 'control_repeat_until';
Expand Down
40 changes: 27 additions & 13 deletions src/lib/ruby-to-blocks-converter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,26 +684,40 @@ class RubyToBlocksConverter {
return b;
}

_popWaitBlock (block) {
if (!block) {
_removeWaitBlocks (block) {
if (!block || block === Opal.nil) {
return null;
}

const b = this._lastBlock(block);
if (b.opcode === 'ruby_statement') {
const textBlock = this._context.blocks[b.inputs.STATEMENT.block];
if (textBlock.fields.TEXT.value === 'wait') {
if (b.parent) {
const parent = this._context.blocks[b.parent];
if (parent.next === b.id) {
parent.next = null;
}
let firstBlock = null;
let b = block;
let prev = b.parent;
while (b) {
let isWaitBlock = false;
if (b.opcode === 'ruby_statement') {
const textBlock = this._context.blocks[b.inputs.STATEMENT.block];
if (textBlock.fields.TEXT.value === 'wait') {
isWaitBlock = true;
}
}
if (isWaitBlock) {
delete this._context.blocks[b.id];
return b;
if (prev) {
this._context.blocks[prev].next = null;
}
} else {
if (firstBlock === null) {
firstBlock = b;
}
b.parent = prev;
if (prev) {
this._context.blocks[prev].next = b.id;
}
prev = b.id;
}
b = this._context.blocks[b.next];
}
return null;
return firstBlock;
}

_getBlockType (block) {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/blocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ describe('Working with the blocks', () => {
// wait until the new costume appears in costume item list panel
await findByXpath("//div[contains(@class,'sprite-selector-item_is-selected_')]" +
"//div[contains(text(), 'costume2')]");
await clickText('costume2', scope.costumesTab);
await clickText('costume1', scope.costumesTab);
// Check that the menu has been updated
await clickText('Code');
await clickText('costume2', scope.blocksTab);
Expand Down
Loading