Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Introducing the C Perl-Powered Pre-Processor

by NERDVANA (Curate)
on Nov 09, 2023 at 07:03 UTC ( [id://11155495]=CUFP: print w/replies, xml ) Need Help??

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.

  • Comment on Introducing the C Perl-Powered Pre-Processor

Replies are listed 'Best First'.
Re: Introducing the C Perl-Powered Pre-Processor
by jdporter (Paladin) on Nov 09, 2023 at 15:38 UTC

    I'd like to see some examples of what it's actually capable of.

      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.

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?

      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.

        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.

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?
      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.
        I see. So like in your example, you might, for example, have thousands of structures with different variations that you want to generate ...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://11155495]
Approved by marto
Front-paged by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-05-19 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found