From today and onward I can quite confidently say that Rust is the final implementation language of the Bolt programming language.
After years of trying out different programming languages (from Haskell and OCaml to TypeScript) and ideas (do I want an ML-style syntax or C-like?), I am very happy with how these experiments are converging. In this search Rust plays a pivotal role.
Curious? Here's why.
Losless syntax trees using Rowan
Real editors not only type-check your code; they also might analyse and transform whitespace, formatting your code according to a certain set of rules.
Above that, designing syntax trees in Rust can be quite a pain due to the way borrowing and ownership works. For one, it is very difficult to get a simple and safe pointer to the parent node in Rust.
As opposed to abstract syntax trees, losless syntax trees keep track of this whitespace for you. The Rowan libary provides a means for defining such syntax trees. It takes care of ownership and even thread-safety for you, so you can focus on things like type-checking.
Rowan is based off the concept of red-green trees, where the green nodes contain the data and the red nodes are simple 'views' on this data. The red nodes are constructed whenever the program needs a node of a certain type, while the green nodes are usually constructed during parsing.
Robust query engine with Salsa
Another Rust crate that makes your life as a compiler writer that much easier is Salsa. It is difficult to work with at first, but once you get the hang of it, it will speed up the development of your compiler tremendously.
Salsa is a tool to build an incremental query-based compiler. With Salsa, there are a few top-level queries one can make about the inputs (such as some programming code written in your language), which get split up in smaller queries. These queries trigger even more queries, all the way down until an answer is formed. The answer then is cached, so that if only a single bit of your inputs change, not everything has to be re-computed from scratch.
The amazing thing is that you as a compiler designer need not to take into account the million ways of which an input might be changed. Salsa takes care of it. Brilliant!
I highly recommend to read this article from Olle Fredriksson and the official Salsa documentation to learn more!
Beautiful error messages with Ariadne
Ariadne is a Rust crate that does all of the dirty work for you when all you want is print a diagnostic message to the terminal. It creates beautiful and very readable error messages, pointing exactly to the location in your source file where the error occurred.
Conclusion
Thanks to the useful Rust libraries out there, writing a compiler has never been so easy. I will continue to work on my programming language called Bolt using this blueprint. I hope that you might consider it, too!


Top comments (0)