forked from mit-cml/appinventor-sources
/
yail.js
710 lines (664 loc) · 27.9 KB
/
yail.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2012-2019 Massachusetts Institute of Technology. All rights reserved.
/**
* @fileoverview Helper functions for generating Yail for blocks.
* @author andrew.f.mckinney@gmail.com (Andrew F. McKinney)
* @author sharon@google.com (Sharon Perl)
*/
'use strict';
goog.provide('Blockly.Yail');
goog.require('Blockly.Generator');
Blockly.Yail = new Blockly.Generator('Yail');
/**
* 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.
*
* TODO: fill this in or remove it.
* @private
*/
Blockly.Yail.RESERVED_WORDS_ = '';
/**
* Order of operation ENUMs.
* https://developer.mozilla.org/en/Yail/Reference/Operators/Operator_Precedence
*/
Blockly.Yail.ORDER_ATOMIC = 0; // 0 "" ...
Blockly.Yail.ORDER_NONE = 99; // (...)
Blockly.Yail.YAIL_ACTIVE_FORM = "(SimpleForm:getActiveForm)";
Blockly.Yail.YAIL_ADD_COMPONENT = "(add-component ";
Blockly.Yail.YAIL_ADD_TO_LIST = "(add-to-list ";
Blockly.Yail.YAIL_BEGIN = "(begin ";
// This "break" symbol must match the one that is used in the
// foreach macro, forrange and while macros
Blockly.Yail.YAIL_BREAK = "*yail-break*";
Blockly.Yail.YAIL_CALL_COMPONENT_METHOD = "(call-component-method ";
Blockly.Yail.YAIL_CALL_COMPONENT_METHOD_BLOCKING = "(call-component-method-with-blocking-continuation ";
Blockly.Yail.YAIL_CALL_COMPONENT_METHOD_CONTINUATION = "(call-component-method-with-continuation ";
Blockly.Yail.YAIL_CALL_COMPONENT_TYPE_METHOD = "(call-component-type-method ";
Blockly.Yail.YAIL_CALL_COMPONENT_TYPE_METHOD_BLOCKING = "(call-component-type-method-with-blocking-continuation ";
Blockly.Yail.YAIL_CALL_COMPONENT_TYPE_METHOD_CONTINUATION = "(call-component-type-method-with-continuation ";
Blockly.Yail.YAIL_CALL_YAIL_PRIMITIVE = "(call-yail-primitive ";
Blockly.Yail.YAIL_CLEAR_FORM = "(clear-current-form)";
// The lines below are complicated because we want to support versions of the
// Companion older then 2.20ai2 which do not have set-form-name defined
Blockly.Yail.YAIL_SET_FORM_NAME_BEGIN = "(try-catch (let ((attempt (delay (set-form-name \"";
Blockly.Yail.YAIL_SET_FORM_NAME_END = "\")))) (force attempt)) (exception java.lang.Throwable 'notfound))";
Blockly.Yail.YAIL_CLOSE_COMBINATION = ")";
Blockly.Yail.YAIL_CLOSE_BLOCK = ")\n";
Blockly.Yail.YAIL_COMMENT_MAJOR = ";;; ";
Blockly.Yail.YAIL_COMPONENT_REMOVE = "(remove-component ";
Blockly.Yail.YAIL_COMPONENT_TYPE = "component";
Blockly.Yail.YAIL_CONSTANT_ALL = '(static-field com.google.appinventor.components.runtime.util.YailDictionary \'ALL)';
Blockly.Yail.YAIL_DEFINE = "(def ";
Blockly.Yail.YAIL_DEFINE_EVENT = "(define-event ";
Blockly.Yail.YAIL_DEFINE_GENERIC_EVENT = '(define-generic-event ';
Blockly.Yail.YAIL_DEFINE_FORM = "(define-form ";
Blockly.Yail.YAIL_DO_AFTER_FORM_CREATION = "(do-after-form-creation ";
Blockly.Yail.YAIL_DOUBLE_QUOTE = "\"";
Blockly.Yail.YAIL_FALSE = "#f";
Blockly.Yail.YAIL_FILTER = "(filter_nondest ";
Blockly.Yail.YAIL_FOREACH = "(foreach ";
Blockly.Yail.YAIL_FORRANGE = "(forrange ";
Blockly.Yail.YAIL_GET_COMPONENT = "(get-component ";
Blockly.Yail.YAIL_GET_ALL_COMPONENT = "(get-all-components ";
Blockly.Yail.YAIL_GET_PROPERTY = "(get-property ";
Blockly.Yail.YAIL_GET_COMPONENT_TYPE_PROPERTY = "(get-property-and-check ";
Blockly.Yail.YAIL_GET_VARIABLE = "(get-var ";
Blockly.Yail.YAIL_AND_DELAYED = "(and-delayed ";
Blockly.Yail.YAIL_OR_DELAYED = "(or-delayed ";
Blockly.Yail.YAIL_IF = "(if ";
Blockly.Yail.YAIL_INIT_RUNTIME = "(init-runtime)";
Blockly.Yail.YAIL_INITIALIZE_COMPONENTS = "(call-Initialize-of-components";
Blockly.Yail.YAIL_LET = "(let ";
Blockly.Yail.YAIL_LEXICAL_VALUE = "(lexical-value ";
Blockly.Yail.YAIL_SET_LEXICAL_VALUE = "(set-lexical! ";
Blockly.Yail.YAIL_LINE_FEED = "\n";
Blockly.Yail.YAIL_MAP = "(map_nondest ";
Blockly.Yail.YAIL_NULL = "(get-var *the-null-value*)";
Blockly.Yail.YAIL_EMPTY_LIST = "'()";
Blockly.Yail.YAIL_EMPTY_YAIL_LIST = "'(*list*)";
Blockly.Yail.YAIL_EMPTY_DICT = "(make com.google.appinventor.components.runtime.util.YailDictionary)";
Blockly.Yail.YAIL_OPEN_BLOCK = "(";
Blockly.Yail.YAIL_OPEN_COMBINATION = "(";
Blockly.Yail.YAIL_QUOTE = "'";
Blockly.Yail.YAIL_REDUCE = "(reduceovereach ";
Blockly.Yail.YAIL_RENAME_COMPONENT = "(rename-component ";
Blockly.Yail.YAIL_SET_AND_COERCE_PROPERTY = "(set-and-coerce-property! ";
Blockly.Yail.YAIL_SET_AND_COERCE_COMPONENT_TYPE_PROPERTY = "(set-and-coerce-property-and-check! ";
Blockly.Yail.YAIL_SET_SUBFORM_LAYOUT_PROPERTY = "(%set-subform-layout-property! ";
Blockly.Yail.YAIL_SET_VARIABLE = "(set-var! ";
Blockly.Yail.YAIL_SET_THIS_FORM = "(set-this-form)\n ";
Blockly.Yail.YAIL_SPACER = " ";
Blockly.Yail.YAIL_SORT_COMPARATOR_NONDEST = "(sortcomparator_nondest ";
Blockly.Yail.YAIL_SORT_KEY_NONDEST = "(sortkey_nondest ";
Blockly.Yail.YAIL_TRUE = "#t";
Blockly.Yail.YAIL_UNREGISTER =
"com.google.appinventor.components.runtime.EventDispatcher:unregisterEventForDelegation";
Blockly.Yail.YAIL_WHILE = "(while ";
Blockly.Yail.YAIL_LIST_CONSTRUCTOR = "*list-for-runtime*";
Blockly.Yail.SIMPLE_HEX_PREFIX = "&H";
Blockly.Yail.YAIL_HEX_PREFIX = "#x";
// permit leading and trailing whitespace for checking that strings are numbers
Blockly.Yail.INTEGER_REGEXP = "^[\\s]*[-+]?[0-9]+[\\s]*$";
Blockly.Yail.FLONUM_REGEXP = "^[\\s]*[-+]?([0-9]*)((\\.[0-9]+)|[0-9]\\.)[\\s]*$";
/**
* Generate the Yail code for this blocks workspace, given its associated form specification.
*
* @param {String} formJson JSON string describing the contents of the form. This is the JSON
* content from the ".scm" file for this form.
* @param {String} packageName the name of the package (to put in the define-form call)
* @param {Boolean} forRepl true if the code is being generated for the REPL, false if for an apk
* @param {Blockly.WorkspaceSvg} workspace Workspace to use for generating code.
* @returns {String} the generated code if there were no errors.
*/
Blockly.Yail.getFormYail = function(formJson, packageName, forRepl, workspace) {
var oldForRepl = this.forRepl;
var code;
try {
this.forRepl = forRepl;
code = Blockly.Yail.getFormYail_(formJson, packageName, forRepl, workspace);
} finally {
this.forRepl = oldForRepl;
}
return code;
};
Blockly.Yail.getFormYail_ = function(formJson, packageName, forRepl, workspace) {
var jsonObject = JSON.parse(formJson);
// TODO: check for JSON parse error
var componentNames = [];
var formProperties;
var formName;
var code = [];
var propertyNameConverter = function(input) {
return input;
};
if (jsonObject.Properties) {
formProperties = jsonObject.Properties;
formName = formProperties.$Name;
} else {
throw "Cannot find form properties";
}
if (!formName) {
throw "Unable to determine form name";
}
if (!forRepl) {
code.push(Blockly.Yail.getYailPrelude(packageName, formName,
!jsonObject.Properties['Theme'] || jsonObject.Properties['Theme'] === 'Classic'));
}
var componentMap = workspace.buildComponentMap([], [], false, false);
var globalBlocks = componentMap.globals;
for (var i = 0, block; block = globalBlocks[i]; i++) {
code.push(Blockly.Yail.blockToCode(block));
}
if (formProperties) {
var sourceType = jsonObject.Source;
if (sourceType == "Form") {
code = code.concat(Blockly.Yail.getComponentLines(formName, formProperties, null /*parent*/,
componentMap, false /*forRepl*/, propertyNameConverter, workspace.getComponentDatabase()));
} else {
throw "Source type " + sourceType + " is invalid.";
}
// Fetch all of the components in the form, this may result in duplicates
componentNames = Blockly.Yail.getDeepNames(formProperties, componentNames);
// Remove the duplicates
componentNames = componentNames.filter(function(elem, pos) {
return componentNames.indexOf(elem) == pos});
// Add runtime initializations
code.push(Blockly.Yail.YAIL_INIT_RUNTIME);
if (forRepl) {
code = Blockly.Yail.wrapForRepl(formName, code, componentNames);
}
// TODO?: get rid of empty property assignments? I'm not convinced this is necessary.
// The original code in YABlockCompiler.java attempts to do this, but it matches on
// "set-property" rather than "set-and-coerce-property" so I'm not sure it is actually
// doing anything. If we do need this, something like the call below might work.
//
// finalCode = code.join('\n').replace(/\\(set-property.*\"\"\\)\\n*/mg, "");
}
return code.join('\n'); // Blank line between each section.
};
Blockly.Yail.getDeepNames = function(componentJson, componentNames) {
if (componentJson.$Components) {
var children = componentJson.$Components;
for (var i = 0, child; child = children[i]; i++) {
componentNames.push(child.$Name);
componentNames = Blockly.Yail.getDeepNames(child, componentNames);
}
}
return componentNames;
};
/**
* Generate the beginning Yail code for an APK compilation (i.e., not the REPL)
*
* @param {String} packageName the name of the package for the app
* (e.g. "appinventor.ai_somebody.myproject.Screen1")
* @param {String} formName (e.g., "Screen1")
* @returns {String} Yail code
* @private
*/
Blockly.Yail.getYailPrelude = function(packageName, formName, classicTheme) {
return "#|\n$Source $Yail\n|#\n\n"
+ Blockly.Yail.YAIL_DEFINE_FORM
+ packageName
+ Blockly.Yail.YAIL_SPACER
+ formName
+ Blockly.Yail.YAIL_SPACER
+ (classicTheme ? "#t" : "#f")
+ Blockly.Yail.YAIL_CLOSE_BLOCK
+ "(require <com.google.youngandroid.runtime>)\n";
};
/**
* Wraps Yail code for use in the REPL and returns the new code as an array of strings
*
* @param {String} formName
* @param {Array} code code strings to be wrapped
* @param {Array} componentNames array of component names
* @returns {Array} wrapped code strings
* @private
*/
Blockly.Yail.wrapForRepl = function(formName, code, componentNames) {
var replCode = [];
replCode.push(Blockly.Yail.YAIL_BEGIN);
replCode.push(Blockly.Yail.YAIL_CLEAR_FORM);
if (formName != "Screen1") {
// If this form is not named Screen1, then the REPL won't be able to resolve any references
// to it or to any properties on the form itself (such as Title, BackgroundColor, etc) unless
// we tell it that "Screen1" has been renamed to formName.
// By generating a call to rename-component here, the REPL will rename "Screen1" to formName
// in the current environment. See rename-component in runtime.scm.
replCode.push(Blockly.Yail.getComponentRenameString("Screen1", formName));
}
replCode = replCode.concat(code);
replCode.push(Blockly.Yail.getComponentInitializationString(formName, componentNames));
replCode.push(Blockly.Yail.YAIL_CLOSE_BLOCK);
return replCode;
};
/**
* Return code to initialize all components in componentMap.
*
* @param {string} formName name of the current screen
* @param {Array} componentNames array of names of components in the workspace
* @returns {Array} code strings
* @private
*/
Blockly.Yail.getComponentInitializationString = function(formName, componentNames) {
var code = Blockly.Yail.YAIL_INITIALIZE_COMPONENTS;
code += " " + Blockly.Yail.YAIL_QUOTE + formName;
for (var i = 0, cName; cName = componentNames[i]; i++) { // TODO: will we get non-component fields this way?
if (cName != formName) // Avoid duplicate initialization of the form
code = code + " " + Blockly.Yail.YAIL_QUOTE + cName;
}
code = code + ")";
return code;
};
/**
* Generate Yail code for the component described by componentJson, and all of its child
* components (if it has any). componentJson may describe a Form or a regular component. The
* generated code includes adding each component to the form, as well as generating code for
* the top-level blocks for that component.
*
* @param {String} formName
* @param {String} componentJson JSON string describing the component
* @param {String} parentName the name of the component that contains this component (which may be
* its Form, for top-level components).
* @param {Object} componentMap map from component names to the top-level blocks for that component
* in the workspace. See the Blockly.Component.buildComponentMap description for the structure.
* @param {Boolean} forRepl true iff we're generating code for the REPL rather than an apk.
* @param {function(string): string} nameConverter function that converts fully-qualified
* property names into YAIL names for invocation.
* @param {!Blockly.ComponentDatabase} componentDb Component database, for type information
* @returns {Array} code strings
* @private
*/
Blockly.Yail.getComponentLines = function(formName, componentJson, parentName, componentMap,
forRepl, nameConverter, componentDb) {
var code = [], i, block, child;
var componentName = componentJson.$Name;
if (componentJson.$Type == 'Form') {
code = Blockly.Yail.getFormPropertiesLines(formName, componentJson, !forRepl, componentDb);
} else {
code = Blockly.Yail.getComponentPropertiesLines(formName, componentJson, parentName, !forRepl,
nameConverter, componentDb);
}
if (!forRepl) {
// Generate code for all top-level blocks related to this component
if (componentMap.components && componentMap.components[componentName]) {
var componentBlocks = componentMap.components[componentName];
for (i = 0; block = componentBlocks[i]; i++) {
code.push(Blockly.Yail.blockToCode(block));
}
}
}
// Generate code for child components of this component
if (componentJson.$Components) {
var children = componentJson.$Components;
for (i = 0; child = children[i]; i++) {
code = code.concat(Blockly.Yail.getComponentLines(formName, child, componentName,
componentMap, forRepl, nameConverter, componentDb));
}
}
return code;
};
/**
* Generate Yail to add the component described by componentJson to its parent, followed by
* the code that sets each property of the component (for all its properties listed in
* componentJson).
*
* @param {String} formName
* @param {String} componentJson JSON string describing the component
* @param {String} parentName the name of the component that contains this component (which may be
* its Form, for top-level components).
* @param {Boolean} includeComments whether to include comments in the generated code
* @param {function(string): string} nameConverter function that converts a fully-qualified
* property name into a YAIL-compatible name.
* @param {Blockly.ComponentDatabase} componentDb Component database, for type information
* @returns {Array} code strings
* @private
*/
Blockly.Yail.getComponentPropertiesLines = function(formName, componentJson, parentName,
includeComments, nameConverter, componentDb) {
var code = [];
var componentName = componentJson.$Name;
var componentType = componentJson.$Type;
// generate the yail code that adds the component to its parent, followed by the code that
// sets each property of the component
if (includeComments) {
code.push(Blockly.Yail.YAIL_COMMENT_MAJOR + componentName + Blockly.Yail.YAIL_LINE_FEED);
}
// Send component type for full class path but first feed it to propertyNameConverter function
// passed in. This may trim it back to just the simple component name if we are sending this yail
// to an older companion
code.push(Blockly.Yail.YAIL_ADD_COMPONENT + parentName + Blockly.Yail.YAIL_SPACER +
nameConverter(componentDb.getType(componentType).type) +
Blockly.Yail.YAIL_SPACER + componentName + Blockly.Yail.YAIL_SPACER);
code = code.concat(Blockly.Yail.getPropertySettersLines(componentJson, componentName, componentDb));
code.push(Blockly.Yail.YAIL_CLOSE_BLOCK);
return code;
};
/**
* Generate Yail to set the properties for the Form described by componentJson.
*
* @param {String} formName
* @param {String} componentJson JSON string describing the component
* @param {Boolean} includeComments whether to include comments in the generated code
* @returns {Array} code strings
* @private
*/
Blockly.Yail.getFormPropertiesLines = function(formName, componentJson, includeComments, componentDb) {
var code = [];
if (includeComments) {
code.push(Blockly.Yail.YAIL_COMMENT_MAJOR + formName + Blockly.Yail.YAIL_LINE_FEED);
}
var yailForComponentProperties = Blockly.Yail.getPropertySettersLines(componentJson, formName, componentDb);
if (yailForComponentProperties.length > 0) {
// getPropertySettersLine returns an array of lines. So we need to
// concatenate them (using join) before pushing them onto the Yail expression.
// WARNING: There may be other type errors of this sort in this file, which
// (hopefully) will be uncovered in testing. Please
// be alert for these errors and check carefully.
code.push(Blockly.Yail.YAIL_DO_AFTER_FORM_CREATION + yailForComponentProperties.join(" ") +
Blockly.Yail.YAIL_CLOSE_BLOCK);
}
return code;
};
/**
* Generate the code to set property values for the specifed component.
*
* @param {Object} componentJson JSON String describing the component
* @param {String} componentName the name of the component (also present in the $Name field in
* componentJson)
* @param {Blockly.ComponentDatabase} componentDb The workspace's database of components and types.
* @returns {Array} code strings
* @private
*
* Hack Note (JIS): We do not output a property setter line for the TutorialURL
* property. This property is only for use within the designer and has no meaning
* within an Android app. It is harmless to output it, once we have deployed a new
* companion (version > 2.41). Once such a Companion is deployed, the exception
* for TutorialURL below (and this comment) can be removed.
*/
Blockly.Yail.getPropertySettersLines = function(componentJson, componentName, componentDb) {
var code = [];
var type = componentDb.getType(componentJson['$Type']);
function shouldSendProperty(prop, info) {
return (prop.charAt(0) !== '$' && prop !== 'Uuid' &&
prop !== 'TutorialURL' && prop !== 'BlocksToolkit') ||
(info && info['alwaysSend']);
}
// Gather all of the properties together
var propsToSend = Object.keys(componentJson);
for (var prop in type['properties']) {
var property = type['properties'][prop];
if (property['alwaysSend'] && !(prop in componentJson)) {
propsToSend.push(property['name']);
}
}
// Keep ordering so default properties will still be sent in the right position.
propsToSend.sort();
// Construct the code
propsToSend.forEach(function(prop) {
var info = type['properties'][prop];
if (shouldSendProperty(prop, info)) {
var value = componentJson[prop];
if (!Boolean(value) && value !== '') {
value = info['defaultValue'];
}
code.push(Blockly.Yail.getPropertySetterString(componentName, componentJson['$Type'], prop,
value, componentDb));
}
});
return code;
};
/**
* Generate the code to set a single property value.
*
* @param {String} componentName
* @param {String} componentType
* @param {String} propertyName
* @param {String} propertyValue
* @param {!Blockly.ComponentDatabase} componentDb Component database, for type information
* @returns code string
* @private
*/
Blockly.Yail.getPropertySetterString = function(componentName, componentType, propertyName,
propertyValue, componentDb) {
var code = Blockly.Yail.YAIL_SET_AND_COERCE_PROPERTY + Blockly.Yail.YAIL_QUOTE +
componentName + Blockly.Yail.YAIL_SPACER + Blockly.Yail.YAIL_QUOTE + propertyName +
Blockly.Yail.YAIL_SPACER;
var propDef = componentDb.getPropertyForType(componentType, propertyName);
// If a designer property does not have a corresponding block property, then propDef will be
// undefined. In this case, we assume "any" as the type. A corresponding fix is included in
// ComponentProcessor to enforce that newer components/extensions always have both a designer
// and block definition.
var propType = Blockly.Yail.YAIL_QUOTE + (propDef ? propDef.type : "any");
var value = Blockly.Yail.getPropertyValueString(propertyValue, propType);
code = code.concat(value + Blockly.Yail.YAIL_SPACER + propType + Blockly.Yail.YAIL_CLOSE_BLOCK);
return code;
};
/**
* Generate the Yail code for a property value. Special case handling when propertyType is
* "'number", "'boolean", "'component", the empty string, or null. For all other property values
* it returns the value as converted by Blockly.Yail.quotifyForREPL().
*
* @param {String} propertyValue
* @param {String} propertyType
* @returns code string
* @private
*/
Blockly.Yail.getPropertyValueString = function(propertyValue, propertyType) {
if (propertyType == "'number") {
if (propertyValue.match(Blockly.Yail.INTEGER_REGEXP)
|| propertyValue.match(Blockly.Yail.FLONUM_REGEXP)) { // integer
return propertyValue;
} else if (propertyValue.match(Blockly.Yail.SIMPLE_HEX_PREFIX + "[0-9A-F]+")) { // hex
return Blockly.Yail.YAIL_HEX_PREFIX +
propertyValue.substring(Blockly.Yail.SIMPLE_HEX_PREFIX.length);
}
} else if (propertyType == "'boolean") {
if (-1 != propertyValue.indexOf("False")) {
return "#f";
} else if (-1 != propertyValue.indexOf("True")) {
return "#t";
}
} else if (propertyType == "'component") {
if (propertyValue == "") {
return "\"\"";
} else {
return Blockly.Yail.YAIL_GET_COMPONENT + propertyValue + ")";
}
}
if (propertyValue == "" || propertyValue == "null") { // empty string
return "\"\"";
}
return Blockly.Yail.quotifyForREPL(propertyValue);
};
/**
* Generate the code to rename a component
*
* @param {String} oldName
* @param {String} newName
* @returns {String} code
* @private
*/
Blockly.Yail.getComponentRenameString = function(oldName, newName) {
return Blockly.Yail.YAIL_RENAME_COMPONENT + Blockly.Yail.quotifyForREPL(oldName)
+ Blockly.Yail.YAIL_SPACER + Blockly.Yail.quotifyForREPL(newName)
+ Blockly.Yail.YAIL_CLOSE_BLOCK;
};
/**
* Transform a string to the Kawa input representation of the string, for sending to
* the REPL, by using backslash to escape quotes and backslashes. But do not escape a backslash
* if it is part of \n. Then enclose the result in quotes.
* TODO: Extend this to a complete transformation that deals with the full set of formatting
* characters.
*
* @param {String} s string to be quotified
* @returns {String}
* @private
*/
Blockly.Yail.quotifyForREPL = function(s) {
if (!s) {
return null;
} else {
var sb = [];
sb.push('"');
var len = s.length;
var lastIndex = len - 1;
for (var i = 0; i < len; i++) {
c = s.charAt(i);
if (c == '\\') {
// If this is \n, \t or \r don't slashify the backslash
// TODO(user): Make this cleaner and more general
if (!(i == lastIndex) && (s.charAt(i + 1) == 'n' || s.charAt(i + 1) == 't' || s.charAt(i + 1) == 'r')) {
sb.push(c);
sb.push(s.charAt(i + 1));
i = i + 1;
} else {
sb.push('\\');
sb.push(c);
}
} else if (c == '"') {
sb.push('\\');
sb.push(c);
} else {
var u = s.charCodeAt(i); // unicode of c
if (u < ' '.charCodeAt(0) || u > '~'.charCodeAt(0)) {
// Replace any special chars with \u1234 unicode
var hex = "000" + u.toString(16);
hex = hex.substring(hex.length - 4);
sb.push("\\u" + hex);
} else {
sb.push(c);
}
}
}
sb.push('"');
return sb.join("");
}
};
/**
* Encode a string as a properly escaped Yail string, complete with quotes.
* @param {String} string Text to encode.
* @return {String} Yail string.
* @private
*/
Blockly.Yail.quote_ = function(string) {
string = Blockly.Yail.quotifyForREPL(string);
if (!string) { // quotifyForREPL can return null for
string = '""'; // empty string
}
return string;
};
/**
* 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.Yail.scrubNakedValue = function(line) {
return line;
};
/**
* 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 Yail code created for this block.
* @param {boolean} thisOnly if true, only return code for this block and not any following
* statements note that calls of scrub_ with no 3rd parameter are equivalent to thisOnly=false,
* which was the behavior before this parameter was added.
* @return {string} Yail code with comments and subsequent blocks added.
* @private
*/
Blockly.Yail.scrub_ = function(block, code, thisOnly) {
if (code === null) {
// Block has handled code generation itself.
return '';
}
var commentCode = '';
/* TODO: fix for Yail comments?
// Only collect comments for blocks that aren't inline.
if (!block.outputConnection || !block.outputConnection.targetConnection) {
// Collect comment for this block.
var comment = block.getCommentText();
if (comment) {
commentCode += Blockly.Generator.prefixLines(comment, '// ') + '\n';
}
// Collect comments for all value arguments.
// Don't collect comments for nested statements.
for (var x = 0; x < block.inputList.length; x++) {
if (block.inputList[x].type == Blockly.INPUT_VALUE) {
var childBlock = block.inputList[x].targetBlock();
if (childBlock) {
var comment = Blockly.Generator.allNestedComments(childBlock);
if (comment) {
commentCode += Blockly.Generator.prefixLines(comment, '// ');
}
}
}
}
}*/
var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
var nextCode = thisOnly ? "" : this.blockToCode(nextBlock);
return commentCode + code + nextCode;
};
Blockly.Yail.getDebuggingYail = function() {
var code = [];
var componentMap = Blockly.Component.buildComponentMap([], [], false, false);
var globalBlocks = componentMap.globals;
for (var i = 0; i < globalBlocks.length; i++) {
code.push(Blockly.Yail.blockToCode(globalBlocks[i]));
}
var blocks = Blockly.mainWorkspace.getTopBlocks(true);
for (var x = 0, block; block = blocks[x]; x++) {
// generate Yail for each top-level language block
if (!block.category) {
continue;
}
code.push(Blockly.Yail.blockToCode(block));
}
return code.join('\n\n');
};
/**
* Generate code for the specified block but *not* attached blocks.
* @param {Blockly.Block} block The block to generate code for.
* @return {string|!Array} For statement blocks, the generated code.
* For value blocks, an array containing the generated code and an
* operator order value. Returns '' if block is null.
*/
Blockly.Yail.blockToCode1 = function(block) {
if (!block) {
return '';
}
var func = this[block.type];
if (!func) {
throw 'Language "' + name + '" does not know how to generate code ' +
'for block type "' + block.type + '".';
}
var code = func.call(block);
if (code instanceof Array) {
// Value blocks return tuples of code and operator order.
if (block.disabled || block.isBadBlock()) {
code[0] = '';
}
return [this.scrub_(block, code[0], true), code[1]];
} else {
if (block.disabled || block.isBadBlock()) {
code = '';
}
return this.scrub_(block, code, true);
}
};
/**
* Generates YAIL that will unregister an event if the corresponding block is disabled in the
* workspace.
*
* @param {!Blockly.BlockSvg} block
* @returns {string}
*/
Blockly.Yail.disabledEventBlockToCode = function(block) {
return Blockly.Yail.YAIL_OPEN_BLOCK + Blockly.Yail.YAIL_UNREGISTER + Blockly.Yail.YAIL_SPACER +
Blockly.Yail.YAIL_ACTIVE_FORM + Blockly.Yail.YAIL_SPACER +
Blockly.Yail.YAIL_QUOTE + block.getFieldValue('COMPONENT_SELECTOR') +
Blockly.Yail.YAIL_SPACER + Blockly.Yail.YAIL_QUOTE + block.eventName +
Blockly.Yail.YAIL_CLOSE_BLOCK;
};