Skip to content

Commit 7dfa32e

Browse files
committed
Operatorsの命令ブロックの変換処理のいくつかを修正
1 parent 357c02c commit 7dfa32e

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

src/lib/ruby-generator/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ export default function (Blockly) {
108108
return (typeof s === 'string' || s instanceof String);
109109
};
110110

111+
Blockly.Ruby.isWhiteSpace = function (s) {
112+
return s === null || (typeof s === 'string' && s.trim().length === 0);
113+
};
114+
111115
Blockly.Ruby.scalarToCode = function (scalar) {
112116
if (this.isString(scalar)) {
113117
return this.quote_(scalar);

src/lib/ruby-generator/operators.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,70 @@
66
export default function (Blockly) {
77
Blockly.Ruby.operator_add = function (block) {
88
const order = Blockly.Ruby.ORDER_ADDITIVE;
9-
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || '0';
10-
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || '0';
9+
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || 0;
10+
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || 0;
1111
return [`${num1} + ${num2}`, order];
1212
};
1313

1414
Blockly.Ruby.operator_subtract = function (block) {
1515
const order = Blockly.Ruby.ORDER_ADDITIVE;
16-
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || '0';
17-
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || '0';
16+
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || 0;
17+
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || 0;
1818
return [`${num1} - ${num2}`, Blockly.Ruby.ORDER_ADDITIVE];
1919
};
2020

2121
Blockly.Ruby.operator_multiply = function (block) {
2222
const order = Blockly.Ruby.ORDER_MULTIPLICATIVE;
23-
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || '0';
24-
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || '0';
23+
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || 0;
24+
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || 0;
2525
return [`${num1} * ${num2}`, order];
2626
};
2727

2828
Blockly.Ruby.operator_divide = function (block) {
2929
const order = Blockly.Ruby.ORDER_MULTIPLICATIVE;
30-
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || '0';
31-
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || '1';
30+
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || 0.0;
31+
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || 0.0;
3232
return [`${num1} / ${num2}`, order];
3333
};
3434

3535
Blockly.Ruby.operator_random = function (block) {
36-
const fromNum = Blockly.Ruby.valueToCode(block, 'FROM', Blockly.Ruby.ORDER_RANGE) || '0';
37-
const toNum = Blockly.Ruby.valueToCode(block, 'TO', Blockly.Ruby.ORDER_RANGE) || '0';
36+
const fromNum = Blockly.Ruby.valueToCode(block, 'FROM', Blockly.Ruby.ORDER_RANGE) || 1;
37+
const toNum = Blockly.Ruby.valueToCode(block, 'TO', Blockly.Ruby.ORDER_RANGE) || 10;
3838
return [`rand(${fromNum}..${toNum})`, Blockly.Ruby.ORDER_FUNCTION_CALL];
3939
};
4040

41+
const stringOperandToCode = function (operand) {
42+
if (Blockly.Ruby.isString(operand) &&
43+
operand[0] === '"' &&
44+
operand[operand.length - 1] === '"') {
45+
const s = operand.slice(1, operand.length - 1);
46+
const n = Number(s);
47+
if (n !== 0 || !Blockly.Ruby.isWhiteSpace(s)) {
48+
return n;
49+
}
50+
}
51+
return operand;
52+
};
53+
4154
Blockly.Ruby.operator_gt = function (block) {
4255
const order = Blockly.Ruby.ORDER_RELATIONAL;
43-
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || '0';
44-
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || '0';
45-
return [`Cast.compare(${operand1}, ${operand2}) > 0`, order];
56+
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || 0;
57+
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || 0;
58+
return [`${stringOperandToCode(operand1)} > ${stringOperandToCode(operand2)}`, order];
4659
};
4760

4861
Blockly.Ruby.operator_lt = function (block) {
4962
const order = Blockly.Ruby.ORDER_RELATIONAL;
50-
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || '0';
51-
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || '0';
52-
return [`Cast.compare(${operand1}, ${operand2}) < 0`, order];
63+
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || 0;
64+
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || 0;
65+
return [`${stringOperandToCode(operand1)} < ${stringOperandToCode(operand2)}`, order];
5366
};
5467

5568
Blockly.Ruby.operator_equals = function (block) {
5669
const order = Blockly.Ruby.ORDER_EQUALS;
57-
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || '0';
58-
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || '0';
59-
return [`Cast.compare(${operand1}, ${operand2}) == 0`, order];
70+
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || 0;
71+
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || 0;
72+
return [`${stringOperandToCode(operand1)} == ${stringOperandToCode(operand2)}`, order];
6073
};
6174

6275
Blockly.Ruby.operator_and = function (block) {

0 commit comments

Comments
 (0)