Skip to content

Commit 01adfbd

Browse files
authored
Merge pull request #165 from smalruby/issues/134_sensing1
convert Ruby to Sensing blocks. (refs #134)
2 parents fa1850d + b222ebf commit 01adfbd

File tree

11 files changed

+859
-117
lines changed

11 files changed

+859
-117
lines changed

src/lib/ruby-generator/sensing.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default function (Generator) {
4141

4242
Generator.sensing_askandwait = function (block) {
4343
const question = Generator.valueToCode(block, 'QUESTION', Generator.ORDER_NONE) || null;
44-
return `ask_and_wait(${question})\n`;
44+
return `ask(${question})\n`;
4545
};
4646

4747
Generator.sensing_answer = function () {
@@ -55,7 +55,7 @@ export default function (Generator) {
5555
};
5656

5757
Generator.sensing_keyoptions = function (block) {
58-
const key = Generator.quote_(Generator.getFieldValue(block, 'KEY_OPTION') || null);
58+
const key = Generator.quote_(Generator.getFieldValue(block, 'KEY_OPTION') || '');
5959
return [key, Generator.ORDER_ATOMIC];
6060
};
6161

@@ -72,8 +72,8 @@ export default function (Generator) {
7272
};
7373

7474
Generator.sensing_setdragmode = function (block) {
75-
const mode = Generator.quote_(Generator.getFieldValue(block, 'DRAG_MODE') || null);
76-
return `set_drag_mode(${mode})\n`;
75+
const mode = Generator.quote_(Generator.getFieldValue(block, 'DRAG_MODE') || '');
76+
return `self.drag_mode = ${mode}\n`;
7777
};
7878

7979
Generator.sensing_loudness = function () {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const KeyOptions = [
2+
'space',
3+
'left arrow',
4+
'right arrow',
5+
'down arrow',
6+
'up arrow',
7+
'any',
8+
'a',
9+
'b',
10+
'c',
11+
'd',
12+
'e',
13+
'f',
14+
'g',
15+
'h',
16+
'i',
17+
'j',
18+
'k',
19+
'l',
20+
'm',
21+
'n',
22+
'o',
23+
'p',
24+
'q',
25+
'r',
26+
's',
27+
't',
28+
'u',
29+
'v',
30+
'w',
31+
'x',
32+
'y',
33+
'z',
34+
'0',
35+
'1',
36+
'2',
37+
'3',
38+
'4',
39+
'5',
40+
'6',
41+
'7',
42+
'8',
43+
'9'
44+
];
45+
46+
export {
47+
KeyOptions
48+
};

src/lib/ruby-to-blocks-converter/event.js

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,7 @@
11
/* global Opal */
22
import _ from 'lodash';
33
import Variable from 'scratch-vm/src/engine/variable';
4-
5-
const KeyOptions = [
6-
'space',
7-
'left arrow',
8-
'right arrow',
9-
'down arrow',
10-
'up arrow',
11-
'any',
12-
'a',
13-
'b',
14-
'c',
15-
'd',
16-
'e',
17-
'f',
18-
'g',
19-
'h',
20-
'i',
21-
'j',
22-
'k',
23-
'l',
24-
'm',
25-
'n',
26-
'o',
27-
'p',
28-
'q',
29-
'r',
30-
's',
31-
't',
32-
'u',
33-
'v',
34-
'w',
35-
'x',
36-
'y',
37-
'z',
38-
'0',
39-
'1',
40-
'2',
41-
'3',
42-
'4',
43-
'5',
44-
'6',
45-
'7',
46-
'8',
47-
'9'
48-
];
4+
import {KeyOptions} from './constants';
495

506
const GreaterThanMenu = [
517
'LOUDNESS',

src/lib/ruby-to-blocks-converter/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ class RubyToBlocksConverter {
302302
return value && value.type === 'hash';
303303
}
304304

305+
_isConst (value) {
306+
return value && value.type === 'const';
307+
}
308+
305309
_isBlock (block) {
306310
try {
307311
return block.hasOwnProperty('opcode');
@@ -378,6 +382,9 @@ class RubyToBlocksConverter {
378382
}
379383

380384
_createFieldBlock (opcode, fieldName, value) {
385+
if (this._isBlock(value)) {
386+
return value;
387+
}
381388
return this._createBlock(opcode, 'value', {
382389
fields: {
383390
[fieldName]: {
@@ -461,6 +468,14 @@ class RubyToBlocksConverter {
461468
this._addInput(block, name, this._createTextBlock(inputValue), shadowBlock);
462469
}
463470

471+
_addFieldInput (block, name, opcode, fieldName, inputValue, shadowValue) {
472+
let shadowBlock;
473+
if (!this._isString(inputValue)) {
474+
shadowBlock = this._createFieldBlock(opcode, fieldName, shadowValue);
475+
}
476+
this._addInput(block, name, this._createFieldBlock(opcode, fieldName, inputValue), shadowBlock);
477+
}
478+
464479
_addSubstack (block, substackBlock, num = 1) {
465480
let name = 'SUBSTACK';
466481
if (num > 1) {
@@ -893,7 +908,11 @@ class RubyToBlocksConverter {
893908
_onConst (node) {
894909
this._checkNumChildren(node, 2);
895910

896-
return this._createRubyExpressionBlock(this._getSource(node));
911+
const value = {
912+
scope: this._process(node.children[0]),
913+
name: node.children[1].toString()
914+
};
915+
return new Primitive('const', value, node);
897916
}
898917

899918
_onArgs (node) {

src/lib/ruby-to-blocks-converter/motion.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
/* global Opal */
22
import _ from 'lodash';
33

4+
const RotationStyle = [
5+
'left-right',
6+
'don\'t rotate',
7+
'all around'
8+
];
9+
410
/**
511
* Motion converter
612
*/
@@ -79,12 +85,7 @@ const MotionConverter = {
7985
}
8086
break;
8187
case 'rotation_style=': {
82-
const ROTATION_STYLE = [
83-
'left-right',
84-
'don\'t rotate',
85-
'all around'
86-
];
87-
if (args.length === 1 && this._isString(args[0]) && ROTATION_STYLE.indexOf(args[0].toString()) >= 0) {
88+
if (args.length === 1 && this._isString(args[0]) && RotationStyle.indexOf(args[0].toString()) >= 0) {
8889
block = this._createBlock('motion_setrotationstyle', 'statement');
8990
this._addField(block, 'STYLE', args[0]);
9091
}

src/lib/ruby-to-blocks-converter/operators.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,8 @@ const OperatorsConverter = {
158158
operator = name;
159159
}
160160
if (args.length === 1 &&
161-
this._matchRubyExpression(receiver, /^(::)?Math$/) &&
161+
this._isConst(receiver) && receiver.toString() === '::Math' &&
162162
this._isNumberOrBlock(args[0])) {
163-
delete this._context.blocks[receiver.inputs.EXPRESSION.block];
164-
delete this._context.blocks[receiver.id];
165-
166163
block = this._createBlock('operator_mathop', 'value');
167164
this._addField(block, 'OPERATOR', operator);
168165
this._addNumberInput(block, 'NUM', 'math_number', args[0], '');
@@ -172,10 +169,8 @@ const OperatorsConverter = {
172169
case '**':
173170
if (args.length === 1 && this._isNumberOrBlock(args[0])) {
174171
let operator;
175-
if (this._matchRubyExpression(receiver, /^(::)?Math::E$/)) {
172+
if (this._isConst(receiver) && receiver.toString() === '::Math::E') {
176173
operator = 'e ^';
177-
delete this._context.blocks[receiver.inputs.EXPRESSION.block];
178-
delete this._context.blocks[receiver.id];
179174
} else if (receiver.type === 'int' && receiver.value === 10) {
180175
operator = '10 ^';
181176
}

src/lib/ruby-to-blocks-converter/primitive.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* global Opal */
2+
13
/**
24
* Primitive class for RubyToBlocksConverter
35
*/
@@ -34,6 +36,9 @@ class Primitive {
3436
}
3537

3638
toString () {
39+
if (this._type === 'const') {
40+
return `${this._value.scope === Opal.nil ? '' : this._value.scope.toString()}::${this._value.name}`;
41+
}
3742
return this._value.toString();
3843
}
3944

0 commit comments

Comments
 (0)