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
8 changes: 4 additions & 4 deletions src/lib/ruby-generator/sensing.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function (Generator) {

Generator.sensing_askandwait = function (block) {
const question = Generator.valueToCode(block, 'QUESTION', Generator.ORDER_NONE) || null;
return `ask_and_wait(${question})\n`;
return `ask(${question})\n`;
};

Generator.sensing_answer = function () {
Expand All @@ -55,7 +55,7 @@ export default function (Generator) {
};

Generator.sensing_keyoptions = function (block) {
const key = Generator.quote_(Generator.getFieldValue(block, 'KEY_OPTION') || null);
const key = Generator.quote_(Generator.getFieldValue(block, 'KEY_OPTION') || '');
return [key, Generator.ORDER_ATOMIC];
};

Expand All @@ -72,8 +72,8 @@ export default function (Generator) {
};

Generator.sensing_setdragmode = function (block) {
const mode = Generator.quote_(Generator.getFieldValue(block, 'DRAG_MODE') || null);
return `set_drag_mode(${mode})\n`;
const mode = Generator.quote_(Generator.getFieldValue(block, 'DRAG_MODE') || '');
return `self.drag_mode = ${mode}\n`;
};

Generator.sensing_loudness = function () {
Expand Down
48 changes: 48 additions & 0 deletions src/lib/ruby-to-blocks-converter/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const KeyOptions = [
'space',
'left arrow',
'right arrow',
'down arrow',
'up arrow',
'any',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'
];

export {
KeyOptions
};
46 changes: 1 addition & 45 deletions src/lib/ruby-to-blocks-converter/event.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,7 @@
/* global Opal */
import _ from 'lodash';
import Variable from 'scratch-vm/src/engine/variable';

const KeyOptions = [
'space',
'left arrow',
'right arrow',
'down arrow',
'up arrow',
'any',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'
];
import {KeyOptions} from './constants';

const GreaterThanMenu = [
'LOUDNESS',
Expand Down
21 changes: 20 additions & 1 deletion src/lib/ruby-to-blocks-converter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ class RubyToBlocksConverter {
return value && value.type === 'hash';
}

_isConst (value) {
return value && value.type === 'const';
}

_isBlock (block) {
try {
return block.hasOwnProperty('opcode');
Expand Down Expand Up @@ -378,6 +382,9 @@ class RubyToBlocksConverter {
}

_createFieldBlock (opcode, fieldName, value) {
if (this._isBlock(value)) {
return value;
}
return this._createBlock(opcode, 'value', {
fields: {
[fieldName]: {
Expand Down Expand Up @@ -461,6 +468,14 @@ class RubyToBlocksConverter {
this._addInput(block, name, this._createTextBlock(inputValue), shadowBlock);
}

_addFieldInput (block, name, opcode, fieldName, inputValue, shadowValue) {
let shadowBlock;
if (!this._isString(inputValue)) {
shadowBlock = this._createFieldBlock(opcode, fieldName, shadowValue);
}
this._addInput(block, name, this._createFieldBlock(opcode, fieldName, inputValue), shadowBlock);
}

_addSubstack (block, substackBlock, num = 1) {
let name = 'SUBSTACK';
if (num > 1) {
Expand Down Expand Up @@ -893,7 +908,11 @@ class RubyToBlocksConverter {
_onConst (node) {
this._checkNumChildren(node, 2);

return this._createRubyExpressionBlock(this._getSource(node));
const value = {
scope: this._process(node.children[0]),
name: node.children[1].toString()
};
return new Primitive('const', value, node);
}

_onArgs (node) {
Expand Down
13 changes: 7 additions & 6 deletions src/lib/ruby-to-blocks-converter/motion.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/* global Opal */
import _ from 'lodash';

const RotationStyle = [
'left-right',
'don\'t rotate',
'all around'
];

/**
* Motion converter
*/
Expand Down Expand Up @@ -79,12 +85,7 @@ const MotionConverter = {
}
break;
case 'rotation_style=': {
const ROTATION_STYLE = [
'left-right',
'don\'t rotate',
'all around'
];
if (args.length === 1 && this._isString(args[0]) && ROTATION_STYLE.indexOf(args[0].toString()) >= 0) {
if (args.length === 1 && this._isString(args[0]) && RotationStyle.indexOf(args[0].toString()) >= 0) {
block = this._createBlock('motion_setrotationstyle', 'statement');
this._addField(block, 'STYLE', args[0]);
}
Expand Down
9 changes: 2 additions & 7 deletions src/lib/ruby-to-blocks-converter/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,8 @@ const OperatorsConverter = {
operator = name;
}
if (args.length === 1 &&
this._matchRubyExpression(receiver, /^(::)?Math$/) &&
this._isConst(receiver) && receiver.toString() === '::Math' &&
this._isNumberOrBlock(args[0])) {
delete this._context.blocks[receiver.inputs.EXPRESSION.block];
delete this._context.blocks[receiver.id];

block = this._createBlock('operator_mathop', 'value');
this._addField(block, 'OPERATOR', operator);
this._addNumberInput(block, 'NUM', 'math_number', args[0], '');
Expand All @@ -172,10 +169,8 @@ const OperatorsConverter = {
case '**':
if (args.length === 1 && this._isNumberOrBlock(args[0])) {
let operator;
if (this._matchRubyExpression(receiver, /^(::)?Math::E$/)) {
if (this._isConst(receiver) && receiver.toString() === '::Math::E') {
operator = 'e ^';
delete this._context.blocks[receiver.inputs.EXPRESSION.block];
delete this._context.blocks[receiver.id];
} else if (receiver.type === 'int' && receiver.value === 10) {
operator = '10 ^';
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib/ruby-to-blocks-converter/primitive.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global Opal */

/**
* Primitive class for RubyToBlocksConverter
*/
Expand Down Expand Up @@ -34,6 +36,9 @@ class Primitive {
}

toString () {
if (this._type === 'const') {
return `${this._value.scope === Opal.nil ? '' : this._value.scope.toString()}::${this._value.name}`;
}
return this._value.toString();
}

Expand Down
Loading