In my opinion, there is the best tutorial to make your own programming language compiler (in C++): My First Language Frontend.
The theory and practice of writing compilers is a whole world, which has been evolving for a few decades. Enthusiasts implement their own programming languages (here is an interesting collection), often alone.
The tutorial tells how to write a compiler for a simple Turing-complete language, which is capable to execute programs like this:
1
2
3
4
5
6
7
8
9
# Compute the x'th fibonacci number.
def fib(x)
if x < 3 then
1
else
fib(x-1)+fib(x-2)
# This expression will compute the 40th number.
fib(40)
The tutorial explains compilers’ pipeline and these topics:
- Lexical analysis - translating the file into tokens.
- Parsing - translating the tokens into an AST (Abstract Syntax Tree) with writing a simple LL(1)-parser.
- Code generation - translating the AST into the LLVM IR (intermediate representation); you’ll learn theory about SSA and control-flow graph.
- Code optimization - tweaking optimization passes (there is the list of the passes) and theory about it.
- Compilating the code into an object file.
- … and many other topics: debug-symbols, types, memory handling, standard library…
The C++ is ultimately hard in each of these topics (and has non-standard phases like the preprocessor stage 🤯), but with using a simpler language one can learn how compilers work and even how to create your own compiler.
I liked the most the possibility to link your language’s code with C++ code: I made a simple description on github.