A hands-on, test driven guide to implementing a simple programming language
Python Shell
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is 27 commits behind kvalle:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


DIY Lang

batteries included, some assembly required

In this tutorial/workshop we'll be implementing our own little language, more or less from scratch.

By the end of the tutorial you will be the proud author of a programming language, and will hopefully better understand how programming languages work on a fundamental level.

What we will be making

We will make a relatively simple, but neat language. We aim for the following features:

  • A handful of datatypes (integers, booleans and symbols)
  • Variables
  • First class functions with lexical scoping
  • That nice homemade quality feeling

We will not have:

  • A proper type system
  • Error handling
  • Good performance
  • And much, much more

The language should be able to interpret the following code by the time we are done:

(define fact
    ;; Factorial function
    (lambda (n)
        (if (eq n 0)
            1 ; Factorial of 0 is 1
            (* n (fact (- n 1))))))

;; When parsing the file, the last statement is returned
(fact 5)

The syntax is very similar to languages in the Lisp family. If you find the example unfamiliar, you might want to have a look at a more detailed description of the language.


Before we get started, make sure you have installed Python and Pip. (It should now work with Python 3. If you have any problem with it, please fill an issue.)

Then install nose, the Python test framework we'll be using.

pip install nose

Optional: If you are familiar with virtualenv you might want to do this in a separate pyenv.

Finally, clone this repo, and you're ready to go!

git clone https://github.com/kvalle/diy-lang.git

Also, if you're unfamiliar with Python, you might want to have a look at the basics in the Python tutorial before we get going. There is also the small Python cheat sheet to help you along.

A few tips

Take the time to consider the following points before we get going:

  • Keep things simple

    Don't make things more complicated than they need to be. The tests should hopefully guide you every step of the way.

  • Read the test descriptions

    Each test has a small text describing what you are going to implement and why. Reading these should make things easier, and you might end up learning more.

  • Use the provided functions

    Some of the more boring details are already taken care of. Take the time to look at the functions provided in parser.py, and the various imports in files where you need to do some work.

  • The Python cheat sheet in python.md

    Unless you're fluent in Python, there should be some helpful pointers in the Python cheat sheet.

  • Description of your language

    Read a description of the language you are going to make in language.md.

Get started!

The workshop is split up into eight parts. Each consist of an introduction, and a bunch of unit tests which it is your task to make run. When all the tests run, you'll have implemented that part of the language.

Have fun!