I compiled my first compiler today 🕺
I stumbled on the S.I.M.P.L.E programming language yesterday and I found myself excited and intrigued.
I felt the child-like, adventurous me stir in his sweet, sweet slumber and I think he may just be awake.
What that will bring, we are yet to see.
I decided I must try this out on my computer and asked its creator, Azeez Adewale, for help with this.
However, only a windows build was available, so I was supposed to build the compiler source code for macOS.
I hadn’t used the C programming language since 100 level “Introduction to Computer Science” class when Mr. J. made it a complete bore, so I was ecstatic about this.
How it’s done
Compiling C code is done with the gcc
command traditionally, and clang
on recent macOS versions.
Because there were multiple files to be compiled, I had to use a Makefile
, which, in hindsight, is actually much easier than it sounds.
This tutorial made me confident that I could do it, and this other one helped me light the way. 🔥
What I eventually did
In the end, I found I had to place the Makefile
in the same directory as the *.c
files, which makes sense, because the *.c
files reference the *.h
files.
So, I ended up with this:
Hehehe 😝 … from zero to warrisdis?
This creates user-defined constants at the top of the file like program_NAME
and adds values to special constants like CPPFLAGS
.
It then builds the output for each *.c
file into an equivalent *.o
output file in the same directory, then somehow bundles all the *.o
files into a dynamic linked library called simple.dylib
.
The simple.dylib
is similar to a DLL file used in windows programs, so it’s a library which needs an executable.
Azeez provided one called simple.c
:
#include "./includes/simple.h"
int main(int argc, char** argv) {
simple_state_main(argc,argv);
return 0;
}
This is done by line 21 in the Makefile
above, which outputs an executable file called a.out
The remaining lines in the Makefile
perform cleanup operations like deleting all *.o
files, and moving simple.dylib
and a.out
to the already createddist
directory.