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
51 changes: 48 additions & 3 deletions src/lib/ruby-to-blocks-converter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class RubyToBlocksConverter {
MyBlocksConverter
];
this._receiverToMethods = {};
this._receiverToMyBlocks = {};
this.reset();

[
Expand All @@ -134,7 +135,8 @@ class RubyToBlocksConverter {
GdxForConverter,
SmalrubotS1Converter,
MotionConverter,
SensingConverter
SensingConverter,
MyBlocksConverter
].forEach(x => x.register(this));
}

Expand Down Expand Up @@ -343,16 +345,59 @@ class RubyToBlocksConverter {
this.registerCallMethodWithBlock(receiverName, name, numArgs, 'none', createBlockFunc);
}

registerCallMyBlock (receiverName, myBlockHandler) {
if (receiverName === 'any') {
this._anyReceiverNames().forEach(rn => this.registerCallMyBlock(rn, myBlockHandler));
return;
}

if (_.isArray(receiverName)) {
receiverName.forEach(rn => this.registerCallMyBlock(rn, myBlockHandler));
return;
}

if (receiverName === 'self') {
this.registerCallMyBlock('sprite', myBlockHandler);
this.registerCallMyBlock('stage', myBlockHandler);
return;
}

if (!this._receiverToMyBlocks[receiverName]) {
this._receiverToMyBlocks[receiverName] = [];
}
this._receiverToMyBlocks[receiverName].push(myBlockHandler);
}

callMethod (receiver, name, args, rubyBlockArgs, rubyBlock, node) {
const receiverName = this._getReceiverName(receiver);
if (!receiverName) return null;

// Check for my-block procedure calls
if (this._receiverToMyBlocks[receiverName]) {
const procedure = this._lookupProcedure(name);
if (procedure) {
const params = {
receiver: receiver,
receiverName: receiverName,
name: name,
args: args,
rubyBlockArgs: rubyBlockArgs,
rubyBlock: rubyBlock,
node: node,
procedure: procedure
};

for (const handler of this._receiverToMyBlocks[receiverName]) {
const block = handler.apply(this, [params]);
if (block) return block;
}
}
}

const methodToNumArgs = this._receiverToMethods[receiverName];
if (!methodToNumArgs) return null;

const numArgsToNumRubyBlockArgs = methodToNumArgs[name];
if (!numArgsToNumRubyBlockArgs) return null;

const numRubyBlockArgsToCreateBlockFuncs = numArgsToNumRubyBlockArgs[args.length];
if (!numRubyBlockArgsToCreateBlockFuncs) return null;

Expand Down
90 changes: 44 additions & 46 deletions src/lib/ruby-to-blocks-converter/my-blocks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* global Opal */
import _ from 'lodash';
import Blockly from 'scratch-blocks';
import {RubyToBlocksConverterError} from './errors';
Expand All @@ -7,54 +6,53 @@ import {RubyToBlocksConverterError} from './errors';
* My Blocks converter
*/
const MyBlocksConverter = {
// eslint-disable-next-line no-unused-vars
onSend: function (receiver, name, args, rubyBlockArgs, rubyBlock) {
let block;
if (this._isSelf(receiver) || receiver === Opal.nil) {
const procedure = this._lookupProcedure(name);
if (procedure) {
if (procedure.argumentIds.length === args.length) {
block = this._createBlock('procedures_call', 'statement', {
mutation: {
argumentids: JSON.stringify(procedure.argumentIds),
children: [],
proccode: procedure.procCode.join(' '),
tagName: 'mutation',
warp: 'false'
}
});
register: function (converter) {
// Register my-block handler for procedure calls
converter.registerCallMyBlock('self', params => {
const {name, args, procedure} = params;

if (Object.prototype.hasOwnProperty.call(this._context.procedureCallBlocks, procedure.id)) {
this._context.procedureCallBlocks[procedure.id].push(block.id);
} else {
this._context.procedureCallBlocks[procedure.id] = [block.id];
}
if (procedure.argumentIds.length !== args.length) return null;

args.forEach((arg, i) => {
const argumentId = procedure.argumentIds[i];
if (this._isFalseOrBooleanBlock(arg)) {
if (procedure.argumentVariables[i].isBoolean ||
this._changeToBooleanArgument(procedure.argumentNames[i])) {
if (!this._isFalse(arg)) {
this._addInput(block, argumentId, arg, null);
}
return;
}
}
if (!procedure.argumentVariables[i].isBoolean &&
(this._isNumberOrBlock(arg) || this._isStringOrBlock(arg))) {
this._addTextInput(block, argumentId, this._isNumber(arg) ? arg.toString() : arg, '');
return;
}
throw new RubyToBlocksConverterError(
this._context.currentNode,
`invalid type of My Block "${name}" argument #${i + 1}`
);
});
const block = converter._createBlock('procedures_call', 'statement', {
mutation: {
argumentids: JSON.stringify(procedure.argumentIds),
children: [],
proccode: procedure.procCode.join(' '),
tagName: 'mutation',
warp: 'false'
}
});

if (Object.prototype.hasOwnProperty.call(converter._context.procedureCallBlocks, procedure.id)) {
converter._context.procedureCallBlocks[procedure.id].push(block.id);
} else {
converter._context.procedureCallBlocks[procedure.id] = [block.id];
}
}
return block;

args.forEach((arg, i) => {
const argumentId = procedure.argumentIds[i];
if (converter._isFalseOrBooleanBlock(arg)) {
if (procedure.argumentVariables[i].isBoolean ||
converter._changeToBooleanArgument(procedure.argumentNames[i])) {
if (!converter._isFalse(arg)) {
converter._addInput(block, argumentId, arg, null);
}
return;
}
}
if (!procedure.argumentVariables[i].isBoolean &&
(converter._isNumberOrBlock(arg) || converter._isStringOrBlock(arg))) {
converter._addTextInput(block, argumentId, converter._isNumber(arg) ? arg.toString() : arg, '');
return;
}
throw new RubyToBlocksConverterError(
converter._context.currentNode,
`invalid type of My Block "${name}" argument #${i + 1}`
);
});

return block;
});
},

// eslint-disable-next-line no-unused-vars
Expand Down Expand Up @@ -112,7 +110,7 @@ const MyBlocksConverter = {
const originalName = n.toString();
// Convert argument name to snake_case lowercase
const normalizedName = this._toSnakeCaseLowercase(originalName);

procedure.argumentNames.push(normalizedName);
procedure.argumentVariables.push(this._lookupOrCreateVariable(normalizedName));
procedure.procCode.push('%s');
Expand Down
Loading