Implementing a Programming Language in Swift — Part 1: Introduction

Þorvaldur Rúnarsson
Swift2Go
Published in
2 min readJan 21, 2019

--

Welcome! This is the first part in a tutorial series on “Implementing a Programming Language in Swift.” TLDR; Full Source Code

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:

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!

P.S. Don’t forget to clap 👏 Also feel free to follow me here on Medium for notifications and discussions on future tutorials.

--

--

Þorvaldur Rúnarsson
Swift2Go

Full Stack Developer (TypeScript, React, Python, Node.js, Swift, ReactNative, Flutter), also... dad, football enthusiast & guitar enthusiast.