AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
In expansion of macro in c9/11/2023 ![]() ![]() Especially if you get to use variadic macros in ways that they were never intended to be used.^ That is something you will know after spending an afternoon learning C, but there is a bit more to macros and the rules for how they expand, and while pure text substitution isn’t much of a programming language, you can do some meta-programming. It is annoying at times, but this can also allow you to manipulate parts of expressions if you so desire, so there is some sense to it. # define expr(x) x expr(( struct foo), and not a single token. You use #define to, well, define a macro, You haven’t used C for long before you learn how to define and use macros. ![]() Ph’nglui mglw’nafh Cthulhu R’lyeh wgah’nagl fhtagn! Basic macro expansion ![]() Before I gave up, however, I went deep down the rabbit hole of macro meta-programming, and it is now my intent to drag you down with me. I gave up (but I will tell you about the solution I ended up with some other time it isn’t bad, just not quite what I wanted). I wasted a lot of time not quite getting there-the final realisation that I wouldn’t make it was that I needed to generate macros, and you cannot do that in other macros. Except that there is some redundancy in what code I must generate from macros to make it work. And it did I have a solution I am reasonably satisfied with. So, I figured macros would get me most of the way, with _Generic() expressions for static type dispatch. C doesn’t do generic types, and there are a couple of places where you need to provide type information. Nothing in the source code really changes, just the type information. That varies, and thus so does the return type of indexing, some initialisation code (where the user provides the buffer that must be of the correct type) and a few other things. They all behave exactly the same, even with the same code, except for the type of the underlying buffer. The only thing that makes it difficult is that I need slices of different types. You need a pointer to an underlying buffer and a length (and maybe a capacity), and that’s that. There is nothing complicated in implementing such slices. Essentially arrays, but where I can index from the end using negative numbers (like in Python) and where I can extract a sub-slice, x, in constant time (like in Go I implement them the same way as Go does). I’ve been working on a small C library for Python- or Go-like slices the last couple of weeks. On Writing, Science, Programming and more ![]()
0 Comments
Read More
Leave a Reply. |