7 ms·
OCaml? Thanks for saving me a click!
by Croftengea 2y ago
OCaml? Thanks for saving me a click!
- hdbxbxndj 2y agoOCaml is one of the most used languages for compiler design A good engineer should be able to use the right tool for the job
- materielle 2y agoFor hobbyist compiler implementations, right? Compilers for the most popular languages are either written in C/C++, or self-hosted. You can write compilers in almost any language. I fail to see how C, C++, or even Java or Python aren’t the right tool for the job here. I like pattern matching too, but given that hundreds of successful production compilers have been written without pattern matching, it’s surely just a personal preference.
- porcoda 2y agoI’ve worked on multiple compilers in industry that are written in Ocaml. A number of industrial static analyzers are written in Ocaml too (eg, Infer from Facebook/Meta). Yes, LLVM and GCC are the big ones written in the C/C++ family but they don’t represent everything.
- materielle 2y agoAnd the Go, Java, Ruby, JavaScript, C#, Typescript, PHP, Kotlin, R compilers, and so on. But even for hobby projects, it’s just a matter of personal preference. OCaml is great for implementing compilers. So are Go, C++, and Java.
- jsnnsjxj 2y agoI mean, we _are_ talking about a book which invites you to build your own toy C compiler ^^ Nevertheless, OCaml is very strong in compiler design. For example Rust and Hack were written in OCaml initially. Nevertheless you are not wrong that compilers needing the very last bit of performance like the JVM and LLVM tend to be written in C++ But the barrier is quite a lot more tending to high performance/very high performance and not toy/production Java and Python are suitable for implementing a toy Compiler and the auther invites you to use any language you like. Just the reference implementation is using OCaml I would however argue that using C++ is quite advanced since it does not have pattern matching and using C is just masochm. You will be fighting against the language to do even trivial things instead of fighting the actual problem at hand
- materielle 2y agoI totally agree that OCaml is a great language to write a compiler. I’ve used Rust and Haskell, and loved them both. I was more so pushing back on the the implication that if it’s not OCaml, it’s not the right tool for the job. Like, I honestly can’t think of a mainstream language in which it would be hard to implement a C compiler in.
- wang_li 2y agoA Retargetable C Compiler is another book that implements a C compiler in C. https://www.amazon.com/Retargetable-Compiler-Design-Implementation/dp/0805316701 https://www.amazon.com/Retargetable-Compiler-Design-Implemen...