2023-07-24

Experimental

The other day I happened upon Heb Sutter's closing talk from CppCon 2022, about cppfront, his experimental attempt to provide a new syntax and set of defaults for C++.

Intriguing, I thought. If only I had a not very important project to try that out on. Well, that and some time, of course.

I've also been reading—but not working—Crafting Interpreters by Robert Nystrom.1 Up to a point not working it was going just fine because this is not my first exposure to the art of compilers and I could see what he was doing because I understood why.2 But last week, around section 25.1, I started to feel that I was losing the thread in pretty significant ways.

A marriage made in ... well, some other plane of existance, I'm sure.

I give you cpp2lox. Or at least the raw beginnings of it.

Observation: It's easy to forget how many nice things a good development environment does for you until you have to do without. Right now I have no auto-formatting and no syntax highlighting for cpp2, much less IDE support behaviors like completions, on-the-fly static analysis, etc. And it shows in the condition of the code. Well, you've been warned.

Observation the second: It's also surprisingly hard to notice violations of expectations in the experimental syntax that I'd have noticed pretty quickly in the official syntax. Trying to populate a std::vector of a user defined type was throwing some very unclear and mysterious errors that kept me confused for far longer than it should have given that the problem was simply that I hadn't defined a copy constructor for the type.


1 I highly recomend the book which is available for free online (and you should read part of it that way to get a sense of the book's mixture on technical clarity and humor), but I recommend buying it if your financial circumstances allow. It goes through two complete builds, line-by-line. The first is implemented in Java, and parses the source to an abstract-syntax-tree the walks the tree to interpret the source. The second one is done is plain ol' C and eschews the AST in favor of generating a bytecode for a custom stack machine, which it implments to run the bytecode. I'm not done with the second half yet, and still have the closure implementation and the garbage collector ahead of me. The interesting thing to me is that despitre the title the book gives you all the skills to build a source-to-metal compiler if you are so inclined.

2 Mind you my main prior exposure was working through Jack Crenshaw's series of articles Let's Build a Compiler which does a single-pass, immediate-code-generation, recursive-descent job on a Pascal-like language targets at m68k assembly.

No comments:

Post a Comment