CompSci 308 Spring 2024 |
Advanced Software Design and Implementation |
The basic commands that make up the SLogo language are given below and here is an example of a typical program:
|
![]() |
Things to keep in mind about the syntax of a program:
Your SLOGO parser must support the following six language features, which are identified by their syntax. Syntax is given as Regular Expressions, a typical way to match a variety of strings that share a common, well-defined, pattern.
ID | Token | Priority | Syntax | Semantics | Examples |
---|---|---|---|---|---|
SLOGO-01
|
Constant | Core |
|
Any real valued number. When the user inputs a token that begins with "-" or a digit (0-9), the IDE should successfully parse the token if it matches the constant regex. Otherwise, the IDE should present an error to the user that says the input contains an "Invalid Constant". The error message should include the invalid token that caused the error. NOTE, to avoid potential ambiguity in parsing there should not be a space between the negative sign and the value |
|
SLOGO-02
|
Variable | Core |
|
Any word proceeded by a colon, :. When the user inputs a token that begins with ":", if the token does not match the variable regex, the IDE should present and error message to the user that says the input contains an "Invalid Variable" and the value of the token that caused the error. Otherwise, if a value has been assigned to the variable, return the value of the variable. If no value has been assigned to the variable, return a default value (i.e., 0). |
|
SLOGO-03
|
Command | Core |
|
Any word can be a command-name. When the user inputs a token that begins with a letter (a-zA-Z_), the interpreter should check to see if the token matches the name of a command. If no command with that name exists, the IDE should present an error to the user that says the input contains an "Invalid Command". The name of the command should be included in the error message. If a command with that name exists but the wrong number of parameters are provided, the IDE should present an error to the user that says the input contains an "Invalid Number of Parameters". The error message should include the name of the command and the number of parameters that were provided and the number of parameters that were expected. If the command name matches the name of a command and the correct number of parameters are provided, the IDE should invoke the command and return the output. NOTE, all commands return a numeric value NOTE, built-in commands are given below and user-defined commands cannot reuse those names NOTE, command names are not case sensitive |
|
SLOGO-04
|
List | Core |
|
These brackets enclose a list of zero or more commands or variables. When the user inputs tokens wrapped in brackets, everything inside the brackets should be treated as a list of commands or variables. This overrides the precedence of normal whitespace parsing. If there are mismatched brackets, the IDE should present an error to the user that contains "Unterminated List". NOTE, to make parsing easier, these will always be separated from other tokens by whitespace. |
|
SLOGO-05
|
Comment | Core |
|
Ignore any lines that are empty, i.e., only zero or more spaces, and
lines that begin with comment character, “#”.
When the user inputs a token that begins with "#", the IDE should ignore the token and any tokens that follow it on the same line. |
|
For all command definitions, the “canonical” name of the command is bold. Use this name when referring to the command
ID | Name(s) | Priority | Description |
---|---|---|---|
SLOGO-06
|
FORWARD pixels FD pixels |
Core |
Moves turtle forward in its current heading by pixels
distance Returns distance turtle moved (the value of pixels) |
SLOGO-07
|
BACK pixels BK pixels |
Core |
Moves turtle backward in its current heading by pixels
distance Returns distance turtle moved (the value of pixels) |
SLOGO-08
|
LEFT degrees LT degrees |
Core |
Turns turtle counterclockwise by degrees angle returns amount turtle turned (the value of degrees) |
SLOGO-09
|
RIGHT degrees RT degrees |
Core |
Turns turtle clockwise by degrees angle Returns amount turtle turned (the value of degrees) |
SLOGO-10
|
SETHEADING degrees SETH degrees |
Core |
Turns turtle to an absolute heading Returns number of degrees turned |
SLOGO-11
|
TOWARDS x y | Core |
Turns turtle to face the point (x, y), where (0, 0) is
the center of the screen Returns the number of degrees turtle turned |
SLOGO-12
|
SETXY x y GOTO x y |
Core |
Moves turtle to an absolute screen position, where (0, 0) is the center
of the screen Returns distance turtle moved |
SLOGO-13
|
PENDOWN PD |
Core |
Puts pen down such that when the turtle moves, it leaves a trail Returns 1 |
SLOGO-14
|
PENUP PU |
Core |
Puts pen up such that when the turtle moves, it does not leave a
trail Returns 0 |
SLOGO-15
|
SHOWTURTLE ST |
Core |
Makes turtle visible Returns 1 |
SLOGO-16
|
HIDETURTLE HT |
Core |
Makes turtle invisible Returns 0 |
SLOGO-17
|
HOME | Core |
Moves turtle to the center of the screen (0 0) Returns distance turtle moved |
SLOGO-18
|
CLEARSCREEN CS |
Core |
Erases turtle's trails and sends it to the home position Returns distance turtle moved |
ID | Name(s) | Priority | Description |
---|---|---|---|
SLOGO-19
|
XCOR | Core | Returns the turtle's X coordinate from the center of the screen |
SLOGO-20
|
YCOR | Core | Returns the turtle's Y coordinate from the center of the screen |
SLOGO-21
|
HEADING | Core | Returns the turtle's heading in degrees |
SLOGO-22
|
PENDOWN? PENDOWNP |
Core | Returns 1 if turtle's pen is down, 0 if it is up |
SLOGO-23
|
SHOWING? SHOWINGP |
Core | Returns 1 if turtle is showing, 0 if it is hiding |
ID | Name(s) | Priority | Description |
---|---|---|---|
SLOGO-24
|
SUM expr1 expr2 + expr1 expr2 |
Core | Returns sum of the values of expr1 and expr2 |
SLOGO-25
|
DIFFERENCE expr1 expr2 - expr1 expr2 |
Core | Returns difference of the values of expr1 and expr2 |
SLOGO-26
|
PRODUCT expr1 expr2 * expr1 expr2 |
Core | Returns product of the values of expr1 and expr2 |
SLOGO-27
|
QUOTIENT expr1 expr2 / expr1 expr2 |
Core | Returns quotient of the values of expr1 and expr2 |
SLOGO-28
|
|
Core | returns negative of the values of expr |
SLOGO-30
|
RANDOM max RAND max |
Core | Returns random non-negative number strictly less than max |
SLOGO-29
|
REMAINDER expr1 expr2 % expr1 expr2 |
Extension | Returns remainder on dividing the values of expr1 by expr2 |
SLOGO-31
|
RANDOMRANGE min max RANDR min max |
Extension | Returns random number within the given range min to max, inclusive |
SLOGO-32
|
SINE degrees SIN degrees |
Extension | returns sine of degrees |
SLOGO-33
|
COSINE degrees COS degrees |
Extension | Return cosine of degrees |
SLOGO-34
|
TANGENT degrees TAN degrees |
Extension | Returns tangent of degrees |
SLOGO-35
|
ARCTANGENT degrees ATAN degrees |
Extension | Returns arctangent of degrees |
SLOGO-36
|
SQUAREROOT expr SQRT expr |
Extension | returns square root of expr |
SLOGO-37
|
LOG expr | Extension | returns natural log of expr |
SLOGO-38
|
POWER base exponent POW base exponent |
Extension | Returns base raised to the power of the exponent |
SLOGO-39
|
PI | Extension |
Returns value of the number Pi (Math.PI )
|
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.
ID | Name(s) | Priority | Description |
---|---|---|---|
SLOGO-40
|
EQUAL? expr1 expr2 == expr1 expr2 |
Core | Returns 1 if the value of expr1 and the value of expr2 are equal, otherwise 0 |
SLOGO-41 |
LESS? expr1 expr2 < expr1 expr2 |
Core | Returns 1 if the value of expr1 is strictly less > than the value of expr2, otherwise 0 |
SLOGO-42 |
GREATER? expr1 expr2 > expr1 expr2 |
Core | returns 1 if the value of expr1 is strictly greater than the value of expr2, otherwise 0 |
SLOGO-43
|
AND test1 test2 | Core | Returns 1 if test1 and test2 are non-zero, otherwise 0 |
SLOGO-44
|
OR test1 test2 | Core | Returns 1 if test1 or test2 are non-zero, otherwise 0 |
SLOGO-45
|
NOT test | Extension | Returns 1 if test is 0 and 0 if test is non-zero |
SLOGO-46
|
NOTEQUAL? expr1 expr2 != expr1 expr2 |
Extension | Returns 1 if the value of expr1 and the value of expr2 are not equal, otherwise 0 |
SLOGO-47
|
LESSEQUAL? expr1 expr2 <= expr1 expr2 |
Extension | returns 1 if the value of expr1 is less than or equal to the value of expr2, otherwise 0 |
SLOGO-48
|
GREATEREQUAL? expr1 expr2 >= expr1 expr2 |
Extension | Returns 1 if the value of expr1 is greater than or equal to the value of expr2, otherwise 0 |
ID | Name(s) | Priority | Description |
---|---|---|---|
SLOGO-49
|
MAKE variable expr SET variable expr |
Core |
Assigns the value of expr to
variable, creating the variable if necessary returns expr |
SLOGO-50
|
REPEAT expr [ command(s) ] | Core |
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)
|
SLOGO-51
|
DOTIMES [ variable limit ] [ command(s) ] |
Core |
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) |
SLOGO-52
|
FOR [ variable start end increment ] [ command(s) ] |
Core |
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) |
SLOGO-53
|
IF expr [ command(s) ] | Core |
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) |
SLOGO-54
|
IFELSE expr [ trueCommand(s) ] [ falseCommand(s) ] |
Core |
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) |
SLOGO-55
|
TO commandName [ variable(s) ] [ command(s) ] |
Core |
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 |