This is a small project implementing a subset of Logo, used for learning how to build/design languages in Racket. The style and code is heavily influenced by Beautiful Racket.
To learn more about Logo, consult the primer at the Logo foundation.
This implementation supports the following commands:
Moves the turtle forwards n steps.
Moves the turtle backwards n steps.
Turns the turtle n degrees right.
Turns the turtle n degrees left.
Moves the pen up to stop drawing when moving.
Moves the pen down to resume drawing when moving.
Moves the turtle to the center of the screen.
Removes all drawings.
Moves the turtle to position (x, y).
Returns the maximum width of the logo canvas.
Returns the maximum height of the logo canvas.
Returns a random number between 0 and n.
Repeats the expressions in brackets n times.
Tests a simple numeric condition and runs the first bracket if true and the second bracket if false. The second bracket is optional.
The conditions in the if statement can be of the form
n = m
n != m
n < m
n > m
n <= m
n >= m
and all of these can be prefaced with not to change the value.
Defines a new procedure. Arguments are supplied with a leading colon. For example
to flower :size
repeat 36 [right 10 square :size]
end
defines a new procedure that can then be called with
flower 50
Inside a to statement, stop immediately returns from the procedure.
Like stop, but returns the value n.
Prints the result of the expression to the log.
Sets the pen color to the value of expression, which should be a color-name, or to the color defined by the RGB-triple (red, green, blue),
Sets the style of the pen to expression, which should either be a string in the list (‘solid’ ‘dot’ ‘long-dash’ ‘short-dash’ ‘dot-dash’) or an index to the same list.
To use the program, clone the repository and install it as a package with raco. It should then be ready for use with #lang.
This package currently supports a small subset of the logo programming language. Comments start with # and run to the end of the line. Currently, integers and strings are the only working data types. There are no floats. Strings can be written both as “string1” and ‘string2’. Both () and [] can be used as parentheses.
#lang logo
to pattern
repeat 12 [penup forward 60 pendown repeat 4 [forward 45 right 90] penup back 60 left 30]
end
pattern
#lang logo
to square :size
repeat 4 [forward :size right 90]
end
to flower :size
repeat 36 [right 10 square :size]
end
to garden :number :size
repeat :number [penup setposition random 1000 random 1000 pendown flower :size]
end
garden 20 50
#lang logo
to factorial :number
if :number = 1 [output 1]
output :number * factorial [:number - 1]
end
print factorial 5
forward factorial 10
Implement error messages when parsing fails.
Automatically put the file in the input area when running from a .rkt file.
Add support for statements that are just values, for ease of use.
Implement more commands.
Add a turtle.