Implementing a Programming Language in Swift — Part 1: Introduction

By Þorvaldur Rúnarsson

Welcome! This is the first part in a tutorial series on “Implementing a Programming Language in Swift.”

In general it isn’t considered very practical to create new programming languages. In fact in most cases it’s even considered a waste of time. Not pragmatic enough. Thankfully this is true in most cases and open source languages seem to exist for almost any niche. But despite all this, knowing a thing or two about how interpreters are implemented can actually come in handy when solving numerous problems.

Understanding the inner mechanics might even change your approach to software development as it did for me. The driving factor when I wrote my first language (Zolang) was a challenge at work. It was the first time for me that implementing an interpreter sounded like a good way to go. I don’t expect it to be the last.

At the time we needed a single source of truth for various business logic that was implemented separately in multiple programming languages. We had a go to scripting language (Ruby) and then a separate language for each platform, iOS (Swift), Android (Kotlin) & server (TypeScript).

By implementing a simple interpreter we were able to create a single interface for all of them and it was much easier to do than I had anticipated.

Dwight agrees!

As you can see, I found that writing my own language was definitely worth the hassle. A realization which inspired me to share this tutorial series with you guys.

In this series we will begin by creating a simple interpreter for a calculator, and then expand that implementation to a simple programming language. Here’s a rough plan for the first 6 parts in this series:

  • Part 1: Introduction
  • Part 2: Grammar — Designing Our First Language (01/28/19)
  • Part 3: The Lexer (02/04/19)
  • Part 4: The Parser (02/11/19)
  • Part 5: Adding Variables (02/18/19)
  • Part 6: Adding Functions (02/25/19)

I hope you guys are looking forward to this, I know I am. You will see a post from me every Monday morning for next few weeks. I want to add more tutorials than I’ve already planned for, but it will depend on your feedback, so don’t hesitate to comment or clap. Every bit of feedback is greatly appreciated.

Coming up next: Grammar!