For those developers who do both C and Perl, and frequently run into the upper limit of the C preprocessor, I have a treat for you!
CodeGen::Cpppp
It's still a little rough around the edges, and could use lots more features, but I think it's reached a point of usability where it's worth sharing.
Re: Introducing the C Perl-Powered Pre-Processor
by jdporter (Paladin) on Nov 09, 2023 at 15:38 UTC
|
| [reply] |
|
I do need better examples, and a "cookbook" document. The SYNOPSIS is actually fairly complete, showing how all perl code goes into ## directives, and any lines without ## are treated as a template, and how you can use perl string interpolation (with a few extensions) throughout the template. Aside from that, you can do anything with it that Perl can do! It's really just like writing a perl script with C code in here-docs, but with more convenient interpolation of perl variables, and automatic formatting so that the output looks better, and behind-the-scenes magic so that it compiles into a module instead of a one-shot script. Plus, the command line tool that more conveniently populates your parameters and directs your output into multiple files.
My main use right now is an un-finished C library for doing red/black hash tables, which requires a lot of repetitive code compiled for different bit-widths. I also started an example of how you could implement the C equivalent of std::vector (which is in the examples directory) but it isn't finished or presentable yet.
| [reply] |
Re: Introducing the C Perl-Powered Pre-Processor
by etj (Deacon) on May 12, 2024 at 20:02 UTC
|
Love the Spinal Tap reference in the doc!
With EUMM, you can already do this with PL_FILES. And really, using Perl to generate C is PDL's whole thing, so you could use that? | [reply] [d/l] |
|
C code is just text, so there are infinite ways to use Perl (or any other language with a print statement) to generate C code. The point of cpppp is to have templates that are fairly readable as C code, and output that is good enough to commit to a repo, and a collection of tools that meet common needs in authoring C. These things can be fairly awkward to do with plain old print statements, or even Template Toolkit. And, maybe more to the point, they're painful to do with C's own preprocessor, which was designed specifically for this role, and especially painful to handle with the C++ template system, which was designed to make up for the shortcomings of the preprocessor and yet still falls short, in my opinion.
Now, sometimes it looks OK to just use here-docs. But with more advanced cases it can get rather ugly. It can even get ugly just using C's preprocessor in a pure C project. I haven't gotten around to rewriting those in cpppp yet, but I assure you it will be more readable.
If you have examples of PDL making life easier in this manner, I'd be interested to see them.
| [reply] |
|
That "rather ugly" one looks actually quite neat!
I was over-egging the pudding a bit, for humorous(?) effect. Within the PDL-o-sphere, its DSL makes it quite close to fun to write n-dimensional loops. It has some slightly deranged, borderline "cpp abuse" levels of macro to generate per-supported-type code within proper C code (PDL_GENERICSWITCH et al if you're interested), and PDL has infrastructure to generate one version of given "code" per supported type.
The above results in something a bit like, but more deranged and worse than, https://github.com/rofl0r/order-pp/blob/master/example/array_ops.c
I keep thinking that there are various parts of PDL that would work as standalone parts, and the type-generic stuff is one. Probably a different direction from what your tool enables.
| [reply] |
|
|
Re: Introducing the C Perl-Powered Pre-Processor
by Danny (Pilgrim) on May 13, 2024 at 01:09 UTC
|
So this is for converting Perl code into C code? | [reply] |
|
It's for authoring C code, using perl. i.e. you have to know C and know what C code you wanted to generate (or a pattern of C code that you want to generate variations of), and then use Perl to get there instead of typing it all out by hand.
| [reply] |
|
I see. So like in your example, you might, for example, have thousands of structures with different variations that you want to generate ...
| [reply] |
|
|