NAME
CodeGen::Cpppp - The C Perl-Powered Pre-Processor
VERSION
version 0.001_03
SYNOPSIS
Does that mean it's more powerful? ...Is it more powerful?
Well, it's one layer of abstraction higher, isn't it? It's not m4
. You see, most blokes gonna be templating with cpp
or m4
, you're on m4
here all the way up, all the way up, Where can you go from there? Where?
Nowhere! Exactly.
What we do is if we need that extra, push, over the cliff, you know what we do?
perl
, exactly.
These go to perl
.
Input:
#! /usr/bin/env cpppp
## for (my $bits= 8; $bits <= 16; $bits <<= 1) {
struct tree_node_$bits {
uint${bits}_t left: ${{$bits-1}},
color: 1,
right: ${{$bits-1}};
};
## }
Output:
struct tree_node_8 {
uint8_t left: 7,
right: 7,
color: 1;
};
struct tree_node_16 {
uint16_t left: 15,
right: 15,
color: 1;
};
Input:
## my @extra_args;
extern int fn( char *format, @extra_args );
## for ('int a', 'int b') {
## push @extra_args, $_;
extern int fn_$_( char *format, @extra_args );
## }
Output:
extern int fn( char *format );
extern int fn_a( char *format, int a );
extern int fn_b( char *format, int a, int b );
DESCRIPTION
WARNING: this API is completely and totally unstable.
This module is a preprocessor for C, or maybe more like a perl template engine that specializes in generating C code. Each input file gets translated to Perl in a way that declares a new OO class, and then you can create instances of that class with various parameters to generate your C output, or call methods on it like automatically generating headers or function prototypes.
For the end-user, there is a 'cpppp' command line tool that behaves much like the 'cpp' tool.
If you have an interest in this, contact me, because I could use help brainstorming ideas about how to accommodate the most possibilities, here.
Possible Future Features:
Scan existing headers to discover available macros, structs, and functions on the host.
Pass a list of headers through the real cpp and analyze the macro output.
Shell out to a compiler to find 'sizeof' information for structs.
Directly perform the work of inlining one function into another.
CONSTRUCTOR
Bare-bones for now, it accepts whatever hash values you hand to it.
METHODS
compile_template
$cpppp->compile_template($input_fh, $filename);
$cpppp->compile_template(\$scalar_tpl, $filename, $line_offset);
This reads the input file handle (or scalar-ref) and builds a new perl template class out of it (and dies if there are syntax errors in the template).
Yes, this 'eval's the input, and no, there are not any guards against malicious templates. But you run the same risk any time you run someone's './configure' script.
AUTHOR
Michael Conrad <mike@nrdvana.net>
COPYRIGHT AND LICENSE
This software is copyright (c) 2023 by Michael Conrad.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.