Skip to content

Commit c89f3f0

Browse files
author
t-kazu
committed
優先度を変更しました
1 parent e316a49 commit c89f3f0

File tree

1 file changed

+77
-61
lines changed

1 file changed

+77
-61
lines changed

src/lib/ruby-generator/operators.js

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,85 +5,98 @@
55
*/
66
export default function (Blockly) {
77
Blockly.Ruby.operator_add = function (block) {
8-
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', Blockly.Ruby.ORDER_NONE) || '0';
9-
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', Blockly.Ruby.ORDER_NONE) || '0';
10-
return [`${num1} + ${num2}`, Blockly.Ruby.ORDER_ATOMIC];
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';
11+
return [`${num1} + ${num2}`, order];
1112
};
1213

1314
Blockly.Ruby.operator_subtract = function (block) {
14-
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', Blockly.Ruby.ORDER_NONE) || '0';
15-
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', Blockly.Ruby.ORDER_NONE) || '0';
16-
return [`${num1} - ${num2}`, Blockly.Ruby.ORDER_ATOMIC];
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';
18+
return [`${num1} - ${num2}`, Blockly.Ruby.ORDER_ADDITIVE];
1719
};
1820

1921
Blockly.Ruby.operator_multiply = function (block) {
20-
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', Blockly.Ruby.ORDER_NONE) || '0';
21-
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', Blockly.Ruby.ORDER_NONE) || '0';
22-
return [`${num1} * ${num2}`, Blockly.Ruby.ORDER_ATOMIC];
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';
25+
return [`${num1} * ${num2}`, order];
2326
};
2427

2528
Blockly.Ruby.operator_divide = function (block) {
26-
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', Blockly.Ruby.ORDER_NONE) || '0';
27-
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', Blockly.Ruby.ORDER_NONE) || '1';
28-
return [`${num1} / ${num2}`, Blockly.Ruby.ORDER_ATOMIC];
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';
32+
return [`${num1} / ${num2}`, order];
2933
};
3034

3135
Blockly.Ruby.operator_random = function (block) {
32-
const fromNum = Blockly.Ruby.valueToCode(block, 'FROM', Blockly.Ruby.ORDER_NONE) || '0';
33-
const toNum = Blockly.Ruby.valueToCode(block, 'TO', Blockly.Ruby.ORDER_NONE) || '0';
34-
return [`rand(${fromNum}..${toNum})`, Blockly.Ruby.ORDER_ATOMIC];
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';
38+
return [`rand(${fromNum}..${toNum})`, Blockly.Ruby.ORDER_FUNCTION_CALL];
3539
};
3640

3741
Blockly.Ruby.operator_gt = function (block) {
38-
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', Blockly.Ruby.ORDER_NONE) || '0';
39-
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', Blockly.Ruby.ORDER_NONE) || '0';
40-
return [`Cast.compare(${operand1}, ${operand2}) > 0`, Blockly.Ruby.ORDER_ATOMIC];
42+
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];
4146
};
4247

4348
Blockly.Ruby.operator_lt = function (block) {
44-
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', Blockly.Ruby.ORDER_NONE) || '0';
45-
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', Blockly.Ruby.ORDER_NONE) || '0';
46-
return [`Cast.compare(${operand1}, ${operand2}) < 0`, Blockly.Ruby.ORDER_ATOMIC];
49+
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];
4753
};
4854

4955
Blockly.Ruby.operator_equals = function (block) {
50-
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', Blockly.Ruby.ORDER_NONE) || '0';
51-
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', Blockly.Ruby.ORDER_NONE) || '0';
52-
return [`Cast.compare(${operand1}, ${operand2}) == 0`, Blockly.Ruby.ORDER_ATOMIC];
56+
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];
5360
};
5461

5562
Blockly.Ruby.operator_and = function (block) {
56-
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', Blockly.Ruby.ORDER_NONE) || 'false';
57-
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', Blockly.Ruby.ORDER_NONE) || 'false';
58-
return [`${operand1} && ${operand2}`, Blockly.Ruby.ORDER_ATOMIC];
63+
const order = Blockly.Ruby.ORDER_LOGICAL_AND;
64+
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || 'false';
65+
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || 'false';
66+
return [`${operand1} && ${operand2}`, order];
5967
};
6068

6169
Blockly.Ruby.operator_or = function (block) {
62-
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', Blockly.Ruby.ORDER_NONE) || 'false';
63-
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', Blockly.Ruby.ORDER_NONE) || 'false';
64-
return [`${operand1} || ${operand2}`, Blockly.Ruby.ORDER_ATOMIC];
70+
const order = Blockly.Ruby.ORDER_LOGICAL_OR;
71+
const operand1 = Blockly.Ruby.valueToCode(block, 'OPERAND1', order) || 'false';
72+
const operand2 = Blockly.Ruby.valueToCode(block, 'OPERAND2', order) || 'false';
73+
return [`${operand1} || ${operand2}`, order];
6574
};
6675

6776
Blockly.Ruby.operator_not = function (block) {
68-
const operand = Blockly.Ruby.valueToCode(block, 'OPERAND', Blockly.Ruby.ORDER_NONE) || 'false';
69-
return [`!${operand}`, Blockly.Ruby.ORDER_ATOMIC];
77+
const order = Blockly.Ruby.ORDER_UNARY_SIGN;
78+
const operand = Blockly.Ruby.valueToCode(block, 'OPERAND', order) || 'false';
79+
return [`!${operand}`, order];
7080
};
7181

7282
Blockly.Ruby.operator_join = function (block) {
73-
const rightStr = Blockly.Ruby.valueToCode(block, 'STRING1', Blockly.Ruby.ORDER_NONE) || Blockly.Ruby.quote_('');
74-
const leftStr = Blockly.Ruby.valueToCode(block, 'STRING2', Blockly.Ruby.ORDER_NONE) || Blockly.Ruby.quote_('');
75-
return [`${rightStr} + ${leftStr}`, Blockly.Ruby.ORDER_ATOMIC];
83+
const order = Blockly.Ruby.ORDER_ADDITIVE;
84+
const rightStr = Blockly.Ruby.valueToCode(block, 'STRING1', order) || Blockly.Ruby.quote_('');
85+
const leftStr = Blockly.Ruby.valueToCode(block, 'STRING2', order) || Blockly.Ruby.quote_('');
86+
return [`${rightStr} + ${leftStr}`, order];
7687
};
7788

7889
Blockly.Ruby.operator_letter_of = function (block) {
79-
const str = Blockly.Ruby.valueToCode(block, 'STRING', Blockly.Ruby.ORDER_NONE) || Blockly.Ruby.quote_('');
80-
const letter = Blockly.Ruby.valueToCode(block, 'LETTER', Blockly.Ruby.ORDER_NONE) || Blockly.Ruby.quote_('');
81-
return [`${str}[${letter}]`, Blockly.Ruby.ORDER_ATOMIC];
90+
const order = Blockly.Ruby.ORDER_FUNCTION_CALL;
91+
const str = Blockly.Ruby.valueToCode(block, 'STRING', order) || Blockly.Ruby.quote_('');
92+
const letter = Blockly.Ruby.valueToCode(block, 'LETTER', Blockly.Ruby.ORDER_INDEX) || '0';
93+
return [`${str}[${letter}]`, order];
8294
};
8395

8496
Blockly.Ruby.operator_length = function (block) {
85-
const str = Blockly.Ruby.valueToCode(block, 'STRING', Blockly.Ruby.ORDER_NONE) || Blockly.Ruby.quote_('');
86-
return [`${str}.length`, Blockly.Ruby.ORDER_ATOMIC];
97+
const order = Blockly.Ruby.ORDER_FUNCTION_CALL;
98+
const str = Blockly.Ruby.valueToCode(block, 'STRING', order) || Blockly.Ruby.quote_('');
99+
return [`${str}.length`, order];
87100
};
88101

89102
Blockly.Ruby.operator_contains = function (block) {
@@ -93,50 +106,53 @@ export default function (Blockly) {
93106
};
94107

95108
Blockly.Ruby.operator_mod = function (block) {
96-
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', Blockly.Ruby.ORDER_NONE) || '0';
97-
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', Blockly.Ruby.ORDER_NONE) || '0';
98-
return [`${num1} % ${num2}`, Blockly.Ruby.ORDER_ATOMIC];
109+
const order = Blockly.Ruby.ORDER_MULTIPLICATIVE;
110+
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || '0';
111+
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || '0';
112+
return [`${num1} % ${num2}`, order];
99113
};
100114

101115
Blockly.Ruby.operator_round = function (block) {
102-
const num = Blockly.Ruby.valueToCode(block, 'NUM', Blockly.Ruby.ORDER_NONE) || '0';
103-
return [`${num}.round`, Blockly.Ruby.ORDER_ATOMIC];
116+
const order = Blockly.Ruby.ORDER_FUNCTION_CALL;
117+
const num = Blockly.Ruby.valueToCode(block, 'NUM', order) || '0';
118+
return [`${num}.round`, order];
104119
};
105120

106121
Blockly.Ruby.operator_mathop = function (block) {
107-
const num = Blockly.Ruby.valueToCode(block, 'NUM', Blockly.Ruby.ORDER_NONE) || Blockly.Ruby.quote_('');
122+
const order = Blockly.Ruby.ORDER_FUNCTION_CALL;
123+
const num = Blockly.Ruby.valueToCode(block, 'NUM', Blockly.Ruby.ORDER_NONE) || '0';
108124
const operator = block.getFieldValue('OPERATOR') || null;
109125
switch (operator) {
110126
case 'abs':
111-
return [`${num}.abs`, Blockly.Ruby.ORDER_ATOMIC];
127+
return [`${num}.abs`, order];
112128
case 'floor':
113-
return [`${num}.floor`, Blockly.Ruby.ORDER_ATOMIC];
129+
return [`${num}.floor`, order];
114130
case 'ceiling':
115-
return [`${num}.ceil`, Blockly.Ruby.ORDER_ATOMIC];
131+
return [`${num}.ceil`, order];
116132
case 'sqrt':
117-
return [`Math.sqrt(${num})`, Blockly.Ruby.ORDER_ATOMIC];
133+
return [`Math.sqrt(${num})`, order];
118134
case 'sin':
119-
return [`Math.sin(${num})`, Blockly.Ruby.ORDER_ATOMIC];
135+
return [`Math.sin(${num})`, order];
120136
case 'cos':
121-
return [`Math.cos(${num})`, Blockly.Ruby.ORDER_ATOMIC];
137+
return [`Math.cos(${num})`, order];
122138
case 'tan':
123-
return [`Math.tan(${num})`, Blockly.Ruby.ORDER_ATOMIC];
139+
return [`Math.tan(${num})`, order];
124140
case 'asin':
125-
return [`Math.asin(${num})`, Blockly.Ruby.ORDER_ATOMIC];
141+
return [`Math.asin(${num})`, order];
126142
case 'acos':
127-
return [`Math.acos(${num})`, Blockly.Ruby.ORDER_ATOMIC];
143+
return [`Math.acos(${num})`, order];
128144
case 'atan':
129-
return [`Math.atan(${num})`, Blockly.Ruby.ORDER_ATOMIC];
145+
return [`Math.atan(${num})`, order];
130146
case 'ln':
131-
return [`Math.log(${num})`, Blockly.Ruby.ORDER_ATOMIC];
147+
return [`Math.log(${num})`, order];
132148
case 'log':
133-
return [`Math.log10(${num})`, Blockly.Ruby.ORDER_ATOMIC];
149+
return [`Math.log10(${num})`, order];
134150
case 'e ^':
135-
return [`Math::E**${num}`, Blockly.Ruby.ORDER_ATOMIC];
151+
return [`Math::E ** ${num}`, order];
136152
case '10 ^':
137-
return [`10**${num}`, Blockly.Ruby.ORDER_ATOMIC];
153+
return [`10 ** ${num}`, order];
138154
default:
139-
return [null, Blockly.Ruby.ORDER_ATOMIC];
155+
return [null, order];
140156
}
141157
};
142158

0 commit comments

Comments
 (0)