SLogo : Basic Syntax and Builtin Commands
The basic commands that make up the SLogo language are given below and here is an example of a typical program:
repeat 4 [
lt 90
fd 50
]
|
|
Things to keep in mind about the syntax of a program:
- Tokens are guaranteed to be separated by one or more spaces
- Tokens are not case sensitive and any possible correct command name spellings should be accepted (even though the command names below are given in all capital letters)
- Commands can be formatted over any number of lines, including multiple commands on the same line
- Commands all return a double value as described below
- Parameters to a command can be the value of a variable or the value returned from calling any command
- No parentheses are needed to resolve order of operations, the number of parameters expected by each command is enough to resovle any ambiguities
Basic Syntax
Syntax is given as Regular Expressions, a typical way to match a variety of strings that share a common, well-defined, pattern.
Token |
Syntax |
Semantics |
Examples |
Constant |
-?[0-9]+\.?[0-9]*
|
any real valued number
NOTE, to avoid potential ambiguity in parsing there should not be a space between the negative sign and the value
|
50
-1.3
|
Variable |
:[a-zA-Z]+
|
any word preceded by a colon, :
NOTE, if a variable name is used that has not previously been given a value, return a default value (i.e., 0) |
:distance
:side
|
Command |
[a-zA-Z_]+(\?)?
|
any word can be a command-name
NOTE, all commands return a numeric value
NOTE, built-in commands are given below and user-defined commands cannot reuse those names |
forward
fd |
List |
[
]
|
these brackets enclose a list of zero or more commands or variables
NOTE, to make parsing easier, these will always be separated from other tokens by spaces |
[ fd 50 rt 90 ]
|
Comment |
^#.*
|
Ignore any lines that are empty, i.e., only zero or more spaces, and lines that begin with comment character, "#". |
# a comment
#fd 50
|
Turtle Commands
Name(s) |
Description |
FORWARD pixels
FD pixels |
moves turtle forward in its current heading by pixels distance
returns distance turtle moved (the value of pixels) |
BACK pixels
BK pixels |
moves turtle backward in its current heading by pixels distance
returns distance turtle moved (the value of pixels) |
LEFT degrees
LT degrees |
turns turtle counterclockwise by degrees angle
returns amount turtle turned (the value of degrees) |
RIGHT degrees
RT degrees |
turns turtle clockwise by degrees angle
returns amount turtle turned (the value of degrees) |
SETHEADING degrees
SETH degrees |
turns turtle to an absolute heading
returns number of degrees turned |
TOWARDS x y |
turns turtle to face the point (x, y), where (0, 0) is the center of the screen
returns the number of degrees turtle turned |
SETXY x y
GOTO x y |
moves turtle to an absolute screen position, where (0, 0) is the center of the screen
returns distance turtle moved |
PENDOWN PD |
puts pen down such that when the turtle moves, it leaves a trail
returns 1 |
PENUP PU |
puts pen up such that when the turtle moves, it does not leave a trail
returns 0 |
SHOWTURTLE ST |
makes turtle visible
returns 1 |
HIDETURTLE HT |
makes turtle invisible
returns 0 |
HOME |
moves turtle to the center of the screen (0 0)
returns distance turtle moved |
CLEARSCREEN CS |
erases turtle's trails and sends it to the home position
returns distance turtle moved |
Turtle Queries
Name(s) |
Description |
XCOR |
returns the turtle's X coordinate from the center of the screen |
YCOR |
returns the turtle's Y coordinate from the center of the screen |
HEADING |
returns the turtle's heading in degrees |
PENDOWN?
PENDOWNP |
returns 1 if turtle's pen is down, 0 if it is up |
SHOWING?
SHOWINGP |
returns 1 if turtle is showing, 0 if it is hiding |
Math Operations
Name(s) |
Description |
SUM expr1 expr2
+ expr1 expr2 |
returns sum of the values of expr1 and expr2 |
DIFFERENCE expr1 expr2
- expr1 expr2 |
returns difference of the values of expr1 and expr2 |
PRODUCT expr1 expr2
* expr1 expr2 |
returns product of the values of expr1 and expr2 |
QUOTIENT expr1 expr2
/ expr1 expr2 |
returns quotient of the values of expr1 and expr2 |
REMAINDER expr1 expr2
% expr1 expr2 |
returns remainder on dividing the values of expr1 by expr2 |
MINUS expr
~ expr |
returns negative of the values of expr |
RANDOM max
RAND max
|
returns random non-negative number strictly less than max |
RANDOMRANGE min max
RANDR min max
|
returns random number within the given range min to max, inclusive |
SINE degrees
SIN degrees
|
returns sine of degrees |
COSINE degrees
COS degrees
|
return cosine of degrees |
TANGENT degrees
TAN degrees
|
returns tangent of degrees |
ARCTANGENT degrees
ATAN degrees |
returns arctangent of degrees |
SQUAREROOT expr
SQRT expr |
returns square root of expr |
LOG expr |
returns natural log of expr |
POWER base exponent
POW base exponent |
returns base raised to the power of the exponent |
PI |
returns value of the number Pi (Math.PI ) |
Boolean Operations
Note, since all return values are real valued (double
), which are hard to compare exactly due to rounding issues in Java calculations, two values should be assumed as equal if they are within a tolerance of 0.001
of each other.
Name(s) |
Description |
LESS? expr1 expr2
< expr1 expr2 |
returns 1 if the value of expr1 is strictly less than the value of expr2, otherwise 0 |
LESSEQUAL? expr1 expr2
<= expr1 expr2 |
returns 1 if the value of expr1 is less than or equal to the value of expr2, otherwise 0 |
GREATER? expr1 expr2
> expr1 expr2 |
returns 1 if the value of expr1 is strictly greater than the value of expr2, otherwise 0 |
GREATEREQUAL? expr1 expr2
>= expr1 expr2 |
returns 1 if the value of expr1 is greater than or equal to the value of expr2, otherwise 0 |
EQUAL? expr1 expr2
== expr1 expr2 |
returns 1 if the value of expr1 and the value of expr2 are equal, otherwise 0 |
NOTEQUAL? expr1 expr2
!= expr1 expr2 |
returns 1 if the value of expr1 and the value of expr2 are not equal, otherwise 0 |
AND test1 test2 |
returns 1 if test1 and test2 are non-zero, otherwise 0 |
OR test1 test2 |
returns 1 if test1 or test2 are non-zero, otherwise 0 |
NOT test |
returns 1 if test is 0 and 0 if test is non-zero |
Variables, Control Structures, and User-Defined Commands
Name(s) |
Description |
MAKE variable expr
SET variable expr |
assigns the value of expr to variable, creating the variable if necessary
returns expr |
REPEAT expr [ command(s) ] |
runs command(s) given in the list the value of expr number of times
returns the value of the final command executed (or 0 if no commands are executed)
NOTE, the value of the current iteration, starting at 1, is automatically assigned to the variable :repcount so that it can be accessed by the command(s) |
DOTIMES [ variable limit ]
[ command(s) ] |
runs command(s) for each value specified in the range, i.e., from (1 - limit) inclusive
returns the value of the final command executed (or 0 if no commands are executed)
NOTE, variable is assigned to each succeeding value so that it can be accessed by the command(s) |
FOR [ variable start end increment ]
[ command(s) ] |
runs command(s) for each value specified in the range, i.e., from (start - end), going by increment
returns the value of the final command executed (or 0 if no commands are executed)
NOTE, variable is assigned to each succeeding value so that it can be accessed by the command(s) |
IF expr [ command(s) ] |
if expr is not 0, runs the command(s) given in the list
returns the value of the final command executed (or 0 if no commands are executed) |
IFELSE expr
[ trueCommand(s) ]
[ falseCommand(s) ] |
if expr is not 0, runs the trueCommand(s) given in the first list, otherwise runs the falseCommand(s) given in the second list
returns the value of the final command executed (or 0 if no commands are executed) |
TO commandName
[ variable(s) ]
[ command(s) ] |
assigns command(s) given in the second list to commandName using variable(s) given in first list as parameters
when commandName is used later in a program, any given values are assigned to variables that can be accessed when the command list is run and the value of the final command executed is returned (or 0 if no commands are executed)
returns 1 if command is successfully defined, otherwise 0 |