-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscafi_generator.js
284 lines (245 loc) · 9.84 KB
/
scafi_generator.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
const scafiGenerator = new Blockly.Generator('ScaFi');
//Operator precedence constants. See https://developers.google.com/blockly/guides/create-custom-blocks/operator-precedence
scafiGenerator.ORDER_ATOMIC = 0;
scafiGenerator.ORDER_FUNCTION_CALL = 2; //()
scafiGenerator.ORDER_MULTIPLICATION = 5.1; // *
scafiGenerator.ORDER_DIVISION = 5.2; // /
scafiGenerator.ORDER_MODULUS = 5.3; // %
scafiGenerator.ORDER_SUBTRACTION = 6.1; // -
scafiGenerator.ORDER_ADDITION = 6.2; // +
scafiGenerator.ORDER_RELATIONAL = 8; // < <= > >=
scafiGenerator.ORDER_EQUALITY = 12; // == != === !==
scafiGenerator.ORDER_LOGICAL_AND = 13; // &&
scafiGenerator.ORDER_LOGICAL_OR = 14; // ||
scafiGenerator.ORDER_ASSIGNMENT = 20; // =
scafiGenerator.ORDER_NONE = 99;
scafiGenerator.ORDER_OVERRIDES = [
// (foo())() -> foo()()
[scafiGenerator.ORDER_FUNCTION_CALL, scafiGenerator.ORDER_FUNCTION_CALL],
// a * (b * c) -> a * b * c
[scafiGenerator.ORDER_MULTIPLICATION, scafiGenerator.ORDER_MULTIPLICATION],
// a + (b + c) -> a + b + c
[scafiGenerator.ORDER_ADDITION, scafiGenerator.ORDER_ADDITION],
// a && (b && c) -> a && b && c
[scafiGenerator.ORDER_LOGICAL_AND, scafiGenerator.ORDER_LOGICAL_AND],
// a || (b || c) -> a || b || c
[scafiGenerator.ORDER_LOGICAL_OR, scafiGenerator.ORDER_LOGICAL_OR]
];
scafiGenerator['aggregate_program'] = function (block) {
const import_map = {
"distance_to" : ["StandardSensors","BlockG"],
"distance_between" : ["StandardSensors","BlockG"],
"channel" : ["StandardSensors","BlockG"],
"led_all_to" : "Actuation",
}
let importArray = [];
let import_code = "";
const workspace = block.workspace;
const allBlocks = workspace.getAllBlocks();
for(const block of allBlocks){
if(block.type in import_map){
let modules = import_map[block.type];
if(!Array.isArray(modules)){
modules = [modules];
}
for(const module of modules){
if(!importArray.includes(module)){
importArray.push(module);
}
}
}
}
if(importArray.length){
import_code = "//using "+importArray.join(", ")+"\n";
}
const otherCode = Blockly.ScaFi.blockToCode(block.getInputTargetBlock("AGGREGATE_PROGRAM_MAIN")); //Not using statementToCode to avoid first INDENT
return import_code + otherCode;
}
scafiGenerator['string'] = function (block) {
return ['"' + block.getFieldValue('STRING_VALUE') + '"', 0];
}
scafiGenerator['integer'] = function (block) {
const code = block.getFieldValue('INTEGER_VALUE').toString();
return [code, scafiGenerator.ORDER_ATOMIC];
}
scafiGenerator['double'] = function (block) {
const code = block.getFieldValue('VALUE').toString();
return [code, scafiGenerator.ORDER_ATOMIC];
}
scafiGenerator['boolean'] = function (block) {
const code = block.getFieldValue('BOOLEAN_VALUE');
return [code, scafiGenerator.ORDER_ATOMIC];
}
scafiGenerator['tuple'] = function (block) {
const values = [
Blockly.ScaFi.valueToCode(block, "VALUE_1", scafiGenerator.ORDER_ATOMIC),
Blockly.ScaFi.valueToCode(block, "VALUE_2", scafiGenerator.ORDER_ATOMIC)
];
const code = "(" + values[0] + "," + values[1] + ")"
return [code, scafiGenerator.ORDER_ATOMIC];
}
scafiGenerator['boolean_operation'] = function (block) {
const operation = block.getFieldValue("OPERATOR");
let order;
let operator;
if(operation === 'and'){
operator = "&&";
order = scafiGenerator.ORDER_LOGICAL_AND;
}else{ //or
operator = "||";
order = scafiGenerator.ORDER_LOGICAL_OR;
}
const first = Blockly.ScaFi.valueToCode(block, 'FIRST', order);
const second = Blockly.ScaFi.valueToCode(block, 'SECOND', order);
const code = first+' '+operator+' '+second;
return [code, order];
}
scafiGenerator['number_compare'] = function (block) {
const operation = block.getFieldValue("OPERATOR");
let operator;
let order;
if(operation === 'GREATER'){
operator = ' > ';
order = scafiGenerator.ORDER_RELATIONAL;
}else if(operation === 'GREATER_OR_EQUAL'){
operator = ' >= ';
order = scafiGenerator.ORDER_RELATIONAL;
}else if(operation === 'EQUAL'){
operator = ' == ';
order = scafiGenerator.ORDER_EQUALITY;
}else if(operation === 'NOT_EQUAL'){
operator = ' != ';
order = scafiGenerator.ORDER_EQUALITY;
}else if(operation === 'LESS_OR_EQUAL'){
operator = ' <= ';
order = scafiGenerator.ORDER_RELATIONAL;
}else{ //LESS
operator = ' < ';
order = scafiGenerator.ORDER_RELATIONAL;
}
const first = Blockly.ScaFi.valueToCode(block, 'FIRST', order);
const second = Blockly.ScaFi.valueToCode(block, 'SECOND', order);
const code = first+operator+second;
return [code, order];
}
scafiGenerator['number_operation'] = function (block) {
const operation = block.getFieldValue("OPERATOR");
let order;
let operator;
if(operation === 'ADDITION'){
operator = ' + ';
order = scafiGenerator.ORDER_ADDITION;
}else if(operation === 'SUBTRACTION'){
operator = ' - ';
order = scafiGenerator.ORDER_SUBTRACTION;
}else if(operation === 'MULTIPLICATION'){
operator = ' * ';
order = scafiGenerator.ORDER_MULTIPLICATION;
}else if(operation === 'DIVISION'){
operator = ' / ';
order = scafiGenerator.ORDER_DIVISION;
}else{ //MODULUS
operator = ' % ';
order = scafiGenerator.ORDER_MODULUS;
}
const first = Blockly.ScaFi.valueToCode(block, 'FIRST', order);
const second = Blockly.ScaFi.valueToCode(block, 'SECOND', order);
const code = first+operator+second;
return [code, order];
}
scafiGenerator['output'] = function (block) {
return Blockly.ScaFi.valueToCode(block, "OUTPUT_VALUE", scafiGenerator.ORDER_NONE);
}
scafiGenerator['sense'] = function (block) {
const sensor = Blockly.ScaFi.valueToCode(block, 'SENSOR_NAME', scafiGenerator.ORDER_ATOMIC);
const code = 'sense(' + sensor + ')';
return [code, scafiGenerator.ORDER_FUNCTION_CALL];
}
scafiGenerator['mux'] = function (block) {
const condition = Blockly.ScaFi.valueToCode(block, 'CONDITION', scafiGenerator.ORDER_NONE);
const firstBranch = Blockly.ScaFi.valueToCode(block, 'FIRST_BRANCH', scafiGenerator.ORDER_ATOMIC);
const secondBranch = Blockly.ScaFi.valueToCode(block, 'SECOND_BRANCH', scafiGenerator.ORDER_ATOMIC);
let code = 'mux(' + condition + '){\n';
code += scafiGenerator.prefixLines(firstBranch, scafiGenerator.INDENT)+'\n';
code += '}{\n';
code += scafiGenerator.prefixLines(secondBranch, scafiGenerator.INDENT)+'\n';
code += '}';
return [code, scafiGenerator.ORDER_ATOMIC];
}
scafiGenerator['define'] = function(block){
const defName = block.getFieldValue('NAME');
const input = block.getInput('VALUE');
const connection = input.connection;
const targetBlock = connection.targetBlock();
let type = null;
if(targetBlock){
type = targetBlock.outputConnection.getCheck();
}
let code = "def "+defName;
if(type && type.length){
code += " : "+type[0]
}
code += " = "+Blockly.ScaFi.valueToCode(block, "VALUE", scafiGenerator.ORDER_ASSIGNMENT);
return code;
}
scafiGenerator['val'] = function(block){
const defName = block.getFieldValue('NAME');
const input = block.getInput('VALUE');
const connection = input.connection;
const targetBlock = connection.targetBlock();
let type = null;
if(targetBlock){
type = targetBlock.outputConnection.getCheck();
}
let code = "val "+defName;
if(type && type.length){
code += " : "+type[0]
}
code += " = "+Blockly.ScaFi.valueToCode(block, "VALUE", scafiGenerator.ORDER_ASSIGNMENT);
return code;
}
scafiGenerator['getter'] = function(block){
return [block.getFieldValue('NAME'), scafiGenerator.ORDER_ATOMIC];
}
scafiGenerator['distance_to'] = function(block){
const code = "distanceTo("+Blockly.ScaFi.valueToCode(block, "SRC", scafiGenerator.ORDER_ATOMIC)+")";
return [code,scafiGenerator.ORDER_FUNCTION_CALL];
}
scafiGenerator['distance_between'] = function(block){
const code = "distanceBetween("+
Blockly.ScaFi.valueToCode(block, "SOURCE", scafiGenerator.ORDER_ATOMIC)+", "+
Blockly.ScaFi.valueToCode(block, "TARGET", scafiGenerator.ORDER_ATOMIC)+
")";
return [code,scafiGenerator.ORDER_FUNCTION_CALL];
}
scafiGenerator['channel'] = function(block){
const code = "channel("+
Blockly.ScaFi.valueToCode(block, "SOURCE", scafiGenerator.ORDER_ATOMIC)+", "+
Blockly.ScaFi.valueToCode(block, "TARGET", scafiGenerator.ORDER_ATOMIC)+", "+
Blockly.ScaFi.valueToCode(block, "WIDTH", scafiGenerator.ORDER_ATOMIC)+
")";
return [code,scafiGenerator.ORDER_FUNCTION_CALL];
}
scafiGenerator['led_all_to'] = function(block){
const code = "ledAll to "+Blockly.ScaFi.valueToCode(block, "COLOR", scafiGenerator.ORDER_ATOMIC);
return [code,scafiGenerator.ORDER_ATOMIC];
}
scafiGenerator['color'] = function(block){
return ['"'+block.getFieldValue('COLOR')+'"', scafiGenerator.ORDER_ATOMIC];
}
scafiGenerator['type'] = function(block){
return [block.getFieldValue("TYPE"), scafiGenerator.ORDER_ATOMIC];
}
scafiGenerator['other_type'] = function(block){
return [block.getFieldValue("TYPE"), scafiGenerator.ORDER_ATOMIC];
}
//scrub_ is the common tasks for generating code from blocks, called on every block.
scafiGenerator.scrub_ = function (block, code, opt_thisOnly) {
const nextBlock = block.nextConnection && block.nextConnection.targetBlock();
let nextCode = '';
if (nextBlock) {
nextCode = opt_thisOnly ? '' : '\n' + scafiGenerator.blockToCode(nextBlock);
}
return code + nextCode;
};
Blockly.ScaFi = scafiGenerator;