-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBlocklyPython.js
More file actions
67 lines (59 loc) · 2.52 KB
/
BlocklyPython.js
File metadata and controls
67 lines (59 loc) · 2.52 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
/**
* Set up python code generation.
*/
'use strict';
define('BlocklyPython', [], function() {
function update(Blockly) {
Blockly.Python['if'] = Blockly.Python['controls_if'];
Blockly.Python['ifelse'] = Blockly.Python['controls_if'];
Blockly.Python['while'] = Blockly.Python['controls_whileUntil'];
Blockly.Python['for'] = Blockly.Python['controls_repeat_ext'];
Blockly.Python['funccall'] = Blockly.Python['procedures_callnoreturn'];
Blockly.Python['funcdef'] = function(block) {
var ret = Blockly.Python['procedures_defnoreturn'](block);
// Do not create global variables;
Blockly.Python.definitions_.variables = '';
var funcName = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
var code = Blockly.Python.definitions_[funcName]
var lines = code.split('\n');
lines = lines.filter(function(line) {
var isGlobalDef = Boolean(line.match(/^\s+global/));
return !isGlobalDef
})
Blockly.Python.definitions_[funcName] = lines.join('\n')
return ret
};
var originalVariablesSet = Blockly.Python['variables_set'];
Blockly.Python['variables_set'] = function(block) {
var ret = originalVariablesSet(block);
Blockly.Python.definitions_.variables = '';
return ret
};
Blockly.Python['command_'] = function(block) {
// Call a procedure with no return value.
var funcName = block.type;
var args = [];
for (var x = 0, input; input = block.getInput('ARG' + x); x++) {
args[x] = Blockly.Python.valueToCode(block, 'ARG' + x, Blockly.Python.ORDER_NONE) || 'None';
}
var code = funcName + '(' + args.join(', ') + ')\n';
return code;
};
Blockly.Python['conditions'] = function(block) {
// Call a procedure with no return value.
var funcName = block.getFieldValue('ARG0');
var args = [];
for (var x = 0, arg; arg = block.getFieldValue('ARG' + (x + 1)); x++) {
if (!checkNumber(arg))
arg = '"' + arg + '"';
args[x] = arg;
}
var code = funcName + '(' + args.join(', ') + ')';
var order = Blockly.Python.ORDER_ATOMIC;
return [code, order];
};
}
return {
update: update
}
})