The plan

Our journey is made of 4 stations - each of them depending on the previous ones:

  1. The tokenizer (aka “Lexical Analysis”): converting an input code - in LISP syntax - into an array of tokens.
  2. The parser (aka “Syntactic Analysis”): transforming an array of tokens into an Abstract Syntax Tree (AST).
  3. The emitter (aka “Code Generation”): string-ifying an AST into C-like code.
  4. The compiler (aka “You made it”): combining all the pieces together.

(The interactive code snippets are powered by a tool of mine named KLIPSE.)

The last station: the compiler

The last station is only fun - lot of fun!!!

Let’s write our last piece of code - by assembling the tokenizer, the parser and the emitter into a single my_compiler function:

my_compiler = input => {
  let tokens = tokenizer(input);
  let ast    = parser(tokens);
  let output = emitter(ast);

  return output;
}

And let’s test it…

my_compiler("(add 1 2 (mult 3 4))")

Enjoy the moment, play with your compiler…

Congratulations - I mean you did it. Give yourself a huge hug, buy yourself a gift. I don’t know… Find the most appropriate way to celebrate your success… You truly deserve it!

ast

One last thing: the whole code is accessible as a single file on github: it’s around 150 line of codes.