|
6 | 6 | export default function (Blockly) { |
7 | 7 | Blockly.Ruby.operator_add = function (block) { |
8 | 8 | 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; |
11 | 11 | return [`${num1} + ${num2}`, order]; |
12 | 12 | }; |
13 | 13 |
|
14 | 14 | Blockly.Ruby.operator_subtract = function (block) { |
15 | 15 | 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; |
18 | 18 | return [`${num1} - ${num2}`, Blockly.Ruby.ORDER_ADDITIVE]; |
19 | 19 | }; |
20 | 20 |
|
21 | 21 | Blockly.Ruby.operator_multiply = function (block) { |
22 | 22 | 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; |
25 | 25 | return [`${num1} * ${num2}`, order]; |
26 | 26 | }; |
27 | 27 |
|
28 | 28 | Blockly.Ruby.operator_divide = function (block) { |
29 | 29 | 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; |
32 | 32 | return [`${num1} / ${num2}`, order]; |
33 | 33 | }; |
34 | 34 |
|
35 | 35 | 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; |
38 | 38 | return [`rand(${fromNum}..${toNum})`, Blockly.Ruby.ORDER_FUNCTION_CALL]; |
39 | 39 | }; |
40 | 40 |
|
| 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 | + |
41 | 54 | Blockly.Ruby.operator_gt = function (block) { |
42 | 55 | 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]; |
46 | 59 | }; |
47 | 60 |
|
48 | 61 | Blockly.Ruby.operator_lt = function (block) { |
49 | 62 | 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]; |
53 | 66 | }; |
54 | 67 |
|
55 | 68 | Blockly.Ruby.operator_equals = function (block) { |
56 | 69 | 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]; |
60 | 73 | }; |
61 | 74 |
|
62 | 75 | Blockly.Ruby.operator_and = function (block) { |
|
0 commit comments