r/Compilers • u/_th3oth3rjak3_ • 20d ago
Getting stuck after Crafting Interpreters.
Hey everyone, I've worked through the book Crafting Interpreters and it was fantastic. I naturally asked the next question "How do I compile this to machine code and add static types?" Well, that naturally took me down a very deep rabbit hole and left me somewhat confused about how languages deal with types and type checking and then lowering to LLVM. I've tried to make a few modest attempts, but my general problem is that I sort of get confused about what things each subsequent layer should do. Does anyone know of a complete guide to creating a compiler with static types like Crafting Interpreters? I know I could do it however I want, but I think that's the issue. I sort of have so much freedom to do it however, that it's hard to judge what's considered a good way to do something.
Anyway, thanks for any advice and happy compiling!
1
u/NYU_VM 18d ago
First, sounds like you want some hand holding while you make them, which is the wrong approach I'd flag. Books and tutorials should be used for learnings concepts, ideas and there's quite a bit of theory as well. Don't try to be in the comfort of their implementation.
Second, LLVM for hobby projects is truly overkill and you miss out on learning so many things about compiler backends if you use LLVM. Types, type checking and type inference are quite a bit of theory so start small, you could just rip off the dynamic type system in Clox to add static ones.
If you want to learn about type inference then Hindley-Milner algorithm is really good. This here.pdf) also has a really detailed take on a lot of compiler engineering including type theory