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
260 changes: 135 additions & 125 deletions src/lib/ruby-to-blocks-converter/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,97 +12,28 @@ const OperatorsConverter = {
switch (name) {
case 'rand':
if (args.length === 1 && this._isBlock(args[0]) && args[0].opcode === 'ruby_range') {
block = this._changeBlock(args[0], 'operator_random', 'value');
return this._changeBlock(args[0], 'operator_random', 'value');
}
break;
}
} else if (!this._isVariableBlock(receiver)) {
}

if (!this._isVariableBlock(receiver)) {
switch (name) {
case '+':
case '-':
case '*':
case '/':
case '%':
if (args.length === 1) {
if (this._isNumberOrBlock(receiver) && this._isNumberOrBlock(args[0])) {
let opcode;
if (name === '+') {
opcode = 'operator_add';
} else if (name === '-') {
opcode = 'operator_subtract';
} else if (name === '*') {
opcode = 'operator_multiply';
} else if (name === '/') {
opcode = 'operator_divide';
} else {
opcode = 'operator_mod';
}
block = this._createBlock(opcode, 'value');
this._addNumberInput(block, 'NUM1', 'math_number', receiver, '');
this._addNumberInput(block, 'NUM2', 'math_number', args[0], '');
} else if (name === '+' &&
(this._isStringOrBlock(receiver) || this._isStringOrBlock(args[0]))) {
block = this._createBlock('operator_join', 'value');
this._addTextInput(
block,
'STRING1',
this._isNumber(receiver) ? receiver.toString() : receiver,
'apple'
);
this._addTextInput(
block,
'STRING2',
this._isNumber(args[0]) ? args[0].toString() : args[0],
'banana'
);
}
}
break;
case '>':
case '<':
case '==':
if (args.length === 1) {
let opcode;
if (name === '>') {
opcode = 'operator_gt';
} else if (name === '<') {
opcode = 'operator_lt';
} else {
opcode = 'operator_equals';
}
block = this._createBlock(opcode, 'value_boolean');
this._addTextInput(
block, 'OPERAND1', this._isNumber(receiver) ? receiver.toString() : receiver, ''
);
this._addTextInput(
block, 'OPERAND2', this._isNumber(args[0]) ? args[0].toString() : args[0], '50'
);
}
break;
case '!':
if (args.length === 0 && this._isFalseOrBooleanBlock(receiver)) {
block = this._createBlock('operator_not', 'value_boolean');
if (!this._isFalse(receiver)) {
this._addInput(
block,
'OPERAND',
this._createTextBlock(this._isNumber(receiver) ? receiver.toString() : receiver)
);
}
}
break;
case '[]':
if (this._isStringOrBlock(receiver) &&
args.length === 1 && this._isNumberOrBlock(args[0])) {
block = this._createBlock('operator_letter_of', 'value');
this._addTextInput(block, 'STRING', receiver, 'apple');
this._addNumberInput(block, 'LETTER', 'math_number', args[0], 1);
return block;
}
break;
case 'length':
if (args.length === 0 && this._isStringOrBlock(receiver)) {
block = this._createBlock('operator_length', 'value');
this._addTextInput(block, 'STRING', receiver, 'apple');
return block;
}
break;
case 'include?':
Expand All @@ -111,41 +42,123 @@ const OperatorsConverter = {
block = this._createBlock('operator_contains', 'value');
this._addTextInput(block, 'STRING1', receiver, 'apple');
this._addTextInput(block, 'STRING2', args[0], 'a');
return block;
}
break;
case 'round':
if (args.length === 0 && this._isNumberOrBlock(receiver)) {
block = this._createBlock('operator_round', 'value');
this._addNumberInput(block, 'NUM', 'math_number', receiver, '');
}
}

switch (name) {
case '+':
case '-':
case '*':
case '/':
case '%':
if (args.length === 1) {
if (this._isNumberOrBlock(receiver) && this._isNumberOrBlock(args[0])) {
let opcode;
if (name === '+') {
opcode = 'operator_add';
} else if (name === '-') {
opcode = 'operator_subtract';
} else if (name === '*') {
opcode = 'operator_multiply';
} else if (name === '/') {
opcode = 'operator_divide';
} else {
opcode = 'operator_mod';
}
block = this._createBlock(opcode, 'value');
this._addNumberInput(block, 'NUM1', 'math_number', receiver, '');
this._addNumberInput(block, 'NUM2', 'math_number', args[0], '');
return block;
} else if (name === '+' &&
(this._isStringOrBlock(receiver) || this._isStringOrBlock(args[0]))) {
block = this._createBlock('operator_join', 'value');
this._addTextInput(
block,
'STRING1',
this._isNumber(receiver) ? receiver.toString() : receiver,
'apple'
);
this._addTextInput(
block,
'STRING2',
this._isNumber(args[0]) ? args[0].toString() : args[0],
'banana'
);
return block;
}
break;
case 'abs':
case 'floor':
case 'ceil': {
let operator;
switch (name) {
case 'ceil':
operator = 'ceiling';
break;
default:
operator = name;
}
break;
case '>':
case '<':
case '==':
if (args.length === 1) {
let opcode;
if (name === '>') {
opcode = 'operator_gt';
} else if (name === '<') {
opcode = 'operator_lt';
} else {
opcode = 'operator_equals';
}
if (args.length === 0 && this._isNumberOrBlock(receiver)) {
block = this._createBlock('operator_mathop', 'value');
this._addField(block, 'OPERATOR', operator);
this._addNumberInput(block, 'NUM', 'math_number', receiver, '');
block = this._createBlock(opcode, 'value_boolean');
this._addTextInput(
block, 'OPERAND1', this._isNumber(receiver) ? receiver.toString() : receiver, ''
);
this._addTextInput(
block, 'OPERAND2', this._isNumber(args[0]) ? args[0].toString() : args[0], '50'
);
return block;
}
break;
case '!':
if (args.length === 0 && this._isFalseOrBooleanBlock(receiver)) {
block = this._createBlock('operator_not', 'value_boolean');
if (!this._isFalse(receiver)) {
this._addInput(
block,
'OPERAND',
this._createTextBlock(this._isNumber(receiver) ? receiver.toString() : receiver)
);
}
break;
return block;
}
break;
case 'round':
if (args.length === 0 && this._isNumberOrBlock(receiver)) {
block = this._createBlock('operator_round', 'value');
this._addNumberInput(block, 'NUM', 'math_number', receiver, '');
return block;
}
case 'sqrt':
case 'sin':
case 'cos':
case 'tan':
case 'asin':
case 'acos':
case 'atan':
case 'log':
case 'log10': {
break;
case 'abs':
case 'floor':
case 'ceil':
if (args.length === 0 && this._isNumberOrBlock(receiver)) {
let operator = name;
if (name === 'ceil') {
operator = 'ceiling';
}
block = this._createBlock('operator_mathop', 'value');
this._addField(block, 'OPERATOR', operator);
this._addNumberInput(block, 'NUM', 'math_number', receiver, '');
return block;
}
break;
case 'sqrt':
case 'sin':
case 'cos':
case 'tan':
case 'asin':
case 'acos':
case 'atan':
case 'log':
case 'log10':
if (args.length === 1 &&
this._isConst(receiver) && receiver.toString() === '::Math' &&
this._isNumberOrBlock(args[0])) {
let operator;
switch (name) {
case 'log':
Expand All @@ -157,34 +170,31 @@ const OperatorsConverter = {
default:
operator = name;
}
if (args.length === 1 &&
this._isConst(receiver) && receiver.toString() === '::Math' &&
this._isNumberOrBlock(args[0])) {
block = this._createBlock('operator_mathop', 'value');
this._addField(block, 'OPERATOR', operator);
this._addNumberInput(block, 'NUM', 'math_number', args[0], '');
return block;
}
break;
case '**':
if (args.length === 1 && this._isNumberOrBlock(args[0])) {
let operator;
if (this._isConst(receiver) && receiver.toString() === '::Math::E') {
operator = 'e ^';
} else if (receiver.type === 'int' && receiver.value === 10) {
operator = '10 ^';
}
if (operator) {
block = this._createBlock('operator_mathop', 'value');
this._addField(block, 'OPERATOR', operator);
this._addNumberInput(block, 'NUM', 'math_number', args[0], '');
return block;
}
break;
}
case '**':
if (args.length === 1 && this._isNumberOrBlock(args[0])) {
let operator;
if (this._isConst(receiver) && receiver.toString() === '::Math::E') {
operator = 'e ^';
} else if (receiver.type === 'int' && receiver.value === 10) {
operator = '10 ^';
}
if (operator) {
block = this._createBlock('operator_mathop', 'value');
this._addField(block, 'OPERATOR', operator);
this._addNumberInput(block, 'NUM', 'math_number', args[0], '');
}
}
break;
}
break;
}

return block;
return null;
},

// eslint-disable-next-line no-unused-vars
Expand Down
55 changes: 54 additions & 1 deletion test/unit/lib/ruby-to-blocks-converter/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import RubyToBlocksConverter from '../../../../src/lib/ruby-to-blocks-converter';
import {
convertAndExpectToEqualBlocks,
rubyToExpected
rubyToExpected,
expectedInfo
} from '../../../helpers/expect-to-equal-blocks';

describe('RubyToBlocksConverter', () => {
Expand Down Expand Up @@ -195,6 +196,58 @@ describe('RubyToBlocksConverter', () => {
expect(converter.errors[0].row).toEqual(2);
expect(res).toBeFalsy();
});

test('==, <, > and variable', () => {
code = `
if !($global == 21)
bounce_if_on_edge
end
`;
expected = [
{
opcode: 'control_if',
inputs: [
{
name: 'CONDITION',
block: {
opcode: 'operator_not',
inputs: [
{
name: 'OPERAND',
block: {
opcode: 'operator_equals',
inputs: [
{
name: 'OPERAND1',
block: {
opcode: 'data_variable',
fields: [
{
name: 'VARIABLE',
variable: '$global'
}
]
},
shadow: expectedInfo.makeText('')
},
{
name: 'OPERAND2',
block: expectedInfo.makeText('21')
}
]
}
}
]
}
}
],
branches: [
rubyToExpected(converter, target, 'bounce_if_on_edge')[0]
]
}
];
convertAndExpectToEqualBlocks(converter, target, code, expected);
});
});
});
});
Loading