Making Languages

Part 6: Compilation

Christopher Pitt
Making Languages
Published in
2 min readOct 13, 2014

--

← Back | More coming soon…

Edit:

Anthony Ferrara points out that my wording is unclear here. Compilers convert code from one programming language to another.

It’s much easier to convert code from a high-level language to a fast, low-level language than it is to make that language. Perhaps you are tied into using a language like PHP, but you’d like to use a different syntax. You can do just that!

Compiling

Most of the work was done during parsing and interpretation. We just need to add a few render() methods to our constructs:

This is from Assignment.php

This is from Number.php

This is from Addition.php

That’s it! We no longer have to worry about calculating values — we’ve moved on to rendering valid PHP from our Abstract Syntax Tree:

Context

Even though we don’t use the $context; it will be useful later on. Context can store type data, as well as value data. This means we can type-check our code, even if the target language doesn’t support static typing.

← Back | More coming soon…

--

--