Building SeaTurtle, a Logo-like Language in Swift and SpriteKit

Today, my programming environment for teaching coding to young people, SeaTurtle, was released on the Mac App Store. SeaTurtle is inspired by the languages that were popular on microcomputers in education for teaching programming in the 1980s—Logo and BASIC. Like these languages, SeaTurtle has a very simple and approachable syntax. And like Logo in particular, SeaTurtle is centered on turtle graphics. My aim in this post is to explain what SeaTurtle is and then give you some insight into how I built it.

While most Logo programs can be fairly easily translated into SeaTurtle, SeaTurtle is not a version of Logo. It has a slightly different syntax, which in my opinion is even simpler. For example, there are no special characters in the SeaTurtle programming language. There are some other simplifications: all variables are global, variables can only hold integers, and variable and subroutine names are case-insensitive. Some experienced programmers might scoff, but SeaTurtle is purposely an extremely simple programming language. These simplifications might seem like they’re teaching bad practices, but the goal is to eliminate whole classes of beginner programming errors and not give them enough features to become overwhelmed.

In my opinion, SeaTurtle is the absolute least featureful and most basic syntax programming language you can have that is still fun to use and can serve its essential function: inspiring young people to learn coding. SeaTurtle has just enough syntax to teach important concepts like variables, loops, subroutines, and arithmetic. It’s not meant to go beyond that. It’s not even meant to be a long-term programming language. It’s simply a first stepping stone to pique a person’s curiosity, so that they then have the interest to pursue a “real” programming language like Python or Java. If you want to checkout SeaTurtle’s syntax, you can see the “Language Reference” section of the SeaTurtle documentation. I have also put together an introductory video tutorial on YouTube.

SeaTurtle is a native Mac app. It has modern features like syntax highlighting, a source code stepper, and animated GIF exports of SeaTurtle drawings. It still probably has some bugs and some useful features that would be beneficial to add. If you have ideas or run into problems, let me know on Twitter @davekopec.

How I Built SeaTurtle

SeaTurtle is a Cocoa app built in Swift. The main intepreter is built in pure Swift using just the Swift standard library and no external dependencies. The turtle graphics runtime is built using SpriteKit. I originally wrote SeaTurtle because I got interested in building an interpreter. The last time I had built a parser was in a compilers class I took in graduate school. It was my lowest grade in graduate school, frankly, probably for some reasons beyond my control, but that’s a story for another time. So, I wanted to retackle this material 7 years later.

I bought the famous Dragon book and started reading it (we used a different book in graduate school). I got some value out of it, but I got more value out of more modern, less academic, more interpreter and less compiler focused books like Writing an Interpreter in Go and Crafting Interpreters, as well as some online tutorials. The tokenizer was fairly easy to write after looking at some other Swift examples. I decided on a handwritten recursive descent parser after studying the alternative options. This seemed like the easiest choice to implement, and there didn’t seem like many downsides for a relatively simple project like this. The best tutorial I found on recursive descent parsers is actually the short article Parsing Expressions by Recursive Descent by Professor Theodore Norvell at Memorial University in Newfoundland, Canada.

The interpreter is written in a protocol-oriented fashion, with one implementer of the protocol being a subclass of NSViewController that manages a SpriteKit scene. SpriteKit was an obvious choice because I had experience in it, and its “actions” (SKActions) for things like movement and rotation of the turtle mapped perfectly to the timed actions I needed for the turtle. There were some frustrations with SpriteKit though during development. In particular, SpriteKit is not good at drawing primitive shapes like lines. There is SKShapeNode but you have to repeatedly add lines to a CGPath and reset the path of the SKShapeNode. The API is not ergonomic to say the least. It would be nice if SpriteKit had a better facility for graphics primitives.

I spent almost as much time as I did writing the actual interpreter on features like animated GIF export, syntax highlighting, printing, and line numbering. Line numbering was surprisingly obtuse for NSTextView. I could’ve used an open source implementation, but I decided to instead write my own as a kind of pride thing. However, I did look at the source code for some open source implementations of line numbering in Cocoa to get an idea of where to start. In the end SeaTurtle doesn’t have any external dependencies beyong Cocoa and SpriteKit.

Like on my last Mac app, Restaurants, I used Cocoa Bindings throughout SeaTurtle. It seems like a technology that’s on its last legs now that Marzipan is a thing. I cannot wholeheartedly recommend it to other developers. It just does not get the attention from Apple that it deserves. The documentation is also not great. The best documentation is really the book Cocoa Programming for Mac OS X, which being built on Swift 1 is quite outdated (hopefully a 6th edition will come out).

Finding an Audience

I don’t know if there is an audience for SeaTurtle. Logo revivals have been very popular on Hacker News the last few months, so my timing seems good. But do people want to use a Mac-native Logo-like language that’s actually lacking some of Logo’s features (subroutines can’t have parameters in SeaTurtle for instance, another simplification, and there are no arrays/lists) but has an even simpler syntax? And that’s not Logo? I guess I will find out shortly, now that SeaTurtle is finally out. I think there might be some software developer parents out there, who are Mac users, who learned on Logo themselves growing up, and think it would be fun to teach their kids programming in a similar environment.

That audience would certainly be a niche. And if there’s not such a niche, then I got to relearn about parsing and interpreters in my spare time, and I have something functional and fun to show for it. If you buy SeaTurtle and find a bug or want a feature added, do get in touch with me.


About Me

I teach Computer Science to college students, develop software, podcast, and write books about programming including the Classic Computer Science Problems series. I'm the publisher of the hyper local newsletter BTV Daily.

You can find me on Twitter and GitHub. Check out my podcasts Kopec Explains Software and Business Books & Co. You can subscribe to my very low volume newsletter to find out about my future book, media, or software projects.

Copyright

©2012-2024 David Kopec. As an Amazon Associate I earn from qualifying purchases.

Based on tdSimple originally by Lasantha Bandara and released under the CC By 3.0.