Live data from Hacker News

Writing a C Compiler: Build a Real Programming Language from Scratch

nostarch.com

131–140 of 159 posts

Re: Writing a C Compiler: Build a Real Programming Language from Scratch

#131
post #122

Earlier quoted context omitted.

> it 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 1. I am developing on windows so that's an issue for me and 2. I don't use emacs, I use VScode and I've not been able to get the experimental debugger working for the VScode plugin…

"... I think FP programmers seem to have a distaste for debuggers ..." I'm unsure that I'd make this assertion so broadly. For Haskell, I'm usually left using Debug.Trace because I've found the traditional symbolic-step debugger is less than ergonomic in the face of lazy evaluation. In Common Lisp, the debugger is my best friend. "... and graphical debugging/development environments" The loud ones might be saying the…

mine too! except, not so much of a closed-world system? a lot of what i like about emacs is that it has some of that same live-malleability that smalltalk and oberon have

if you've tried godot i'm interested to hear what you think about it

Re: Writing a C Compiler: Build a Real Programming Language from Scratch

#132
post #113

Earlier quoted context omitted.

there are new items in this category every day, but https://blog.cr.yp.to/20240803-clang.html is noteworthy

Eh https://news.ycombinator.com/item?id=41146860

yes, that

Re: Writing a C Compiler: Build a Real Programming Language from Scratch

#133
What approach does this book take to error recovery?

Several "compiler light" style articles and books kind of walk over that part, and it can be non-trivial to do properly, especially with modern expectations.

I remember way back in the day, one of the early C compilers for the PDP, and, honestly, it could almost be argued that ed(1) had better error messages than what that thing produced.

A lot of simple compilers hit an error and just give up.

So, just curious what the approach was in this book.

Re: Writing a C Compiler: Build a Real Programming Language from Scratch

#134

Earlier quoted context omitted.

For 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…

I 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 perfo…

I 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.

Re: Writing a C Compiler: Build a Real Programming Language from Scratch

#135
post #67

Earlier quoted context omitted.

For 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…

I’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.

And 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.

Re: Writing a C Compiler: Build a Real Programming Language from Scratch

#136
post #131
post #122

Earlier quoted context omitted.

"... I think FP programmers seem to have a distaste for debuggers ..." I'm unsure that I'd make this assertion so broadly. For Haskell, I'm usually left using Debug.Trace because I've found the traditional symbolic-step debugger is less than ergonomic in the face of lazy evaluation. In Common Lisp, the debugger is my best friend. "... and graphical debugging/development environments" The loud ones might be saying the…

mine too! except, not so much of a closed-world system? a lot of what i like about emacs is that it has some of that same live-malleability that smalltalk and oberon have if you've tried godot i'm interested to hear what you think about it

Unfortunately, I have no idea what godot is. I would like to know more though if you're so inclined.

(Surely not the game engine? That's the only thing my disambiguation machinery can come up with.)

Re: Writing a C Compiler: Build a Real Programming Language from Scratch

#137

Earlier quoted context omitted.

Yeah, Rust is the language for people who think C++ is not complex (or hostile) _enough_.

When I tried to read some rust, I was surprised on how much alien it is to mainstream languages and how convoluted the syntax is.

My experience (and I admit I may be too biased given years of prior C/C++ experience) is that Rust's syntax is a necessity, since no other mainstream languages besides C/C++ are as low-level as Rust.

Most mainstream languages have a GC, and don't support distinguishing between values on the stack or references, don't need to deal with lifetimes or don't provide the safety you get with them, etc.

I'm curious though, could you give an example of syntax you consider convoluted, and how you would do it instead?

Re: Writing a C Compiler: Build a Real Programming Language from Scratch

#138
post #123

I took a compilers course in university and the course culminated in having a compiler for C Minus (a subset of C). The professor noted how each year the line count of the compilers was dropping as students found ways libraries or languages that made it easier. I think the evolution was Java -> Antlr -> Python. I used OCaml and emitted LLVM and blew that metric out of the water.

Blew it out of the water with more or less lines of code? :)

Far fewer, to the point of another student asking me what I even did for the project because I didn't have to implement any of the algorithms.

Re: Writing a C Compiler: Build a Real Programming Language from Scratch

#139

I learned how to write a compiler by studying BYTE magazine in the 70's which published the source to a complete Pascal compiler as an article! https://archive.org/details/byte-magazine-1978-09 (part 1) All 3 parts of Tiny Pascal: https://albillo.hpcalc.org/publications/Easter%20Egg%20-%20T...

Thank you for sharing this, very useful. The BYTE magazine is absolutely amazing, it's a shame nothing similar could be done today.

Re: Writing a C Compiler: Build a Real Programming Language from Scratch

#140

Earlier quoted context omitted.

When I tried to read some rust, I was surprised on how much alien it is to mainstream languages and how convoluted the syntax is.

My experience (and I admit I may be too biased given years of prior C/C++ experience) is that Rust's syntax is a necessity, since no other mainstream languages besides C/C++ are as low-level as Rust. Most mainstream languages have a GC, and don't support distinguishing between values on the stack or references, don't need to deal with lifetimes or don't provide the safety you get with them, etc. I'm curious though, c…

How do you want to be taken seriously if you don't see the convolution of the syntax of c++ and rust? You are going against an absolute truth.
Post reply on HN