3 ms·
IMO one big advance would be writing the compiler in something other than C. C is a bad language for writing compilers in (including C compilers)! Even (cough
by chubot 9y ago
IMO one big advance would be writing the compiler in something other than C. C is a bad language for writing compilers in (including C compilers)!
Even (cough) C++ is better than C for this use case, e.g. comparing LLVM vs. GCC. But C++ has problems too though -- the TableGen language in LLVM is an interesting example of why a big C++ project like LLVM/Clang doesn't want all its logic/specification in C++. They really use C++ to the hilt and they still want/need a DSL.
Other interesting options would be Swift, Rust, OCaml, or Go. Swift, Rust, and OCaml all have algebraic data types.
I guess choosing something other than C or C++ is a little unusual because there is the tendency to self-host. The Rust, OCaml, and Go compilers are written in Rust, OCaml, and Go, respectively. I'm pretty sure Swift is a lot of C++, but that will probably change as times goes on.
- schwede 9y agoOne cool benefit of using C for a C compiler though is the ability to self host the compiler.
- qznc 9y agoI think C is fine for a simple compiler. Why are algebraic data types so relevant? Pattern matching is nice, because switch statements plus unpacking is more verbose. For big optimizing compilers you appreciate abstractions like C++ enables. for example, a data flow Analysis framework.
- jmite 9y agoAlgebraic data is just so perfectly suited to abstract syntax. It's clearly possible to model an AST in C, but not to do it in an ergonomic z typesafe way.
- ufo 9y agoIn addition to being more succinct, algebraic data types are also better about warning if you forgot to handle one of the cases.