Skip to main content

A Turtle Graphics DSL

Posted by driscoll on October 6, 2012 at 1:05 PM PDT

In my previous post, I discussed the example program that I wrote to exercise the things I've described so far.  Today I'll discuss what the DSL will actually look like - as with most programming tasks, designing your DSL before implementation is almost always advised.

There are a couple of different ways that you can use to describe languages, the most popular (and my favorite) being BNF, but for our purposes, that's certainly overkill.  Instead, let's just do this informally.

First, we'll allow all existing Groovy operators and control structures - there are good reasons to strip those out on occasion, but this isn't one of them.  We'll cover how to do that at a later date.  What this means is that structures like while (condition) {block} and if (condition) {block} will work just fine.  We'll also be able to create methods inline, just as you can with Groovy scripts.  And standard comments (i.e., // and /* */) will also work as expected.

We'll need the following commands to create an environment that allows for turtle graphics:

  • Turtle / Movement Commands
    • forward number 
      • move the turtle forward by number of steps
    • back number
      • move the turtle backward by number of steps
    • right number
      • turn the turtle number of degrees to the right (clockwise)
    • left number
      • turn the turtle number of degrees to the left (counter clockwise)
    • home
      • return the turtle to the starting position, without drawing any lines
  • Pen / Drawing Commands
    • pendown
      • set the pen to draw when the turtle moves
    • penup
      • set the pen to not draw when the turtle moves
    • pencolor Color
      • change the pen color to Color
  • Animation / Demo Commands
    • show
      • show the turtle icon
    • hide
      • hide the turtle icon
    • speed number
      • change the speed at which the turtle draws, larger is faster
Keywords/methods are in bold, parameters are in italics.  Since we need to decide what number means, we can define it as a double, and since Groovy will auto convert int to double, we're done with that.  Color is a little more problematic - ideally, we'd make it easy to use and understand, and have a whole bunch of code to do that, but since we're currently just making a sample program, we'll just use javafx.scene.paint.Color.
 
If you download the Groovy Turtle Graphics program Napili, you'll see that all of these methods are already implemented in org.netdance.napili.Turtle.  I'll talk about how to turn those methods into a DSL in the next post.  For now, here's a sample program that you can run using these commands, along with the standard stuff that Groovy provides:
 
def circle(size) {
    45.times {
        forward size
        right 8
    }
}

speed 3
penup
forward 250
pendown
pencolor Color.PURPLE

12.times {
    circle 10
    right 30
}

home
hide

This program draws a series of interlocking circles, like you'd get from the child's toy Spirograph.   We define a method (circle), set the speed a bit higher to make it all draw quickly, move forward without drawing to center the design, set the pen color to purple (PURPLE is one of many predefined values in javafx.scene.paint.Color), then draw 12 circles, turning 30 degrees after each.  At the end, we park the turtle in the home position and then hide it.  If you're unfamiliar with Groovy, Integer.times(Closure) is a method that Groovy places on all values of Integer (and primitives are auto boxed to their class type in Groovy) - it just executes the closure as many times as the value of the integer - remember, parenthesis are optional for methods with arguments, so the circle method is really the equivalent of Integer.valueOf(45).times({forward size; right 8;}) - I trust you'll agree that the format I used is actually easier to read, once you're used to it.

See you next time, where I'll talk about how to turn the methods on Class Turtle into the DSL above.

(As usual, this article is cross posted from my main blog site.)

Related Topics >>