Little Logo interpreter in 81 lines of Red
I have made a little “port” of a logo interpreter I wrote in Rebol some time ago. Back then I did it to try and understand how parsing works in Rebol. Now I just wanted to have some fun with Red, since it has a GUI as of the 0.6 release.
The difference here is that, in the absence of a ‘rotate’ feature on drawing an image in Red (sure to be coming soon), the turtle is drawn as two circles: one for the body and one for the head. Changing the pen colour has the happy side effect that the turtle’s outline changes as well 🙂
The trickiest part was implementing nested repeats: One level is OK, but because the parse execution sets a global variable, another nested repeat breaks things. This is solved by wrapping the implementation for the repeat command in its own function.
It just has some basic commands, listed below, which are handled by some simple parse rules.
I think it would be trivial to implement procedures as well, but I’m not sure the parse function in Red is complete yet, as it seems to be missing the ‘to’ construct. Actually, it turns out that it was a misunderstanding on my part (see the next post).
- fd/forward <distance>
- bk/back <distance>
- right <degrees>
- left <degrees>
- pu (pen up)
- pd (pen down)
- color <color>
- repeat <count> <block>
And here is the code: