forked from google/blockly
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
procedures.js
149 lines (139 loc) · 5.55 KB
/
procedures.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* @license
* Visual Blocks Language
*
* Copyright 2014 Google Inc.
* https://developers.google.com/blockly/
*
* 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.
*/
/**
* @fileoverview Generating Rust for procedure blocks.
* @author luppy@appkaki.com (Lee Lup Yuen)
*/
'use strict';
goog.provide('Blockly.Rust.procedures');
goog.require('Blockly.Rust');
Blockly.Rust['procedures_defreturn'] = function(block) {
// Define a procedure with a return value.
var funcName = Blockly.Rust.variableDB_.getName(block.getFieldValue('NAME'),
Blockly.Procedures.NAME_TYPE);
var branch = Blockly.Rust.statementToCode(block, 'STACK');
if (Blockly.Rust.STATEMENT_PREFIX) {
var id = block.id.replace(/\$/g, '$$$$'); // Issue 251.
branch = Blockly.Rust.prefixLines(
Blockly.Rust.STATEMENT_PREFIX.replace(/%1/g,
'\'' + id + '\''), Blockly.Rust.INDENT) + branch;
}
if (Blockly.Rust.INFINITE_LOOP_TRAP) {
branch = Blockly.Rust.INFINITE_LOOP_TRAP.replace(/%1/g,
'\'' + block.id + '\'') + branch;
}
var returnValue = Blockly.Rust.valueToCode(block, 'RETURN',
Blockly.Rust.ORDER_NONE) || '';
var returnType = returnValue ? 'dynamic' : 'void';
returnValue = Blockly.Rust.INDENT + 'Ok(' + (returnValue || '()') + ')\n';
funcName = funcName.split('__').join('::'); // TODO: Convert sensor__func to sensor::func
var args = [];
for (var i = 0; i < block.arguments_.length; i++) {
// Assemble the args and give them placeholder types: `arg1: _`
args[i] = Blockly.Rust.variableDB_.getName(block.arguments_[i],
Blockly.Variables.NAME_TYPE) + ': _';
}
var code;
if (funcName.indexOf('::') >= 0) {
// System function: Do nothing
// Previously: code = '// Import ' + funcName;
return null;
} else {
// User-defined function: Define the function
code = [
// Set the `infer_type` attribute so that the `infer_type` macro will infer the placeholder types.
'#[infer_type] // Infer the missing types\n',
'fn ', funcName,
'(',
args.join(', '),
') ',
'-> MynewtResult<',
returnType == 'void' ? '()' : returnType,
'> ',
'{\n',
Blockly.Rust.INDENT, 'console::print("', funcName, '\\n");\n',
branch,
returnValue,
'}'
].join('');
}
// console.log(['code', funcName, code]); ////
code = Blockly.Rust.scrub_(block, code);
// Add % so as not to collide with helper functions in definitions list.
Blockly.Rust.definitions_['%' + funcName] = code;
return null;
};
// Defining a procedure without a return value uses the same generator as
// a procedure with a return value.
Blockly.Rust['procedures_defnoreturn'] = Blockly.Rust['procedures_defreturn'];
Blockly.Rust['procedures_callreturn'] = function(block) {
// Call a procedure with a return value.
var funcName = Blockly.Rust.variableDB_.getName(block.getFieldValue('NAME'),
Blockly.Procedures.NAME_TYPE);
funcName = funcName.split('__').join('::'); // TODO: Convert sensor__func to sensor::func
var args = [];
for (var i = 0; i < block.arguments_.length; i++) {
args[i] = Blockly.Rust.valueToCode(block, 'ARG' + i,
Blockly.Rust.ORDER_NONE) || 'null';
// If function is `sensor::new_sensor_listener`, convert the third arg from string to function name, e.g.
// sensor::new_sensor_listener(sensor_key, sensor_type, "handle_sensor_data") becomes
// sensor::new_sensor_listener(sensor_key, sensor_type, handle_sensor_data)
// TODO: Need a better solution.
if (funcName === 'sensor::new_sensor_listener' && i === 2) {
args[i] = args[i].split('"').join('');
}
}
// Generate the function call.
var code = funcName + '(' + args.join(', ') + ') ? ';
// If function is `sensor_network::get_device_id`, return a reference: `&sensor_network::get_device_id`
// TODO: `get_device_id` should return a reference
if (funcName === 'sensor_network::get_device_id') {
code = '&' + code;
}
return [code, Blockly.Rust.ORDER_UNARY_POSTFIX];
};
Blockly.Rust['procedures_callnoreturn'] = function(block) {
// Call a procedure with no return value.
var funcName = Blockly.Rust.variableDB_.getName(block.getFieldValue('NAME'),
Blockly.Procedures.NAME_TYPE);
funcName = funcName.split('__').join('::'); // TODO: Convert sensor__func to sensor::func
var args = [];
for (var i = 0; i < block.arguments_.length; i++) {
args[i] = Blockly.Rust.valueToCode(block, 'ARG' + i,
Blockly.Rust.ORDER_NONE) || 'null';
}
var code = funcName + '(' + args.join(', ') + ') ? ;\n';
return code;
};
Blockly.Rust['procedures_ifreturn'] = function(block) {
// Conditionally return value from a procedure.
var condition = Blockly.Rust.valueToCode(block, 'CONDITION',
Blockly.Rust.ORDER_NONE) || 'false';
var code = 'if ' + condition + ' {\n';
if (block.hasReturnValue_) {
var value = Blockly.Rust.valueToCode(block, 'VALUE',
Blockly.Rust.ORDER_NONE) || 'null';
code += Blockly.Rust.INDENT + 'return ' + value + ';\n';
} else {
code += Blockly.Rust.INDENT + 'return;\n';
}
code += '}\n';
return code;
};