The Turtle Programming Language

This page will look at the details of how Evolved Art represents a turtle's program internally. The Turtle Programming Language is very simple. A turtle does not need to know much in order to draw. All of the complicated geometry and math involved in drawing is handled by the turtle drawing engine inside Evolved Art. This is the brilliance that the Logo programming language provided when its authors conceived of the language. That is why I was inspired by Logo. The text below briefly describes the language and provides an example of a very simple turtle program.

The Turtle Language

The Evolved Art turtle language, is loosely based on the Logo programming language. There are a number of reasons why I deviated from the actual Logo language, but the primary reason is simplicity - it makes handling the evolution step of crossover much easier to program. Since Evolved Art is a hobby, I simply did not have the time to invest in making the language more complex.

The Evolved Art turtle language consists of the following commands:

In reality, these commands have a number of variations that help to eliminate the need for numbers and variables in the programs. For example, the Move Forward command actually consists of Forward_1, Forward_2, Forward_5, Forward_10, Forward_50, and Forward_100. By combining these individual commands along with the ability to repeat them, the code can evolve to move forward any distance. For example, Forward_1 and Forward_2 can combine to cause the turtle to move forward a distance of three units. If combined with Repeat_5, the distance moved becomes fifteen units.

Likewise, the language has commands that turn the turtle right or left a number of degrees. For example, Left_1, Left_2, Right_1, Right_2, and so on. Combining these commands, along with repeat commands, can produce a turn of any number of degrees.

The Lift Pen and Drop Pen commands do exactly what they sound like. When a turtle lifts the pen, drawing does not occur, but moving, turning, and color changing continue to occur. This allows a turtle to make drawings with line segments that are not continuous. When the Drop Pen command is executed, drawing resumes.

Change Color commands have three major variants, Red, Green and Blue, and then sub-variants that are amounts. The colors are based on RGB values from 0 to 255. So the color commands look like Red_1, Red_2, Red_5, Green_1, Green_2, Green_5, Blue_1, Blue_2, Blue_5 and so on. Each color command adds the amount of red, green or blue color to the current RGB value used to determine the color of the pen the turtle is drawing with. When a value of red, green or blue exceeds 255, then 255 is subtracted from the value to keep it in the range of 0 to 255 (modulus for those of you who know that term).

Finally, there are repeat commands Repeat_1, Repeat_2, Repeat_5 and so on. Repeat commands are very powerful because they do not simply repeat a single command, they can repeat entire blocks of commands, much like a loop in C or Java. These are critical to creating complex drawings.

Program Code Versus Expression Trees

When you view turtles programs in the Turtle View, or in an email or turtle definition file, it is presented to you as text. This makes it readable, as well as easy to transfer and edit. However, internally Evolved Art works with Turtle programs as Expression Trees, which makes life much easier for the Evolved Art programmer. Programmers are very familiar with expression trees, but it may be a new concept for you. But it is an important concept when it comes to understanding the crossover that Evolved Art performs when it evolves a new generation of turtles.

The following figure shows a very simple turtle program along, along with the expression tree that represents the program:

The code basically says: Repeat 4 times: ( Move Forward 100 units, Turn Right 90 degrees ). As you might have guessed, this program will draw a perfect square.