control.js
3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Visual Blocks Language
*
* Copyright 2021 openblock.cc.
* https://github.com/openblockcc/openblock-blocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
goog.provide('Blockly.Python.control');
goog.require('Blockly.Python');
Blockly.Python['control_wait'] = function(block) {
var arg0 = Blockly.Python.valueToCode(block, 'DURATION',
Blockly.Python.ORDER_FUNCTION_CALL);
var code = "sleep(" + arg0 + " * 1000" + ")\n";
return code;
};
Blockly.Python['control_repeat'] = function(block) {
var repeats = Blockly.Python.valueToCode(block, 'TIMES',
Blockly.Python.ORDER_FUNCTION_CALL);
var branch = Blockly.Python.statementToCode(block, 'SUBSTACK');
branch = Blockly.Python.addLoopTrap(branch, block.id);
var code = "for count in range(" + repeats + "):\n";
if (branch) {
code += branch;
} else {
code += Blockly.Python.INDENT + "pass\n";
}
return code;
};
Blockly.Python['control_forever'] = function(block) {
var branch = Blockly.Python.statementToCode(block, 'SUBSTACK');
branch = Blockly.Python.addLoopTrap(branch, block.id);
var code = "while True:\n";
code += branch;
if (block.getRootBlock().type === 'event_whenmicrobitbegin') {
Blockly.Python.firstLoop = false;
code += Blockly.Python.INDENT + "repeat()\n";
}
return code;
};
Blockly.Python['control_if'] = function(block) {
var argument = Blockly.Python.valueToCode(block, 'CONDITION',
Blockly.Python.ORDER_NONE) || 'False';
var branch = Blockly.Python.statementToCode(block, 'SUBSTACK');
branch = Blockly.Python.addLoopTrap(branch, block.id);
var code = "if " + argument + ":\n";
if (branch) {
code += branch;
} else {
code += Blockly.Python.INDENT + "pass\n";
}
return code;
};
Blockly.Python['control_if_else'] = function(block) {
var argument = Blockly.Python.valueToCode(block, 'CONDITION',
Blockly.Python.ORDER_NONE) || 'False';
var branch = Blockly.Python.statementToCode(block, 'SUBSTACK');
branch = Blockly.Python.addLoopTrap(branch, block.id);
var branch2 = Blockly.Python.statementToCode(block, 'SUBSTACK2');
branch2 = Blockly.Python.addLoopTrap(branch2, block.id);
var code = "if " + argument + ":\n";
if (branch) {
code += branch;
} else {
code += Blockly.Python.INDENT + "pass\n";
}
code += "else:\n";
if (branch2) {
code += branch2;
} else {
code += Blockly.Python.INDENT + "pass\n";
}
return code;
};
Blockly.Python['control_wait_until'] = function(block) {
var argument = Blockly.Python.valueToCode(block, 'CONDITION',
Blockly.Python.ORDER_UNARY_POSTFIX) || 'False';
var code = "while not " + argument + ":\n";
if (block.getRootBlock().type === 'event_whenmicrobitbegin') {
code += Blockly.Python.INDENT + "repeat()\n";
}
return code;
};
Blockly.Python['control_repeat_until'] = function(block) {
var argument = Blockly.Python.valueToCode(block, 'CONDITION',
Blockly.Python.ORDER_UNARY_POSTFIX) || 'False';
var branch = Blockly.Python.statementToCode(block, 'SUBSTACK');
branch = Blockly.Python.addLoopTrap(branch, block.id);
var code = "while not " + argument + ":\n";
code += branch;
if (block.getRootBlock().type === 'event_whenmicrobitbegin') {
code += Blockly.Python.INDENT + "repeat()\n";
}
return code;
};