Skip to content

Commit a311080

Browse files
authored
Merge pull request #68 from t-kazu/feature/rubygenerator-for-operator
Operatorの命令ブロックをRubyに変換できるようにしました
2 parents e7ebf8a + 3b43e06 commit a311080

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

src/lib/ruby-generator/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import LooksBlocks from './looks.js';
88
import SoundBlocks from './sound.js';
99
import EventBlocks from './event.js';
1010
import ControlBlocks from './control.js';
11+
import OperatorsBlocks from './operators.js';
1112

1213
/**
1314
* Define Ruby
@@ -390,6 +391,7 @@ export default function (Blockly) {
390391
Blockly = SoundBlocks(Blockly);
391392
Blockly = EventBlocks(Blockly);
392393
Blockly = ControlBlocks(Blockly);
394+
Blockly = OperatorsBlocks(Blockly);
393395

394396
return Blockly;
395397
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/**
2+
* Define Ruby with Operators Blocks
3+
* @param {Blockly} Blockly The ScratchBlocks
4+
* @return {Blockly} Blockly defined Ruby generator.
5+
*/
6+
export default function (Blockly) {
7+
Blockly.Ruby.operator_add = function (block) {
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];
12+
};
13+
14+
Blockly.Ruby.operator_subtract = function (block) {
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];
19+
};
20+
21+
Blockly.Ruby.operator_multiply = function (block) {
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];
26+
};
27+
28+
Blockly.Ruby.operator_divide = function (block) {
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];
33+
};
34+
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';
38+
return [`rand(${fromNum}..${toNum})`, Blockly.Ruby.ORDER_FUNCTION_CALL];
39+
};
40+
41+
Blockly.Ruby.operator_gt = function (block) {
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];
46+
};
47+
48+
Blockly.Ruby.operator_lt = function (block) {
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];
53+
};
54+
55+
Blockly.Ruby.operator_equals = function (block) {
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];
60+
};
61+
62+
Blockly.Ruby.operator_and = function (block) {
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];
67+
};
68+
69+
Blockly.Ruby.operator_or = function (block) {
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];
74+
};
75+
76+
Blockly.Ruby.operator_not = function (block) {
77+
const order = Blockly.Ruby.ORDER_UNARY_SIGN;
78+
const operand = Blockly.Ruby.valueToCode(block, 'OPERAND', order) || 'false';
79+
return [`!${operand}`, order];
80+
};
81+
82+
Blockly.Ruby.operator_join = function (block) {
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];
87+
};
88+
89+
Blockly.Ruby.operator_letter_of = function (block) {
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];
94+
};
95+
96+
Blockly.Ruby.operator_length = function (block) {
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];
100+
};
101+
102+
Blockly.Ruby.operator_contains = function (block) {
103+
const str1 = Blockly.Ruby.valueToCode(block, 'STRING1', Blockly.Ruby.ORDER_NONE) || Blockly.Ruby.quote_('');
104+
const str2 = Blockly.Ruby.valueToCode(block, 'STRING2', Blockly.Ruby.ORDER_NONE) || Blockly.Ruby.quote_('');
105+
return [`${str1}.include?(${str2})`, Blockly.Ruby.ORDER_ATOMIC];
106+
};
107+
108+
Blockly.Ruby.operator_mod = function (block) {
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];
113+
};
114+
115+
Blockly.Ruby.operator_round = function (block) {
116+
const order = Blockly.Ruby.ORDER_FUNCTION_CALL;
117+
const num = Blockly.Ruby.valueToCode(block, 'NUM', order) || '0';
118+
return [`${num}.round`, order];
119+
};
120+
121+
Blockly.Ruby.operator_mathop = function (block) {
122+
const order = Blockly.Ruby.ORDER_FUNCTION_CALL;
123+
const num = Blockly.Ruby.valueToCode(block, 'NUM', Blockly.Ruby.ORDER_NONE) || '0';
124+
const operator = block.getFieldValue('OPERATOR') || null;
125+
switch (operator) {
126+
case 'abs':
127+
return [`${num}.abs`, order];
128+
case 'floor':
129+
return [`${num}.floor`, order];
130+
case 'ceiling':
131+
return [`${num}.ceil`, order];
132+
case 'sqrt':
133+
return [`Math.sqrt(${num})`, order];
134+
case 'sin':
135+
return [`Math.sin(${num})`, order];
136+
case 'cos':
137+
return [`Math.cos(${num})`, order];
138+
case 'tan':
139+
return [`Math.tan(${num})`, order];
140+
case 'asin':
141+
return [`Math.asin(${num})`, order];
142+
case 'acos':
143+
return [`Math.acos(${num})`, order];
144+
case 'atan':
145+
return [`Math.atan(${num})`, order];
146+
case 'ln':
147+
return [`Math.log(${num})`, order];
148+
case 'log':
149+
return [`Math.log10(${num})`, order];
150+
case 'e ^':
151+
return [`Math::E ** ${num}`, order];
152+
case '10 ^':
153+
return [`10 ** ${num}`, order];
154+
default:
155+
return [null, order];
156+
}
157+
};
158+
159+
return Blockly;
160+
}

0 commit comments

Comments
 (0)