An Incremental Approach to Compiler Construction

Compilers are perceived to be magical artifacts, carefully crafted
by the wizards, and unfathomable by the mere mortals.

This is from a great paper by Abdulaziz Ghuloum on how compilers are made. It, along with a timely email from Julian Rohrhuber inspired me to have a go at making a tiny one for compiling scheme to betablocker – the idea is that you only need a handful of scheme primitives before you are able to bootstrap all the other code together. Betablocker is no way powerful enough to go too far with this, but I think there may be some good uses for compiling bits of fluxus scheme code on Raspberry Pi and Android, maybe not to native binary but mixing an interpreter with a fast intermediate language for graphics processing would make it possible to compile object deformation inner loops i.e. (pdata-map!) and friends for per-vertex operations, but have it transparently the same language to the user/livecoder.

I’ve only dealt with fixnums, booleans, null lists and a handful of simple core procedures, but compiling something stupid like this:

(and
  (zero? (- (+ 12 4) (+ 43 (add1 34))))
  (boolean? #f))

Results in this:

compiled

Via the slightly more conventional looking:

   pshl 24
   pshl 8
   add
   pshl 86
   pshl 68
   pshl 2
   add
   add
   sub
   pshl 0
   equ
   jmpz label-1
   pshl 4
   jmp label-2
label-1:
   pshl 0
label-2:
   pshl 0
   pshl 3
   and
   pshl 3
   equ
   jmpz label-3
   pshl 4
   jmp label-4
label-3:
   pshl 0
label-4:
   and
   jmpz label-5
   pshl 4
   jmp label-6
label-5:
   pshl 0
label-6:

2 thoughts on “An Incremental Approach to Compiler Construction”

Leave a Reply

Your email address will not be published. Required fields are marked *