A Useless Compiler in PureScript

Posted on April 1, 2019 by Riccardo

Intro

This week I’ve decided to write some code in PureScript that resembles a compiler. As for the last few posts, I’ve limited the scope to the smallest possible unit.

That means, I haven’t had much time to learn about compilers myself. Hopefully, this will prove useful foundation when I’ll decide to dive deeper.

Let’s roll

As long as I understand, a compiler consists of a series of steps that take some input code and transforms it into the target language.

As an input we will use a language that supports two operations (i.e. add and sub) on integers. In particular, we will use as an input the following code: add 1 sub 6 add 3 2.

Parsing

The first thing we want to do is to parse the code into an Abstract Syntax Tree (AST). In other words, a structure that enables us to work easily with the code. The AST for the example code provided above would look like

 add
  / \
  1  sub
     / \
    6  add
       / \
      3   2

The code to do that is the following

Generation / Evaluation

With the AST at our disposal we can either compile to some other language or evaluate the code:

Test Drive

Outro

If you want to read more, this is what inspired me to take a few steps into the compilers world:


If you liked the post and want to help spread the word, please make some noise 🤘 But only if you really liked it. Otherwise, please feel free to comment or tweet me with any suggestions or feedback. And please do cause I need help with my FP!