This repository was archived by the owner on May 18, 2026. It is now read-only.
forked from scratchfoundation/scratch-gui
-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathindex.js
More file actions
496 lines (450 loc) · 14.6 KB
/
Copy pathindex.js
File metadata and controls
496 lines (450 loc) · 14.6 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
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
import _ from 'lodash';
import Blockly from 'scratch-blocks';
import Generator from '../generator';
import MathBlocks from './math.js';
import TextBlocks from './text.js';
import ColourBlocks from './colour.js';
import MotionBlocks from './motion.js';
import LooksBlocks from './looks.js';
import SoundBlocks from './sound.js';
import EventBlocks from './event.js';
import ControlBlocks from './control.js';
import SensingBlocks from './sensing.js';
import OperatorsBlocks from './operators.js';
import DataBlocks from './data.js';
import ProcedureBlocks from './procedure.js';
import RubyBlocks from './ruby.js';
import MusicBlocks from './music.js';
import PenBlocks from './pen.js';
import VideoBlocks from './video.js';
import Text2SpeechBlocks from './text2speech.js';
import TranslateBlocks from './translate.js';
import MakeyMakeyBlocks from './makeymakey.js';
import MicrobitBlocks from './microbit.js';
import MicrobitMoreBlocks from './microbit_more.js';
import BoostBlocks from './boost.js';
import EV3Blocks from './ev3.js';
import WeDo2Blocks from './wedo2.js';
import GdxForBlocks from './gdx_for.js';
import MeshBlocks from './mesh.js';
import SmalrubotS1Blocks from './smalrubot_s1.js';
import KoshienBlocks from './koshien.js';
const SCALAR_TYPE = '';
const LIST_TYPE = 'list';
const RubyGenerator = new Generator('Ruby');
RubyGenerator.addReservedWords(
`BEGIN
class
ensure
nil
self
when
END
def
false
not
super
while
alias
defined?
for
or
then
yield
and
do
if
redo
true
__LINE__
begin
else
in
rescue
undef
__FILE__
break
elsif
module
retry
unless
__ENCODING__
case
end
next
return
until`.split(/\s+/));
/* eslint-disable no-multi-spaces */
RubyGenerator.ORDER_ATOMIC = 0; // 0 "" ...
RubyGenerator.ORDER_COLLECTION = 1; // tuples, lists, dictionaries
RubyGenerator.ORDER_STRING_CONVERSION = 1; // `expression...`
RubyGenerator.ORDER_MEMBER = 2; // ::
RubyGenerator.ORDER_INDEX = 3; // []
RubyGenerator.ORDER_FUNCTION_CALL = 4; // ()
RubyGenerator.ORDER_UNARY_SIGN = 5; // +(単項) ! ~
RubyGenerator.ORDER_EXPONENTIATION = 6; // **
RubyGenerator.ORDER_UNARY_MINUS_SIGN = 7; // -(単項)
RubyGenerator.ORDER_MULTIPLICATIVE = 8; // * / %
RubyGenerator.ORDER_ADDITIVE = 9; // + -
RubyGenerator.ORDER_BITWISE_SHIFT = 10; // << >>
RubyGenerator.ORDER_BITWISE_AND = 11; // &
RubyGenerator.ORDER_BITWISE_XOR = 12; // ^
RubyGenerator.ORDER_BITWISE_OR = 12; // |
RubyGenerator.ORDER_RELATIONAL = 13; // > >= < <=
RubyGenerator.ORDER_EQUALS = 14; // <=> == === != =~ !~
RubyGenerator.ORDER_LOGICAL_AND = 15; // &&
RubyGenerator.ORDER_LOGICAL_OR = 16; // ||
RubyGenerator.ORDER_RANGE = 17; // .. ...
RubyGenerator.ORDER_CONDITIONAL = 18; // ?:(条件演算子)
RubyGenerator.ORDER_ASSIGNMENT = 19; // =(+=, -= ... )
RubyGenerator.ORDER_NOT = 20; // not
RubyGenerator.ORDER_AND_OR = 21; // and or
RubyGenerator.ORDER_NONE = 99; // (...)
/* eslint-enable no-multi-spaces */
RubyGenerator.init = function (options) { // eslint-disable-line no-unused-vars
this.definitions_ = {};
if (this.variableDB_) {
this.variableDB_.reset();
} else {
this.variableDB_ = new Blockly.Names(RubyGenerator.RESERVED_WORDS_);
}
};
RubyGenerator.finish = function (code, options) {
const defs = [];
for (const name in this.definitions_) {
const def = this.definitions_[name];
if (this.isString(def)) {
if (name.match(/^require__/)) {
this.requires_[name] = def;
} else if (name.match(/^prepare__/)) {
this.prepares_[name] = def;
} else {
defs.push(def);
}
}
}
const comments = RubyGenerator.getTargetCommentTexts();
if (comments.length > 0) {
const commentCodes = comments.map(comment => `${this.prefixLines(comment, '# ')}\n`);
code = `${commentCodes.join('\n')}\n${code}`;
}
if (options && options.withSpriteNew) {
const spriteNewCode = this.spriteNew(this.currentTarget);
if (code.length > 0) {
code = this.prefixLines(code, this.INDENT);
}
code = `${spriteNewCode} do\n${code}end\n`;
}
if (defs.length === 0 && code.length === 0) {
return '';
}
let s = '';
if (defs.length > 0) {
s += `${defs.join('\n')}\n\n`;
}
return s + code;
};
RubyGenerator.initTargets = function (options) {
this.requires_ = {};
this.prepares_ = {};
if (options && Object.prototype.hasOwnProperty.call(options, 'requires')) {
options.requires.forEach(name => {
this.requires_[`require__${name}`] = `require "${name}"`;
});
}
};
RubyGenerator.finishTargets = function (code, options) { // eslint-disable-line no-unused-vars
let s = '';
const requires = Object.keys(this.requires_).map(name => this.requires_[name]);
if (requires.length > 0) {
s += `${requires.join('\n')}\n\n`;
}
const prepares = Object.keys(this.prepares_).map(name => this.prepares_[name]);
if (prepares.length > 0) {
s += `${prepares.join('\n')}\n\n`;
}
return s + code;
};
RubyGenerator.isString = function (s) {
return _.isString(s);
};
RubyGenerator.isWhiteSpace = function (s) {
return s === null || (this.isString(s) && s.trim().length === 0);
};
RubyGenerator.scalarToCode = function (scalar) {
if (this.isString(scalar)) {
return this.quote_(scalar);
}
return scalar;
};
RubyGenerator.listToCode = function (list) {
const values = list.map(i => {
if (this.isString(i)) {
return this.quote_(i);
}
return i;
}).join(', ');
return `[${values}]`;
};
RubyGenerator.hashToCode = function (hash, separator = ': ', brace = true) {
const lines = [];
for (const key in hash) {
const value = hash[key];
lines.push(`${key}${separator}${value}`);
}
let code = lines.join(',\n');
if (brace) {
code = ['{', this.prefixLines(code, this.INDENT), '}'].join('\n');
}
return code;
};
RubyGenerator.numberOrStringToCode = function (value) {
if (RubyGenerator.isString(value) &&
value[0] === '"' &&
value[value.length - 1] === '"') {
const s = value.slice(1, value.length - 1);
const n = Number(s);
if (!isNaN(n) && !(n === 0 && RubyGenerator.isWhiteSpace(s))) {
return n;
}
}
return value;
};
RubyGenerator.nosToCode = RubyGenerator.numberOrStringToCode;
RubyGenerator.spriteNew = function (renderedTarget) {
if (!renderedTarget) {
return null;
}
const attributes = {};
if (renderedTarget.x !== 0) {
attributes.x = renderedTarget.x;
}
if (renderedTarget.y !== 0) {
attributes.y = renderedTarget.y;
}
if (renderedTarget.direction !== 90) {
attributes.direction = renderedTarget.direction;
}
if (!renderedTarget.visible) {
attributes.visible = !!renderedTarget.visible;
}
if (renderedTarget.size !== 100) {
attributes.size = renderedTarget.size;
}
if (renderedTarget.currentCostume > 1) {
attributes.current_costume = renderedTarget.currentCostume - 1;
}
const costumes = renderedTarget.sprite.costumes;
if (costumes.length > 0) {
const s = costumes.map(i => {
const h = {
asset_id: this.quote_(i.assetId),
name: this.quote_(i.name),
bitmap_resolution: i.bitmapResolution ? i.bitmapResolution : 1,
data_format: this.quote_(i.dataFormat),
rotation_center_x: i.rotationCenterX,
rotation_center_y: i.rotationCenterY
};
return this.hashToCode(h);
}).join(',\n');
attributes.costumes = `[\n${this.prefixLines(s, this.INDENT)}\n]`;
}
if (renderedTarget.rotationStyle !== 'all around') {
attributes.rotation_style = this.quote_(renderedTarget.rotationStyle);
}
const variables = [];
const lists = [];
for (const id in renderedTarget.variables) {
const v = renderedTarget.variables[id];
switch (v.type) {
case SCALAR_TYPE:
variables.push(v);
break;
case LIST_TYPE:
lists.push(v);
break;
}
}
if (variables.length > 0) {
const s = variables.map(i => {
const h = {
name: this.quote_(this.escapeVariableName(i.name))
};
if (i.value !== 0) {
h.value = this.scalarToCode(i.value);
}
return this.hashToCode(h);
}).join(',\n');
attributes.variables = `[\n${this.prefixLines(s, this.INDENT)}\n]`;
}
if (lists.length > 0) {
const s = lists.map(i => {
const h = {
name: this.quote_(this.escapeVariableName(i.name))
};
if (i.value.length > 0) {
h.value = this.listToCode(i.value);
}
return this.hashToCode(h);
}).join(',\n');
attributes.lists = `[\n${this.prefixLines(s, this.INDENT)}\n]`;
}
let code = this.hashToCode(attributes, ': ', false);
if (code.length > 0) {
const indent = renderedTarget.isStage ? ' ' : ' ';
code = `,\n${this.prefixLines(code, indent)}`;
}
const klass = renderedTarget.isStage ? 'Stage' : 'Sprite';
const name = renderedTarget.sprite.name;
return `${klass}.new(${this.quote_(name)}${code})`;
};
RubyGenerator.scrubNakedValue = function (line) {
return `${line}\n`;
};
RubyGenerator.escapeChars_ = {
'"': '\\"',
'\\': '\\\\'
};
RubyGenerator.quote_ = function (string) {
let i;
const s = String(string);
const sb = ['"'];
for (i = 0; i < s.length; i++) {
const ch = s.charAt(i);
sb.push(RubyGenerator.escapeChars_[ch] || ch);
}
sb.push('"');
return sb.join('');
};
RubyGenerator.scrub_ = function (block, code) {
if (code === null) {
return '';
}
let commentCode = '';
if (!this.isConnectedValue(block)) {
let comment = this.getCommentText(block);
if (comment && !comment.startsWith('@ruby:')) {
commentCode += `${this.prefixLines(comment, '# ')}\n`;
}
const inputs = this.getInputs(block);
for (const name in inputs) {
const input = inputs[name];
const childBlock = this.getBlock(input.block);
if (childBlock) {
comment = this.allNestedComments(childBlock);
if (comment) {
const filteredComment = comment.split('\n')
.filter(line => !line.startsWith('@ruby:'))
.join('\n');
if (filteredComment.trim().length > 0) {
commentCode += this.prefixLines(filteredComment, '# ');
}
}
}
}
}
const nextBlock = this.getBlock(block.next);
let nextCode = this.blockToCode(nextBlock);
let endCode = '';
if (block.isStatement) {
if (nextCode !== '') {
nextCode = this.prefixLines(nextCode, this.INDENT);
}
endCode = 'end\n';
delete block.isStatement;
}
return commentCode + code + nextCode + endCode;
};
RubyGenerator.spriteName = function () {
return 'self';
};
const escapeIdentityRegexp =
/[\x00-\x1f\x7f-\x9f !"#$%&'()*+,-./:;<=>?@[\\\]^`{|}~]/g; // eslint-disable-line no-control-regex
RubyGenerator.escapeVariableName = function (s) {
return s.replace(escapeIdentityRegexp, '_');
};
RubyGenerator.escapeMethodName = RubyGenerator.escapeVariableName;
RubyGenerator.makeVariableName = function (isStage, name) {
const prefix = isStage ? '$' : '@';
return `${prefix}${name.replace(escapeIdentityRegexp, '_')}`;
};
RubyGenerator.variableName = function (id, type = SCALAR_TYPE) {
let currVar;
let isStage;
const target = this.currentTarget;
const variables = target.variables;
if (Object.prototype.hasOwnProperty.call(variables, id)) {
currVar = variables[id];
isStage = target.isStage;
} else if (target.runtime && !target.isStage) {
const stage = target.runtime.getTargetForStage();
if (stage && Object.prototype.hasOwnProperty.call(stage.variables, id)) {
currVar = stage.variables[id];
isStage = true;
}
}
if (currVar && currVar.type === type) {
return this.makeVariableName(isStage, currVar.name);
}
return null;
};
RubyGenerator.listName = function (id) {
return this.variableName(id, LIST_TYPE);
};
RubyGenerator.variableNameByName = function (name, type = SCALAR_TYPE) {
let currVar;
let isStage;
const target = this.currentTarget;
if (target.runtime) {
const stage = target.runtime.getTargetForStage();
currVar = stage.lookupVariableByNameAndType(name, type);
isStage = true;
}
if (!currVar) {
currVar = target.lookupVariableByNameAndType(name, type);
isStage = target.isStage;
}
if (currVar && currVar.type === type) {
return this.makeVariableName(isStage, currVar.name);
}
return null;
};
RubyGenerator.listNameByName = function (name) {
return this.variableNameByName(name, LIST_TYPE);
};
RubyGenerator.getScripts = function () {
return Generator.prototype.getScripts.call(this).sort((a, b) => {
const aValue = (this.getBlock(a).opcode === 'procedures_definition' ? 1 : -1);
const bValue = (this.getBlock(b).opcode === 'procedures_definition' ? 1 : -1);
return bValue - aValue;
});
};
MathBlocks(RubyGenerator);
TextBlocks(RubyGenerator);
ColourBlocks(RubyGenerator);
MotionBlocks(RubyGenerator);
LooksBlocks(RubyGenerator);
SoundBlocks(RubyGenerator);
EventBlocks(RubyGenerator);
ControlBlocks(RubyGenerator);
SensingBlocks(RubyGenerator);
OperatorsBlocks(RubyGenerator);
DataBlocks(RubyGenerator);
ProcedureBlocks(RubyGenerator);
RubyBlocks(RubyGenerator);
MusicBlocks(RubyGenerator);
PenBlocks(RubyGenerator);
VideoBlocks(RubyGenerator);
Text2SpeechBlocks(RubyGenerator);
TranslateBlocks(RubyGenerator);
MakeyMakeyBlocks(RubyGenerator);
MicrobitBlocks(RubyGenerator);
MicrobitMoreBlocks(RubyGenerator);
BoostBlocks(RubyGenerator);
EV3Blocks(RubyGenerator);
WeDo2Blocks(RubyGenerator);
GdxForBlocks(RubyGenerator);
MeshBlocks(RubyGenerator);
SmalrubotS1Blocks(RubyGenerator);
KoshienBlocks(RubyGenerator);
export default RubyGenerator;