Skip to content

Commit c5cad20

Browse files
committed
toLeopard: drop InputShape enum, revise traits documentation
1 parent d46b742 commit c5cad20

File tree

1 file changed

+38
-64
lines changed

1 file changed

+38
-64
lines changed

src/io/leopard/toLeopard.ts

Lines changed: 38 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,15 @@ const LEOPARD_RESERVED_SPRITE_PROPERTIES = [
282282
"stamp"
283283
];
284284

285+
/**
286+
* Desirable traits are the basic attributes controlling what syntax
287+
* is returned for any given block (or primitive value). Provide a
288+
* valid combination of traits to `inputToJS` to constrain the types
289+
* of values that will be proivded to that input.
290+
*
291+
* An empty list of desirable traits indicates any value at all
292+
* (including `undefined`) is acceptable in the current context.
293+
*/
285294
enum DesirableTraits {
286295
/**
287296
* Indicates an exact boolean (true/false) value is desired.
@@ -309,11 +318,23 @@ enum DesirableTraits {
309318

310319
/**
311320
* Indicates a string value is desired (typeof x === 'string').
321+
*
322+
* This must be specified if a number value is *not* desired;
323+
* if left unspecified (declaring any value is acceptable),
324+
* inputs with values such as "1.234", regardless if they are
325+
* string or number inputs in Scratch, will be converted to
326+
* number primitives (such as the number 1.234).
312327
*/
313328
IsString,
314329

315330
/**
316-
* Indicates a series of stack blocks is desired.
331+
* Indicates a series of stack blocks is desired. It may be
332+
* empty, contain a single block, or contain multiple blocks.
333+
*
334+
* In JavaScript, there's generally no difference between a
335+
* "function" for reporting values and a "command" for doing
336+
* side-effects, so blocks are returned without any special
337+
* syntax if a stack is desired.
317338
*/
318339
IsStack,
319340

@@ -335,6 +356,15 @@ enum DesirableTraits {
335356
IsCastToZero
336357
}
337358

359+
/**
360+
* Satisfying traits are the basic attributes that tell what kind
361+
* of value would be returned by a given block. They're mainly used
362+
* to aid the value casting which occurs at the end of `blocktoJS`.
363+
*
364+
* An empty list of satisfying traits indicates no particular type
365+
* of value is guaranteed, i.e. this block could have any value,
366+
* or the type of its value is totally indeterminate.
367+
*/
338368
enum SatisfyingTraits {
339369
/**
340370
* Indicates an exact boolean (true/false) value is satisfied.
@@ -360,6 +390,13 @@ enum SatisfyingTraits {
360390
*/
361391
IsString,
362392

393+
/**
394+
* Indicates a stack block is satisfied. This isn't generally
395+
* apropos to any special meaning in Leopard or in current
396+
* conversion code.
397+
*/
398+
IsStack,
399+
363400
/**
364401
* Indicates that the satisfied number value isn't NaN - i.e,
365402
* it's a non-NaN number.
@@ -388,69 +425,6 @@ type SatisfyingTraitCombo =
388425
| [SatisfyingTraits.IsString]
389426
| [SatisfyingTraits.IsStack];
390427

391-
/**
392-
* Input shapes are the basic attribute controlling which of a set of syntaxes
393-
* is returned for any given block (or primitive value). Provide an input shape
394-
* to inputToJS to specify what kind of value should be provided as the value
395-
* in that input. If the content of input does not match the desired shape, for
396-
* example because it is a block which returns a different type than desired,
397-
* it will be automatically cast to the correct type for use in the block.
398-
*/
399-
enum InputShape {
400-
/**
401-
* Generic shape indicating that any kind of input is acceptable. The input
402-
* will never be cast, and may be null, undefined, or any JavaScript value.
403-
*/
404-
Any = "Any",
405-
406-
/**
407-
* Number input shape. If the input block isn't guaranteed to be a number,
408-
* it is automatically wrapped with this.toNumber(), which has particular
409-
* behavior to match Scratch.
410-
*/
411-
Number = "Number",
412-
413-
/**
414-
* Special "index" shape, representing an arbitrary number which has been
415-
* decremented (decreased by 1). Scratch lists are 1-based while JavaScript
416-
* arrays and strings are indexed starting from 0, so all indexes converted
417-
* from Scratch must be decreased to match. The "index" shape allows number
418-
* primitives to be statically decremented, and blocks which include a plus
419-
* or minus operator to automtaically "absorb" the following decrement.
420-
*/
421-
Index = "Index",
422-
423-
/**
424-
* String input shape. If the input block isn't guaranteed to be a string,
425-
* it is automatically wrapped with this.toString(), which is just a wrapper
426-
* around the built-in String() op but is written so for consistency.
427-
*
428-
* The string input shape also guarantees that primitive values which could
429-
* be statically converted to a number, e.g. the string "1.234", will NOT be
430-
* converted.
431-
*/
432-
String = "String",
433-
434-
/**
435-
* Boolean input shape. If the input block isn't guaranteed to be a boolean,
436-
* it is automatically wrapped with this.toBoolean(), which has particular
437-
* behavior to match Scratch. Note that Scratch doesn't have a concept of
438-
* boolean primitives (no "true" or "false" blocks, nor a "switch" type
439-
* control for directly inputting true/false as in Snap!).
440-
*/
441-
Boolean = "Boolean",
442-
443-
/**
444-
* "Stack" block, referring to blocks which can be put one after another and
445-
* together represent a sequence of steps. Stack inputs may be empty and
446-
* otherwise are one or more blocks. In JavaScript, there's no fundamental
447-
* difference between a "function" for reporting values and a "command" for
448-
* applying effects, so no additional syntax is required to cast any given
449-
* input value to a stack.
450-
*/
451-
Stack = "Stack"
452-
}
453-
454428
function uniqueNameFactory(reservedNames: string[] | Set<string> = []) {
455429
const usedNames: Set<string> = new Set(reservedNames);
456430
return uniqueName;

0 commit comments

Comments
 (0)