Arvids Blog

Thoughts on programming and more

C Wishlist: Inline Functions

This is the first piece of a series of posts describing a few, very easily solvable, shortcomings of the C language. And proposing a way to fix these.
This is purely done the fact that I’m currently working on a C compiler. While I’ve written a few compilers over the years, none was actually intended to be used in production. This time it’s different!

I’m currently rewriting the expression parser of my compiler, to use the power of top down operator precedence parsing as described by Vaughan Pratt in his paper of the same name.

I’m defining token classes with structures, like this:

struct tok_class tok_add = { 10, add_nud, add_led };

when it glared me, that this could’ve been done a lot simpler, by declaring the function pointers inline:

struct tok_class tok_add = {
    .lbp = 10,
    .nud = ^int64_t(struct tok_info *t) {
       /* do work. */
       return 0;
    .led = ^int64_t(struct tok_info *t) {
       /* do work. */
       return 0;

I have chosen this syntax with a reason, since it resembles the already existing syntax for blocks. Which already are a C language extension, implemented in clang.

Unfortunately (or fortunately) blocks work a little different, they work like a std::function in C++. This would merely be syntactic sugar for a static function declaration of which the address is assigned to the function pointer.

Questions, criticism or just want to say hi? You can find me on Twitter @ArvidGerstmann.