Skip to content

Commit 99eccbe

Browse files
committed
feat: stop self.when of microbit extension block
1 parent 72b9454 commit 99eccbe

File tree

6 files changed

+338
-156
lines changed

6 files changed

+338
-156
lines changed

src/lib/ruby-generator/microbit.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default function (Generator) {
77
Generator.microbit_whenButtonPressed = function (block) {
88
block.isStatement = true;
99
const btn = Generator.valueToCode(block, 'BTN', Generator.ORDER_NONE) || null;
10-
return `${Generator.spriteName()}.when(:microbit_button_pressed, ${btn}) do\n`;
10+
return `microbit.when_button_pressed(${btn}) do\n`;
1111
};
1212

1313
Generator.microbit_isButtonPressed = function (block) {
@@ -18,7 +18,7 @@ export default function (Generator) {
1818
Generator.microbit_whenGesture = function (block) {
1919
block.isStatement = true;
2020
const gesture = Generator.valueToCode(block, 'GESTURE', Generator.ORDER_NONE) || null;
21-
return `${Generator.spriteName()}.when(:microbit_gesture, ${gesture}) do\n`;
21+
return `microbit.when(${gesture}) do\n`;
2222
};
2323

2424
Generator.microbit_displaySymbol = function (block) {
@@ -39,7 +39,7 @@ export default function (Generator) {
3939
Generator.microbit_whenTilted = function (block) {
4040
block.isStatement = true;
4141
const direction = Generator.valueToCode(block, 'DIRECTION', Generator.ORDER_NONE) || null;
42-
return `${Generator.spriteName()}.when(:microbit_tilted, ${direction}) do\n`;
42+
return `microbit.when_tilted(${direction}) do\n`;
4343
};
4444

4545
Generator.microbit_isTilted = function (block) {
@@ -55,7 +55,7 @@ export default function (Generator) {
5555
Generator.microbit_whenPinConnected = function (block) {
5656
block.isStatement = true;
5757
const pin = Generator.valueToCode(block, 'PIN', Generator.ORDER_NONE) || null;
58-
return `${Generator.spriteName()}.when(:microbit_pin_connected, ${pin}) do\n`;
58+
return `microbit.when_pin_connected(${pin}) do\n`;
5959
};
6060

6161
Generator.microbit_menu_buttons = function (block) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ const ControlConverter = {
132132
case 'start_as_a_clone':
133133
return converter.callMethod(
134134
params.receiver, 'when_start_as_a_clone', params.args.slice(1),
135-
params.rubyBlockArgs, params.rubyBlock, params.node);
135+
params.rubyBlockArgs, params.rubyBlock, params.node
136+
);
136137
}
137138

138139
return null;

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ const EventConverter = {
113113
};
114114
converter.registerCallMethod(
115115
'self', 'broadcast', 1,
116-
params => createBroadcastBlockFunc(params, 'event_broadcast'));
116+
params => createBroadcastBlockFunc(params, 'event_broadcast')
117+
);
117118
converter.registerCallMethod(
118119
'self', 'broadcast_and_wait', 1,
119-
params => createBroadcastBlockFunc(params, 'event_broadcastandwait'));
120+
params => createBroadcastBlockFunc(params, 'event_broadcastandwait')
121+
);
120122

121123
// backward compatibility
122124
converter.registerCallMethodWithBlock('self', 'when', 1, 0, params => {
@@ -128,11 +130,13 @@ const EventConverter = {
128130
case 'flag_clicked':
129131
return converter.callMethod(
130132
params.receiver, 'when_flag_clicked', params.args.slice(1),
131-
params.rubyBlockArgs, params.rubyBlock, params.node);
133+
params.rubyBlockArgs, params.rubyBlock, params.node
134+
);
132135
case 'clicked':
133136
return converter.callMethod(
134137
params.receiver, 'when_clicked', params.args.slice(1),
135-
params.rubyBlockArgs, params.rubyBlock, params.node);
138+
params.rubyBlockArgs, params.rubyBlock, params.node
139+
);
136140
}
137141

138142
return null;
@@ -148,15 +152,18 @@ const EventConverter = {
148152
case 'key_pressed':
149153
return converter.callMethod(
150154
params.receiver, 'when_key_pressed', params.args.slice(1),
151-
params.rubyBlockArgs, params.rubyBlock, params.node);
155+
params.rubyBlockArgs, params.rubyBlock, params.node
156+
);
152157
case 'backdrop_switches':
153158
return converter.callMethod(
154159
params.receiver, 'when_backdrop_switches', params.args.slice(1),
155-
params.rubyBlockArgs, params.rubyBlock, params.node);
160+
params.rubyBlockArgs, params.rubyBlock, params.node
161+
);
156162
case 'receive':
157163
return converter.callMethod(
158164
params.receiver, 'when_receive', params.args.slice(1),
159-
params.rubyBlockArgs, params.rubyBlock, params.node);
165+
params.rubyBlockArgs, params.rubyBlock, params.node
166+
);
160167
}
161168

162169
return null;
@@ -172,7 +179,8 @@ const EventConverter = {
172179
case 'greater_than':
173180
return converter.callMethod(
174181
params.receiver, 'when_greater_than', params.args.slice(1),
175-
params.rubyBlockArgs, params.rubyBlock, params.node);
182+
params.rubyBlockArgs, params.rubyBlock, params.node
183+
);
176184
}
177185

178186
return null;

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ class RubyToBlocksConverter {
8383
this._converters = [
8484
MusicConverter,
8585
PenConverter,
86-
MicroBitConverter,
8786
MicroBitMoreConverter,
8887
EV3Converter,
8988
Wedo2Converter,
@@ -108,7 +107,8 @@ class RubyToBlocksConverter {
108107

109108
[
110109
EventConverter,
111-
ControlConverter
110+
ControlConverter,
111+
MicroBitConverter
112112
].forEach(x => x.register(this));
113113
}
114114

@@ -486,6 +486,10 @@ class RubyToBlocksConverter {
486486
return _.isString(value) || (value && value.type === 'str');
487487
}
488488

489+
isNumber (value) {
490+
return this._isNumber(value);
491+
}
492+
489493
_isNumber (value) {
490494
return _.isNumber(value) || (value && (value.type === 'int' || value.type === 'float'));
491495
}
@@ -655,6 +659,10 @@ class RubyToBlocksConverter {
655659
return value;
656660
}
657661

662+
createRubyExpressionBlock (expression, node) {
663+
return this._createRubyExpressionBlock(expression, node);
664+
}
665+
658666
_createRubyExpressionBlock (expression, node) {
659667
const block = this._createBlock('ruby_expression', 'value_boolean');
660668
block.node = node;
@@ -734,6 +742,10 @@ class RubyToBlocksConverter {
734742
return value;
735743
}
736744

745+
addTextInput (block, name, inputValue, shadowValue) {
746+
return this._addTextInput(block, name, inputValue, shadowValue);
747+
}
748+
737749
_addTextInput (block, name, inputValue, shadowValue) {
738750
let shadowBlock;
739751
if (!this._isString(inputValue)) {
@@ -742,6 +754,10 @@ class RubyToBlocksConverter {
742754
this._addInput(block, name, this._createTextBlock(inputValue), shadowBlock);
743755
}
744756

757+
addFieldInput (block, name, opcode, fieldName, inputValue, shadowValue) {
758+
return this._addFieldInput(block, name, opcode, fieldName, inputValue, shadowValue);
759+
}
760+
745761
_addFieldInput (block, name, opcode, fieldName, inputValue, shadowValue) {
746762
let shadowBlock;
747763
if (!this._isString(inputValue)) {
@@ -944,6 +960,10 @@ class RubyToBlocksConverter {
944960
return block;
945961
}
946962

963+
changeRubyExpressionBlock (block, opcode, blockType) {
964+
return this._changeRubyExpressionBlock(block, opcode, blockType);
965+
}
966+
947967
_changeRubyExpressionBlock (block, opcode, blockType) {
948968
this._changeBlock(block, opcode, blockType);
949969

0 commit comments

Comments
 (0)