procedures.js
3.76 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
/**
* Visual Blocks Language
*
* Copyright 2020 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.Arduino.procedures');
goog.require('Blockly.Arduino');
Blockly.Arduino['procedures_definition'] = function(block) {
var func = Blockly.Arduino.statementToCode(block, 'custom_block');
// Delet first indent.
func = func.slice(2);
var code = func + ' {\n';
code = Blockly.Arduino.scrub_(block, code);
code += '}\n';
Blockly.Arduino.customFunctions_[func] = code;
return null;
};
Blockly.Arduino['procedures_call'] = function(block) {
// Generators can not automatic handle indefinite parameters. We should get
// block.inputList and handle
var funcName = block.getProcCode();
funcName = funcName.replace(/ /g, '_');
funcName = funcName.replace(/%n/g, 'N');
funcName = funcName.replace(/%s/g, 'S');
funcName = funcName.replace(/%b/g, 'B');
funcName = Blockly.Arduino.variableDB_.getName(funcName, Blockly.Procedures.NAME_TYPE);
var argCode = [];
for (var x = 0; x < block.inputList.length; x++) {
if (block.inputList[x].type == Blockly.INPUT_VALUE) {
if (block.inputList[x].connection.targetBlock()) {
var targetBlock = block.inputList[x].connection.targetBlock();
var targetCode = Blockly.Arduino.blockToCode(targetBlock);
argCode.push(targetCode[0]);
}
// If empty mean's it's a boolean
else {
argCode.push('false');
}
}
}
var code = funcName + '(' + argCode.join(', ') + ');\n';
return code;
};
Blockly.Arduino['procedures_prototype'] = function(block) {
var funcName = block.getProcCode();
var argName = block.displayNames_;
var argCode = [];
funcName = funcName.replace(/ /g, '_');
for (var i = 0; i < argName.length; i++) {
var ch = funcName.charAt(funcName.search(/%[nsb]/g) + 1);
var safeArgName = Blockly.Arduino.variableDB_.getName(argName[i], Blockly.Procedures.NAME_TYPE);
Blockly.Arduino.customFunctionsArgName_[argName[i]] = safeArgName;
if (ch === 'n') {
funcName = funcName.replace('%n', 'N');
argCode.push('float ' + safeArgName);
}
else if (ch === 's') {
funcName = funcName.replace('%s', 'S');
argCode.push('String ' + safeArgName);
}
else {
funcName = funcName.replace('%b', 'B');
argCode.push('boolean ' + safeArgName);
}
}
funcName = Blockly.Arduino.variableDB_.getName(funcName, Blockly.Procedures.NAME_TYPE);
var code = 'void ' + funcName + '(' + argCode.join(', ') + ')';
return code;
};
Blockly.Arduino['argument_reporter_boolean'] = function(block) {
var argName = block.getFieldValue('VALUE');
var safeArgName = Blockly.Arduino.customFunctionsArgName_[argName];
return [safeArgName, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino['argument_reporter_number'] = function(block) {
var argName = block.getFieldValue('VALUE');
var safeArgName = Blockly.Arduino.customFunctionsArgName_[argName];
return [safeArgName, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino['argument_reporter_string'] = function(block) {
var argName = block.getFieldValue('VALUE');
var safeArgName = Blockly.Arduino.customFunctionsArgName_[argName];
return [safeArgName, Blockly.Arduino.ORDER_ATOMIC];
};