+
Skip to content

sstoltze/logo

Repository files navigation

Logo

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.

Documentation

To learn more about Logo, consult the primer at the Logo foundation.

This implementation supports the following commands:

forward n, fd n

Moves the turtle forwards n steps.

back n, bk n

Moves the turtle backwards n steps.

right n, rt n

Turns the turtle n degrees right.

left n, lt n

Turns the turtle n degrees left.

penup, pu

Moves the pen up to stop drawing when moving.

pendown, pd

Moves the pen down to resume drawing when moving.

home

Moves the turtle to the center of the screen.

clearscreen, clear, cs

Removes all drawings.

setposition x y, setpos x y, setxy x y

Moves the turtle to position (x, y).

maxwidth

Returns the maximum width of the logo canvas.

maxheight

Returns the maximum height of the logo canvas.

random n

Returns a random number between 0 and n.

repeat n [ expressions … ]

Repeats the expressions in brackets n times.

if conditional [ true-expressions … ] ( [ false-expressions … ] )

Tests a simple numeric condition and runs the first bracket if true and the second bracket if false. The second bracket is optional.

Conditionals

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.

to id arguments … expressions … end

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

stop

Inside a to statement, stop immediately returns from the procedure.

output n

Like stop, but returns the value n.

print expression

Prints the result of the expression to the log.

setpencolor expression, setpencolor [ red green blue ], setpc expression, setpc [ red green blue ]

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),

setpenstyle expression, setps expression

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.

Installation

To use the program, clone the repository and install it as a package with raco. It should then be ready for use with #lang.

Notes

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.

Sample programs

Pattern

Link to file.

Code

#lang logo

to pattern
repeat 12 [penup forward 60 pendown repeat 4 [forward 45 right 90] penup back 60 left 30]
end

pattern

Output

./images/pattern.png

Garden

Link to file.

Code

#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

Output

./images/garden.png

Factorial

Link to file.

Code

#lang logo

to factorial :number
if :number = 1 [output 1]
output :number * factorial [:number - 1]
end

print factorial 5

forward factorial 10

Output

./images/factorial.png

Todo

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.

About

A subset of the logo programming language, implemented in Racket.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载