Improved Logo interpreter in Red

Using what I have learned in the last few days about ‘parse’ in Red, I have improved the Logo interpreter from the last post, so that it now supports procedures.

(It turns out that I had it all wrong about Red’s parse. It’s actually already full featured and it was a misunderstanding on my side that made me think it’s incomplete).

Anyway, it’s amazing how, with so little extra effort, it was possible to extend the Logo/Turtle program to do so much more, still all in under 100 lines of code! (Though when you think about it, you can paste you code in Notepad++ or Sublime Text and press Ctrl+J and then everything will be one one line of code! :-) ).

The only thing lacking, perhaps, is that I should use floats for the co-ordinates (done),  as there is some inaccuracy due to rounding, as the turtle just uses pair! co-ordinates corresponding to the pixels on the screen.

The last things I want to add are, as mentioned, changing the co-ordinates to use float! numbers (done) and adding an ‘undo’ command.

So the full list of commands is now:

  • fd | forward <distance> – Move forward
  • bk | back <distance> – Move backward
  • rt | right <degrees> – Turn right
  • lt | left <degrees> – Turn left
  • pu | penup – Pen up (won’t draw lines)
  • pd | pendown – Pen down (will draw lines)
  • color <color> – Change the pen colour
  • repeat <count> [block] – repeat a block of commands <count> times
  • to <procedure name> <procedure body> end – create a callable procedure
  • ht – hide the turtle
  • st – show the turtle
  • reset – reset everything, clear history
  • clear – clear all lines on the screen
  • home – return turtle to center, 0 degrees
  • q | quit – exit the program
  • <proc> – call a stored procedure

You can find the code here:

https://gist.github.com/mydoghasworms/bd342a10f2b806f050ed903e5be435a0

Where is the turtle now?

Where is the turtle now?