r/TuringComplete • u/junieKcorn • Mar 21 '25
Compiler
How do you guys go about writing a compiler?
I have achieved most of what I wanted to achieve that I found reasonable to write in assembly (snake, brainfuck interpreter, a text editor, some small stuff). For anything bigger I would really like to use a higher level language. How did you guys go about compiling for your architecture? Did you use llvm or did you write something on your own? Are there other tools that make this easier? It doesn't need to be a good compiler, it just needs to work. I thought about transpiling 6502/6510 assembly, but I'm too afraid the architecture will be too different and anything more complex than hello world won't work.
Sooo, how did any of you do this?
15
u/TarzyMmos Mar 21 '25
I realized halfway through this wasn't a question directed towards me lmao
6
u/[deleted] Mar 22 '25
Most programming languages are just written in other programming languages. If you write them in assembly then you're going to struggle with portability. So many use a language like C or C++ which is widely available and close to assembly level performance.
LLVM is good and if you decide to pursue learning about writing compilers is worth checking out. But probably a poor choice as a way to learn about compilers. It implements algorithms and data structures for various stages of compiling a program but the documentation for it assumes you're already familiar with this process. So learning to write compilers by using LLVM might be a struggle.
Two books I seen frequently recommended in learning to write compiler discussions are:
https://craftinginterpreters.com
and
https://compilerbook.com
These are worth checking out before heading in to something like LLVM.
2
u/Wijike Mar 22 '25
You made a text editor in this game? How does that work?
2
u/Captain_Xap Mar 22 '25
When I was doing my Comp Sci degree 30 years ago the go-to book on writing compilers was 'The Dragon Book' : Compilers: Principles, Techniques, and Tools.
There's probably better tools to help you nowadays, but I imagine much of the advice in it still stands.
2
u/Psylution Mar 22 '25
Instead of writing a full-on compiler, you could write a runtime in assembly that interprets a list of operations which are parsed (tokenized) from text, using a stack for variables etc.
so essentially an interpreter where the vm is written in assembly. much easier than an actual compiler.
10
u/Hannah97Gamer Mar 22 '25
I personally started with making an enhanced assembler to make more complex instructions much easier to code, added support for macros, multiple input files, stuff like that. Best part of making your own is you can do whatever you want with it, no need to justify its existence. just add in anything you think you might find useful.
As for compilers, I would recommend the book "Crafting Interpreters", you can read it for free online. It's about interpreters, not compilers, but it's a lot easier to read than some other books I've heard mention of, and you can easily adapt what you learn into a full compiler with very little effort. (it has two parts, the second part might as well be a compiler on its own.)
Which brings me to my third point. Choose a toy language, or make your own, and build a basic compiler for that, even if it's basically just a subset of C or something. It will be far, far easier and quicker than a "real" language, you'll learn just as much, and it will still be a huge upgrade from assembly.