Skip to content

Commit fd49e4c

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

File tree

1 file changed

+75
-60
lines changed

1 file changed

+75
-60
lines changed

src/lib/ruby-generator/operators.js

Lines changed: 75 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,85 +5,97 @@
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 str = Blockly.Ruby.valueToCode(block, 'STRING', Blockly.Ruby.ORDER_FUNCTION_CALL) || Blockly.Ruby.quote_('');
91+
const letter = Blockly.Ruby.valueToCode(block, 'LETTER', Blockly.Ruby.ORDER_INDEX) || Blockly.Ruby.quote_('');
92+
return [`${str}[${letter}]`, Blockly.Ruby.ORDER_FUNCTION_CALL];
8293
};
8394

8495
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];
96+
const order = Blockly.Ruby.ORDER_FUNCTION_CALL;
97+
const str = Blockly.Ruby.valueToCode(block, 'STRING', order) || Blockly.Ruby.quote_('');
98+
return [`${str}.length`, order];
8799
};
88100

89101
Blockly.Ruby.operator_contains = function (block) {
@@ -93,50 +105,53 @@ export default function (Blockly) {
93105
};
94106

95107
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];
108+
const order = Blockly.Ruby.ORDER_MULTIPLICATIVE;
109+
const num1 = Blockly.Ruby.valueToCode(block, 'NUM1', order) || '0';
110+
const num2 = Blockly.Ruby.valueToCode(block, 'NUM2', order) || '0';
111+
return [`${num1} % ${num2}`, order];
99112
};
100113

101114
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];
115+
const order = Blockly.Ruby.ORDER_FUNCTION_CALL;
116+
const num = Blockly.Ruby.valueToCode(block, 'NUM', order) || '0';
117+
return [`${num}.round`, order];
104118
};
105119

106120
Blockly.Ruby.operator_mathop = function (block) {
121+
const order = Blockly.Ruby.ORDER_FUNCTION_CALL;
107122
const num = Blockly.Ruby.valueToCode(block, 'NUM', Blockly.Ruby.ORDER_NONE) || Blockly.Ruby.quote_('');
108123
const operator = block.getFieldValue('OPERATOR') || null;
109124
switch (operator) {
110125
case 'abs':
111-
return [`${num}.abs`, Blockly.Ruby.ORDER_ATOMIC];
126+
return [`${num}.abs`, order];
112127
case 'floor':
113-
return [`${num}.floor`, Blockly.Ruby.ORDER_ATOMIC];
128+
return [`${num}.floor`, order];
114129
case 'ceiling':
115-
return [`${num}.ceil`, Blockly.Ruby.ORDER_ATOMIC];
130+
return [`${num}.ceil`, order];
116131
case 'sqrt':
117-
return [`Math.sqrt(${num})`, Blockly.Ruby.ORDER_ATOMIC];
132+
return [`Math.sqrt(${num})`, order];
118133
case 'sin':
119-
return [`Math.sin(${num})`, Blockly.Ruby.ORDER_ATOMIC];
134+
return [`Math.sin(${num})`, order];
120135
case 'cos':
121-
return [`Math.cos(${num})`, Blockly.Ruby.ORDER_ATOMIC];
136+
return [`Math.cos(${num})`, order];
122137
case 'tan':
123-
return [`Math.tan(${num})`, Blockly.Ruby.ORDER_ATOMIC];
138+
return [`Math.tan(${num})`, order];
124139
case 'asin':
125-
return [`Math.asin(${num})`, Blockly.Ruby.ORDER_ATOMIC];
140+
return [`Math.asin(${num})`, order];
126141
case 'acos':
127-
return [`Math.acos(${num})`, Blockly.Ruby.ORDER_ATOMIC];
142+
return [`Math.acos(${num})`, order];
128143
case 'atan':
129-
return [`Math.atan(${num})`, Blockly.Ruby.ORDER_ATOMIC];
144+
return [`Math.atan(${num})`, order];
130145
case 'ln':
131-
return [`Math.log(${num})`, Blockly.Ruby.ORDER_ATOMIC];
146+
return [`Math.log(${num})`, order];
132147
case 'log':
133-
return [`Math.log10(${num})`, Blockly.Ruby.ORDER_ATOMIC];
148+
return [`Math.log10(${num})`, order];
134149
case 'e ^':
135-
return [`Math::E**${num}`, Blockly.Ruby.ORDER_ATOMIC];
150+
return [`Math::E ** ${num}`, order];
136151
case '10 ^':
137-
return [`10**${num}`, Blockly.Ruby.ORDER_ATOMIC];
152+
return [`10 ** ${num}`, order];
138153
default:
139-
return [null, Blockly.Ruby.ORDER_ATOMIC];
154+
return [null, order];
140155
}
141156
};
142157

0 commit comments

Comments
 (0)