4 ms·
Weird that this is about building a C compiler[0] in OCaml. I expected the implementation language to also be C both for consistency but also because i'm willin
by badsectoracula 2y ago
Weird that this is about building a C compiler[0] in OCaml. I expected the implementation language to also be C both for consistency but also because i'm willing to bet that there are more people who can read C than OCaml.
[0] actually from the readme in the github repo[1] it seems to be a C subset, not all of C
[1] https://github.com/nlsandler/nqcc2 https://github.com/nlsandler/nqcc2
- Croftengea 2y agoOCaml? 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...
- kragen 2y agoocaml makes writing a compiler enormously more accessible, and learning to read ocaml, while it can be somewhat intimidating at first, is much easier than learning to write a compiler (imagine a medieval accountant trying to learn to do long division in roman numerals. he'll be much better off learning the western arabic numerals fibonacci is so excited about)
- shortrounddev2 2y agoI really really really want to get more into Ocaml but as far as I know there is no good support for a debugger to use in an IDE like VSCode or even vim. Everyone I talk to says they just do print debugging. It's easier to 'reason about your code' in FP, but I do NOT want to go back to a time where I coded without breakpoints. I use F# because of its first party tooling support
- kragen 2y agoit does have a debugger with breakpoints, which even supports time-travel debugging (except on windows obviously), but i've never used it. it even has first-party ide integration: https://ocaml.org/manual/5.2/debugger.html#s:inf-debugger https://ocaml.org/manual/5.2/debugger.html#s:inf-debugger i use debuggers a lot when i'm programming in assembly, and from time to time when i'm programming in c or c++, but in ocaml i've never needed one. it's not that i've never had bugs in ocaml, but they tend to be of a different flavor, a flavor for which breakpoints are of little value it sounds like your f# experience is different; what kinds of bugs have you recently found the debugger valuable for in f#? debug logging is usually a superior option to breakpoint debugging for problems to which both are applicable, because debug logging shows you the whole history of your program's execution rather than just a single point in time. breakpoint debugging requires a lot of manual labor, painstakingly operating the machine to navigate the execution to the state that has the problem. it's like grinding in a video game. i'd rather program the computer to do that labor for me, at which point i no longer need the debugger (except in c and assembly)
- shortrounddev2 2y ago
- userbinator 2y agoThe bonus of writing a C compiler in C is that you get to being able to experiment with self-compilation.
- anta40 2y agoWritten in OCaml? Ahh interesting. Suddenly I remember similar book: Modern Compiler Implementation in ML: https://www.cs.princeton.edu/~appel/modern/ml/ https://www.cs.princeton.edu/~appel/modern/ml/ As an undergrad student, I think the C version is kinda easier to understand, though.
- norir 2y agoThere are many disadvantages to writing a compiler in c (and I have done it). For me, the biggest is simply that it is very verbose for the type of things that you do commonly in a compiler. I have written a self-hosting compiler that transpiles to c and the generated code is about 5x as long as the original code. I could go through and clean it up and probably get it down to 2-3x, but there are certain concepts that cannot easily be expressed compactly in c (except possibly with preprocessor abuse that I don't have patience for). A simple example is that if you want to represent a union type you have to manually define 2 structs and an enum. Matching with switch is also verbose as you have to both match and manually assign the value in the match case. Again, you can use macros to do this though at that point you arguably aren't even using c but rather a hybrid language. A language like ocaml, makes it much more straightforward to define and match on unions as well as gives you other nice high level things like gc so you can focus on the compiler itself and not low level details.