1 Textual Language
Here’s a program for a fish that swims randomly:
1.1 Example
#lang scratchy |
|
------------------------------------- |
fish |
|
image is @fish-image |
|
do |
forever { |
wait 0.02 |
forward by 2 |
turn by (random 5) - 2 |
} |
The ---...-
starts a sprite declaration, where the
sprite is named fish. In addition, if the file name is
"fish-land.rkt", then fish-land is defined as the
land where fish starts.
1.2 Grammar
Here’s the grammar of this textual language (click on a keyword for more information):
| ‹prog› | ::= | ‹use›* ‹sprite›* |
| ‹use› | ::= | use ‹id› |
| ‹sprite› | ::= | ---...- ‹id› ‹clause›* |
| ‹clause› | ::= | image is ‹expr› |
| | | | x is ‹expr› |
| | | | y is ‹expr› |
| | | | size is ‹expr› |
| | | | direction is ‹expr› |
| | | | on ‹key› key ‹statement›* |
| | | | on ‹string› message ‹statement›* |
| | | | variable ‹id› is ‹expr› |
| | | | do ‹statement›* |
| ‹expr› | ::= | ‹number› | ‹image› | ‹string› |
| | | | ‹id› |
| | | | ‹expr› ‹binary-op› ‹expr› |
| | | | ‹unary-op› ‹expr› |
| | | | random ‹expr› |
| | | | touches ‹expr› |
| | | | ( ‹expr› ) |
| ‹statement› | ::= | move x by ‹expr› |
| | | | move y by ‹expr› |
| | | | move x to ‹expr› |
| | | | move y to ‹expr› |
| | | | turn by ‹expr› |
| | | | turn to ‹expr› |
| | | | forward by ‹expr› |
| | | | change size by ‹expr› |
| | | | change size to ‹expr› |
| | | | wait ‹expr› |
| | | | say ‹expr› |
| | | | hush |
| | | | hide |
| | | | show |
| | | | change image to ‹expr› |
| | | | send ‹expr› to ‹expr› |
| | | | send ‹expr› to everyone |
| | | | watch ‹expr› |
| | | | move to ‹expr› |
| | | | forever { ‹statement›* } |
| | | | while ‹expr› { ‹statement›* } |
| | | | if ‹expr› { ‹statement›* } |
| | | | ‹id› = ‹expr› |
| ‹unary-op› | ::= | - |
| | | | + |
| ‹binary-op› | ::= | + | - | * | / | < | > | <= | >= | = |
| ‹key› | ::= | ‹id› | + | - | * | / | < | > | = |
| ‹id› | ::= | a letter (in a/A to z/Z) followed by letters, numbers, and _s |
| | | | @ followed by a sequence of letters, numbers, _s, and -s |
| ‹number› | ::= | a decimal number |
| ‹string› | ::= | sequence of characters between "s |
1.3 Syntactic Forms
Imports the sprite, variable, and land name of a land defined in the file
whose name matches ‹id› with a ".rkt" suffix.
Starts a sprite declaration. Any number of -s can appear, as long as
there are at least three.
Determines the initial image for a sprite.
Determines the initial placement of a sprite.
Determines the initial size of a sprite, as a multiple of its image’s size.
Determines the initial direction of a sprite, in degrees where 0 is north,
90 is east, 180 is south, and 270 is west.
Declares actions to take when a key is pressed or a message is sent or broadcast.
Declares a variable, which is visible in the whole land.
Starts a concurrent task.
Generates a random number between 0 and one less than the integer produced by ‹expr›.
Produces true if this sprite is touching the one named by ‹expr›.
Changes the sprite’s direction either
by a number of degrees
clockwise or
to a number of degrees like
direction.
Changes the sprite’s
x or
y location either
by a number of pixels left/up or
to a number of
pixels from the screen’s center. If neither
x or
y
is indicated, the sprite instead moves to the specified
land
(keeping its relative position from the current land).
Moves the sprite in its current direction by the specified number of pixels.
Changes the sprite’s size either
by an amount to add to
its current size multiplier or
to a multiple of its image’s size,
or sets the sprite image to a new image.
Pauses a task for the specified number of seconds.
Causes the sprite to have a speech bubble with the specified content.
Removes the sprite’s speech bubble, if any.
Hides or shows the sprite.
Sends a message to a specific sprite or to all sprites in the
land.
A sprite can respond to the message using an
on ‹string› message clause.
Switches the Scratchy view to the specified
land.
Repeats the ‹statement›s forever.
while ‹expr› { ‹statement›* }
|
Repeats the ‹statement›s as long as ‹expr› produces true.
if ‹expr› { ‹statement›* }
|
Performs the ‹statement›s only if ‹expr› produces true.
Keywords that are combined with many others.