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
|
'use strict';
const { Project } = require('sb-edit');
const util = require('util');
// TODO: set prefix to \t
function indent(string, prefix = ' ') {
return string.split('\n').map(l => prefix + l).join('\n');
}
function input(inp, target, flag = false) {
if (!inp) {
return '';
}
const val = inp.value.replace ? inp.value.replace(/[()\[\]]|v$/g, m => '\\' + m) : inp.value;
switch (inp.type) {
case 'number':
case 'angle':
return `(${val})`;
case 'string':
return `[${val}]`;
case 'variable':
case 'list':
case 'graphicEffect':
case 'rotationStyle':
case 'greaterThanMenu':
case 'stopMenu':
case 'dragModeMenu':
case 'propertyOfMenu':
case 'currentMenu':
case 'mathopMenu':
case 'frontBackMenu':
case 'forwardBackwardMenu':
case 'costumeNumberName':
return `[${val} v]`;
case 'costume':
case 'sound':
case 'goToTarget':
case 'pointTowardsTarget':
case 'penColorParam':
case 'target':
case 'cloneTarget':
case 'touchingTarget':
case 'distanceToMenu':
return `(${val} v)`;
case 'broadcast':
case 'backdrop':
case 'key':
if (flag) {
return `[${val} v]`;
} else {
return `(${val} v)`;
}
case 'color':
const hex = k => inp.value[k].toString(16).padStart(2, '0')
return `[#${hex('r') + hex('g') + hex('b')}]`;
case 'block':
if (flag) {
return '\n' + indent(blockToScratchblocks(inp.value, target)) + '\n';
} else {
return blockToScratchblocks(inp.value, target);
}
case 'blocks':
return '\n' + indent(blocksToScratchblocks(inp.value, target)) + '\n';
default:
return `\x1b[33m<${inp.type}>\x1b[0m`;
}
}
function blockToScratchblocks(block, target) {
if (!target) {
throw new Error('expected target');
}
const i = (key, ...args) => input(block.inputs[key], target, ...args);
const operator = op => `(${i('NUM1')} ${op} ${i('NUM2')})`;
const boolop = op => `<${i('OPERAND1')} ${op} ${i('OPERAND2')}>`;
switch (block.opcode) {
// motion ------------------------------------------------------ //
case 'motion_movesteps':
return `move ${i('STEPS')} steps`;
case 'motion_turnright':
return `turn cw ${i('DEGREES')} degrees`;
case 'motion_turnleft':
return `turn ccw ${i('DEGREES')} degrees`;
case 'motion_goto':
return `go to ${i('TO')}`;
case 'motion_gotoxy':
return `go to x: ${i('X')} y: ${i('Y')}`;
case `motion_glideto`:
return `glide ${i('SECS')} secs to ${i('TO')}`;
case `motion_glidesecstoxy`:
return `glide ${i('SECS')} secs to x: ${i('X')} y: ${i('Y')}`;
case 'motion_pointindirection':
return `point in direction ${i('DIRECTION')}`;
case 'motion_pointtowards':
return `point towards ${i('TOWARDS')}`;
case 'motion_changexby':
return `change x by ${i('DX')}`;
case 'motion_setx':
return `set x to ${i('X')}`;
case 'motion_changeyby':
return `change y by ${i('DY')}`;
case 'motion_sety':
return `set y to ${i('Y')}`;
case 'motion_ifonedgebounce':
return 'if on edge, bounce';
case 'motion_setrotationstyle':
return `set rotation style ${i('STYLE')}`;
case 'motion_xposition':
return '(x position)';
case 'motion_yposition':
return '(y position)';
case 'motion_direction':
return '(direction)';
// looks ------------------------------------------------------- //
case 'looks_sayforsecs':
return `say ${i('MESSAGE')} for ${i('SECS')} seconds`;
case 'looks_say':
return `say ${i('MESSAGE')}`;
case 'looks_thinkforsecs':
return `think ${i('MESSAGE')} for ${i('SECS')} seconds`;
case 'looks_think':
return `think ${i('MESSAGE')}`;
case 'looks_switchcostumeto':
return `switch costume to ${i('COSTUME')}`;
case 'looks_nextcostume':
return 'next costume';
case 'looks_switchbackdropto':
return `switch backdrop to ${i('BACKDROP')}`;
case 'looks_nextbackdrop':
return `next backdrop`;
case 'looks_changesizeby':
return `change size by ${i('CHANGE')}`;
case 'looks_setsizeto':
return `set size to ${i('SIZE')}%`;
case 'looks_changeeffectby':
return `change ${i('EFFECT')} effect by ${i('CHANGE')} :: looks`;
case 'looks_seteffectto':
return `set ${i('EFFECT')} effect to ${i('VALUE')} :: looks`;
case 'looks_cleargraphiceffects':
return 'clear graphic effects';
case 'looks_show':
return 'show';
case 'looks_hide':
return 'hide';
case 'looks_gotofrontback':
return `go to ${i('FRONT_BACK')} layer`;
case 'looks_goforwardbackwardlayers':
return `go ${i('FORWARD_BACKWARD')} ${i('NUM')} layers`;
case 'looks_costumenumbername':
return `(costume ${i('NUMBER_NAME')})`;
case 'looks_backdropnumbername':
return `(backdrop ${i('NUMBER_NAME')})`;
case 'looks_size':
return '(size)';
case 'looks_hideallsprites':
return 'hide all sprites :: looks';
case 'looks_switchbackdroptoandwait':
return `switch backdrop to ${i('BACKDROP')} and wait`;
case 'looks_changestretchby':
return `change stretch by ${i('CHANGE')} :: looks`;
case 'looks_setstretchto':
return `set stretch to ${i('STRETCH')} % :: looks`;
// sound ------------------------------------------------------- //
case 'sound_playuntildone':
return `play sound ${i('SOUND_MENU')} until done`;
case 'sound_play':
return `start sound ${i('SOUND_MENU')}`;
case 'sound_stopallsounds':
return 'stop all sounds';
case 'sound_changeeffectby':
return `change ${i('EFFECT')} effect by ${i('VALUE')} :: sound`;
case 'sound_seteffectto':
return `set ${i('EFFECT')} effect to ${i('VALUE')} :: sound`;
case 'sound_cleareffects':
return 'clear sound effects';
case 'sound_changevolumeby':
return `change volume by ${i('VOLUME')}`;
case 'sound_setvolumeto':
return `set volume to ${i('VOLUME')} %`;
case 'sound_volume':
return '(volume)';
// events ------------------------------------------------------ //
case 'event_whenflagclicked':
return 'when green flag clicked';
case 'event_whenkeypressed':
return `when ${i('KEY_OPTION', true)} key pressed`;
case 'event_whenthisspriteclicked':
return 'when this sprite clicked';
case 'event_whenbackdropswitchesto':
return `when backdrop switches to ${i('BACKDROP', true)}`;
case 'event_whengreaterthan':
return `when ${i('WHENGREATERTHANMENU')} > ${i('VALUE')}`;
case 'event_whenbroadcastreceived':
return `when I receive ${i('BROADCAST_OPTION', true)}`;
case 'event_broadcast':
return `broadcast ${i('BROADCAST_INPUT')}`;
case 'event_broadcastandwait':
return `broadcast ${i('BROADCAST_INPUT')} and wait`;
// control ----------------------------------------------------- //
case 'control_wait':
return `wait ${i('DURATION')} seconds`;
case 'control_repeat':
return `repeat ${i('TIMES')}` + (i('SUBSTACK', true) || '\n') + 'end';
case 'control_forever':
return 'forever' + (i('SUBSTACK', true) || '\n') + 'end';
case 'control_if':
return `if ${i('CONDITION') || '<>'} then` + (i('SUBSTACK', true) || '\n') + 'end';
case 'control_if_else':
return `if ${i('CONDITION') || '<>'} then` + (i('SUBSTACK', true) || '\n') + 'else' + (i('SUBSTACK2', true) || '\n') + 'end';
case 'control_wait_until':
return `wait until ${i('CONDITION') || '<>'}`;
case 'control_repeat_until':
return `repeat until ${i('CONDITION') || '<>'}` + (i('SUBSTACK', true) || '\n') + 'end';
case 'control_stop':
return `stop ${i('STOP_OPTION')}`;
case 'control_start_as_clone':
return 'when I start as a clone';
case 'control_create_clone_of':
return `create clone of ${i('CLONE_OPTION')}`;
case 'control_delete_this_clone':
return 'delete this clone';
// sensing ----------------------------------------------------- //
case 'sensing_touchingobject':
return `<touching ${i('TOUCHINGOBJECTMENU')} ?>`;
case 'sensing_touchingcolor':
return `<touching ${i('COLOR')} ?>`;
case 'sensing_coloristouchingcolor':
return `<${i('COLOR')} is touching ${i('COLOR2')} ?>`;
case 'sensing_distanceto':
return `(distance to ${i('DISTANCETOMENU')})`;
case 'sensing_askandwait':
return `ask ${i('QUESTION')} and wait`;
case 'sensing_answer':
return '(answer)';
case 'sensing_keypressed':
return `<key ${i('KEY_OPTION')} pressed?>`;
case 'sensing_mousedown':
return '<mouse down?>';
case 'sensing_mousex':
return '(mouse x)';
case 'sensing_mousey':
return '(mouse y)';
case 'sensing_setdragmode':
return `set drag mode ${i('DRAG_MODE')}`;
case 'sensing_loudness':
return '(loudness)';
case 'sensing_loud':
return '<loud? :: sensing>';
case 'sensing_timer':
return '(timer)';
case 'sensing_resettimer':
return 'reset timer';
case 'sensing_of':
return `(${i('PROPERTY')} of ${i('OBJECT')})`;
case 'sensing_current':
return `(current ${i('CURRENTMENU')})`;
case 'sensing_dayssince2000':
return '(days since 2000)';
case 'sensing_username':
return '(username)';
case 'sensing_userid':
return '(user id :: sensing)';
// operators --------------------------------------------------- //
case 'operator_add':
return operator('+');
case 'operator_subtract':
return operator('-');
case 'operator_multiply':
return operator('*');
case 'operator_divide':
return operator('/');
case 'operator_random':
return `(pick random ${i('FROM')} to ${i('TO')})`;
case 'operator_gt':
return boolop('>');
case 'operator_lt':
return boolop('<');
case 'operator_equals':
return boolop('=');
case 'operator_and':
return boolop('and');
case 'operator_or':
return boolop('or');
case 'operator_not':
return `<not ${i('OPERAND')}>`;
case 'operator_join':
return `(join ${i('STRING1')} ${i('STRING2')})`;
case 'operator_letter_of':
return `(letter ${i('LETTER')} of ${i('STRING')})`;
case 'operator_length':
return `(length of ${i('STRING')})`;
case 'operator_contains':
return `<${i('STRING1')} contains ${i('STRING2')} ? :: operators>`;
case 'operator_mod':
return operator('mod');
case 'operator_round':
return `(round ${i('NUM')})`;
case 'operator_mathop':
return `(${i('OPERATOR')} of ${i('NUM')})`;
// data -------------------------------------------------------- //
case 'data_variable':
return `(${block.inputs.VARIABLE.value} :: variables)`;
case 'data_setvariableto':
return `set ${i('VARIABLE')} to ${i('VALUE')}`;
case 'data_changevariableby':
return `change ${i('VARIABLE')} by ${i('VALUE')}`;
case 'data_showvariable':
return `show variable ${i('VARIABLE')}`;
case 'data_hidevariable':
return `hide variable ${i('VARIABLE')}`;
case 'data_listcontents':
return `(${block.inputs.LIST.value} :: list)`;
case 'data_addtolist':
return `add ${i('ITEM')} to ${i('LIST')}`;
case 'data_deleteoflist':
return `delete ${i('INDEX')} of ${i('LIST')}`;
case 'data_deletealloflist':
return `delete all of ${i('LIST')}`;
case 'data_insertatlist':
return `insert ${i('ITEM')} at ${i('INDEX')} of ${i('LIST')}`;
case 'data_replaceitemoflist':
return `replace item ${i('INDEX')} of ${i('LIST')} with ${i('ITEM')}`;
case 'data_itemoflist':
return `(item ${i('INDEX')} of ${i('LIST')})`;
case 'data_itemnumoflist':
return `(item # of ${i('ITEM')} in ${i('LIST')})`;
case 'data_lengthoflist':
return `(length of ${i('LIST')})`;
case 'data_listcontainsitem':
return `<${i('LIST')} contains ${i('ITEM')} ? :: list>`;
case 'data_showlist':
return `show list ${i('LIST')}`;
case 'data_hidelist':
return `hide list ${i('LIST')}`;
// custom blocks ----------------------------------------------- //
case 'procedures_definition':
const spec = block.inputs.ARGUMENTS.value.map(({ type, name }) => {
switch (type) {
case 'label':
return name.replace(/\//g, '\\/');
case 'numberOrString':
return `(${name})`;
case 'boolean':
return `<${name}>`;
}
}).join(' ');
return `define ${spec}` + (block.inputs.WARP.value ? ' // run without screen refresh' : '');
case 'procedures_call':
const definition = target.scripts.map(s => s.blocks[0]).find(b => b.opcode === 'procedures_definition' && b.inputs.PROCCODE.value === block.inputs.PROCCODE.value);
if (definition) {
let index = 0;
return definition.inputs.ARGUMENTS.value.map(({ type, name }) => {
switch (type) {
case 'label':
return name.replace(/\//g, '\\/');
default:
// TODO: deal with empty boolean inputs, which can't even load yet
return input(block.inputs.INPUTS.value[index++], target);
}
}).join(' ') + ' :: custom';
} else {
return `... // missing custom block definition for ${block.inputs.PROCCODE.value}`;
}
case 'argument_reporter_string_number':
return `(${block.inputs.VALUE.value} :: custom-arg)`;
case 'argument_reporter_boolean':
return `<${block.inputs.VALUE.value} :: custom-arg>`;
// extension: pen ---------------------------------------------- //
case 'pen_clear':
return 'erase all';
case 'pen_stamp':
return 'stamp';
case 'pen_penDown':
return 'pen down';
case 'pen_penUp':
return 'pen up';
case 'pen_setPenColorToColor':
return `set pen color to ${i('COLOR')}`;
case 'pen_changePenColorParamBy':
return `change pen ${i('COLOR_PARAM')} by ${i('VALUE')}`;
case 'pen_setPenColorParamTo':
return `set pen ${i('COLOR_PARAM')} to ${i('VALUE')}`;
case 'pen_changePenSizeBy':
return `change pen size by ${i('SIZE')}`;
case 'pen_setPenSizeTo':
return `set pen size to ${i('SIZE')}`;
case 'pen_changePenShadeBy':
return `change pen shade by ${i('SHADE')}`;
case 'pen_setPenShadeToNumber':
return `set pen shade to ${i('SHADE')}`;
case 'pen_changePenHueBy':
return `change pen color by ${i('HUE')}`;
case 'pen_setPenHueTo':
return `set pen hue to ${i('HUE')}`;
default:
return `\x1b[33m${block.opcode} (${Object.keys(block.inputs).join(', ')})\x1b[0m`;
}
}
function blocksToScratchblocks(blocks, target) {
return blocks.map(b => blockToScratchblocks(b, target)).join('\n');
}
function scriptToScratchblocks(script, target) {
return blocksToScratchblocks(script.blocks, target);
}
Object.assign(module.exports, {
scriptToScratchblocks
});
|