forked from google/blockly
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
rust.js
338 lines (316 loc) · 11.7 KB
/
rust.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* @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 Helper functions for generating Rust for blocks.
* @author luppy@appkaki.com (Lee Lup Yuen)
*/
'use strict';
goog.provide('Blockly.Rust');
goog.require('Blockly.Generator');
/**
* Rust code generator.
* @type {!Blockly.Generator}
*/
Blockly.Rust = new Blockly.Generator('Rust');
// Indent with 4 spaces instead of default 2
Blockly.Rust.INDENT = ' ';
/** TODO
* List of illegal variable names.
* This is not intended to be a security feature. Blockly is 100% client-side,
* so bypassing this list is trivial. This is intended to prevent users from
* accidentally clobbering a built-in object or function.
* @private
*/
Blockly.Rust.addReservedWords(
// https://www.dartlang.org/docs/spec/latest/dart-language-specification.pdf
// Section 16.1.1
'assert,break,case,catch,class,const,continue,default,do,else,enum,' +
'extends,false,final,finally,for,if,in,is,new,null,rethrow,return,super,' +
'switch,this,throw,true,try,var,void,while,with,' +
// https://api.dartlang.org/dart_core.html
'print,identityHashCode,identical,BidirectionalIterator,Comparable,' +
'double,Function,int,Invocation,Iterable,Iterator,List,Map,Match,num,' +
'Pattern,RegExp,Set,StackTrace,String,StringSink,Type,bool,DateTime,' +
'Deprecated,Duration,Expando,Null,Object,RuneIterator,Runes,Stopwatch,' +
'StringBuffer,Symbol,Uri,Comparator,AbstractClassInstantiationError,' +
'ArgumentError,AssertionError,CastError,ConcurrentModificationError,' +
'CyclicInitializationError,Error,Exception,FallThroughError,' +
'FormatException,IntegerDivisionByZeroException,NoSuchMethodError,' +
'NullThrownError,OutOfMemoryError,RangeError,StackOverflowError,' +
'StateError,TypeError,UnimplementedError,UnsupportedError'
);
/** TODO
* Order of operation ENUMs.
* https://www.dartlang.org/docs/dart-up-and-running/ch02.html#operator_table
*/
Blockly.Rust.ORDER_ATOMIC = 0; // 0 "" ...
Blockly.Rust.ORDER_UNARY_POSTFIX = 1; // expr++ expr-- () [] . ?.
Blockly.Rust.ORDER_UNARY_PREFIX = 2; // -expr !expr ~expr ++expr --expr
Blockly.Rust.ORDER_MULTIPLICATIVE = 3; // * / % ~/
Blockly.Rust.ORDER_ADDITIVE = 4; // + -
Blockly.Rust.ORDER_SHIFT = 5; // << >>
Blockly.Rust.ORDER_BITWISE_AND = 6; // &
Blockly.Rust.ORDER_BITWISE_XOR = 7; // ^
Blockly.Rust.ORDER_BITWISE_OR = 8; // |
Blockly.Rust.ORDER_RELATIONAL = 9; // >= > <= < as is is!
Blockly.Rust.ORDER_EQUALITY = 10; // == !=
Blockly.Rust.ORDER_LOGICAL_AND = 11; // &&
Blockly.Rust.ORDER_LOGICAL_OR = 12; // ||
Blockly.Rust.ORDER_IF_NULL = 13; // ??
Blockly.Rust.ORDER_CONDITIONAL = 14; // expr ? expr : expr
Blockly.Rust.ORDER_CASCADE = 15; // ..
Blockly.Rust.ORDER_ASSIGNMENT = 16; // = *= /= ~/= %= += -= <<= >>= &= ^= |=
Blockly.Rust.ORDER_NONE = 99; // (...)
/**
* Initialise the database of variable names.
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
*/
Blockly.Rust.init = function(workspace) {
// Create a dictionary of definitions to be printed before the code.
Blockly.Rust.definitions_ = Object.create(null);
// Create a dictionary mapping desired function names in definitions_
// to actual function names (to avoid collisions with user functions).
Blockly.Rust.functionNames_ = Object.create(null);
if (!Blockly.Rust.variableDB_) {
Blockly.Rust.variableDB_ =
new Blockly.Names(Blockly.Rust.RESERVED_WORDS_);
} else {
Blockly.Rust.variableDB_.reset();
}
Blockly.Rust.variableDB_.setVariableMap(workspace.getVariableMap());
var defvars = [];
// Add developer variables (not created or named by the user).
var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
for (var i = 0; i < devVarList.length; i++) {
defvars.push(Blockly.Rust.variableDB_.getName(devVarList[i],
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
}
// Add user variables, but only ones that are being used.
var variables = Blockly.Variables.allUsedVarModels(workspace);
for (var i = 0; i < variables.length; i++) {
defvars.push(Blockly.Rust.variableDB_.getName(variables[i].getId(),
Blockly.Variables.NAME_TYPE));
}
// Declare all of the variables.
if (defvars.length) {
Blockly.Rust.definitions_['variables'] = [
'/// Application State',
'#[infer_type] // Infer the missing types',
'#[derive(Clone, Data, Default)]',
'struct State {',
Blockly.Rust.prefixLines(
defvars.map(
function(varName) {
return varName + ': _,';
}
).join(',\n'),
Blockly.Rust.INDENT),
'}',
''
].join('\n');
// '// Globals: ' + defvars.join(', ') + ';';
}
};
/**
* Prepend the generated code with the variable definitions.
* @param {string} code Generated code.
* @return {string} Completed code.
*/
Blockly.Rust.finish = function(code) {
// var imports = [];
var definitions = [];
var globals = [];
var state = [];
for (var name in Blockly.Rust.definitions_) {
// Each definition is either `use x` for import or `let x` for global variables.
var def = Blockly.Rust.definitions_[name];
if (def.match(/^use\s/)) {
// imports.push(def);
} else if (def.match(/^\/\/ Globals/)) {
// Put globals at the end.
globals.push(def);
} else if (def.match(/^\/\/\/ Application State/)) {
// Application state.
state.push(def);
} else {
// Other function definitions like `fn start_sensor_listener...`
definitions.push(def);
}
}
// Clean up temporary data.
delete Blockly.Rust.definitions_;
delete Blockly.Rust.functionNames_;
Blockly.Rust.variableDB_.reset();
// var allDefs = imports.join('\n') + '\n\n' + definitions.join('\n\n');
// allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n'),
return [
rustHeader,
state, // Application State
code, // on_start()
definitions.join('\n\n'), // Custom Functions
rustTrailer, // main()
globals.join('\n'), // Globals
'',
].join('\n');
};
/**
* Naked values are top-level blocks with outputs that aren't plugged into
* anything. A trailing semicolon is needed to make this legal.
* @param {string} line Line of generated code.
* @return {string} Legal line of code.
*/
Blockly.Rust.scrubNakedValue = function(line) {
return line + ';\n';
};
/**
* Encode a string as a properly escaped Rust string, complete with quotes.
* @param {string} string Text to encode.
* @return {string} Rust string.
* @private
*/
Blockly.Rust.quote_ = function(string) {
// Can't use goog.string.quote since $ must also be escaped.
string = string.replace(/\\/g, '\\\\')
.replace(/\n/g, '\\\n')
.replace(/\$/g, '\\$')
.replace(/'/g, '\\\'');
return '"' + string + '"';
};
/**
* Common tasks for generating Rust from blocks.
* Handles comments for the specified block and any connected value blocks.
* Calls any statements following this block.
* @param {!Blockly.Block} block The current block.
* @param {string} code The Rust code created for this block.
* @param {boolean=} opt_thisOnly True to generate code for only this statement.
* @return {string} Rust code with comments and subsequent blocks added.
* @private
*/
Blockly.Rust.scrub_ = function(block, code, opt_thisOnly) {
var commentCode = '';
// Only collect comments for blocks that aren't inline.
if (!block.outputConnection || !block.outputConnection.targetConnection) {
// Collect comment for this block.
var comment = block.getCommentText();
comment = Blockly.utils.wrap(comment, Blockly.Rust.COMMENT_WRAP - 3);
if (comment) {
if (block.getProcedureDef) {
// Use documentation comment for function comments.
commentCode += Blockly.Rust.prefixLines(comment + '\n', '/// ');
} else {
commentCode += Blockly.Rust.prefixLines(comment + '\n', '// ');
}
}
// Collect comments for all value arguments.
// Don't collect comments for nested statements.
for (var i = 0; i < block.inputList.length; i++) {
if (block.inputList[i].type == Blockly.INPUT_VALUE) {
var childBlock = block.inputList[i].connection.targetBlock();
if (childBlock) {
var comment = Blockly.Rust.allNestedComments(childBlock);
if (comment) {
commentCode += Blockly.Rust.prefixLines(comment, '// ');
}
}
}
}
}
var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
var nextCode = opt_thisOnly ? '' : Blockly.Rust.blockToCode(nextBlock);
return commentCode + code + nextCode;
};
/**
* Gets a property and adjusts the value while taking into account indexing.
* @param {!Blockly.Block} block The block.
* @param {string} atId The property ID of the element to get.
* @param {number=} opt_delta Value to add.
* @param {boolean=} opt_negate Whether to negate the value.
* @param {number=} opt_order The highest order acting on this value.
* @return {string|number}
*/
Blockly.Rust.getAdjusted = function(block, atId, opt_delta, opt_negate,
opt_order) {
var delta = opt_delta || 0;
var order = opt_order || Blockly.Rust.ORDER_NONE;
if (block.workspace.options.oneBasedIndex) {
delta--;
}
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
if (delta) {
var at = Blockly.Rust.valueToCode(block, atId,
Blockly.Rust.ORDER_ADDITIVE) || defaultAtIndex;
} else if (opt_negate) {
var at = Blockly.Rust.valueToCode(block, atId,
Blockly.Rust.ORDER_UNARY_PREFIX) || defaultAtIndex;
} else {
var at = Blockly.Rust.valueToCode(block, atId, order) ||
defaultAtIndex;
}
if (Blockly.isNumber(at)) {
// If the index is a naked number, adjust it right now.
at = parseInt(at, 10) + delta;
if (opt_negate) {
at = -at;
}
} else {
// If the index is dynamic, adjust it in code.
if (delta > 0) {
at = at + ' + ' + delta;
var innerOrder = Blockly.Rust.ORDER_ADDITIVE;
} else if (delta < 0) {
at = at + ' - ' + -delta;
var innerOrder = Blockly.Rust.ORDER_ADDITIVE;
}
if (opt_negate) {
if (delta) {
at = '-(' + at + ')';
} else {
at = '-' + at;
}
var innerOrder = Blockly.Rust.ORDER_UNARY_PREFIX;
}
innerOrder = Math.floor(innerOrder);
order = Math.floor(order);
if (innerOrder && order >= innerOrder) {
at = '(' + at + ')';
}
}
return at;
};
// Header for the source file
var rustHeader = `//! This program was automatically generated by Visual Embedded Rust. Don't edit here!
extern crate macros as mynewt_macros; // Declare the Mynewt Procedural Macros library
use mynewt_macros::infer_type; // Import Mynewt procedural macros
use mynewt::{
result::*, // Import Mynewt API Result and Error types
sys::console, // Import Mynewt Console API
};
use druid::{
AppLauncher, Data, EventCtx, LocalizedString, Widget, WindowDesc,
widget::{
Align, Button, Column, Label, Padding,
},
argvalue::ArgValue,
env::Env,
};
`;
// Trailer for the source file
var rustTrailer = `
`;