Name
Preprocess::Ops - Preprocess ▷ and ▶ as method dispatch operators in ANSI-C.
Synopsis
Preprocess ▷ and ▶ as method dispatch operators in ANSI-C by translating:
p = node ▶ key("a");
to:
p = node->proto->key(node, "a");
and:
p = tree ▷ root;
to:
p = tree.proto->root(tree);
Occurrences of the $ character are replaced by the base name of the containing file with the first letter capitalized, so that
typedef struct $Node $Node;
in a file called tree.c becomes:
typedef struct TreeNode TreeNode;
Occurrences of:
new XXX
are replaced by:
new XXX(({struct XXX t = {proto: &ProtoTypes_$1}; t;}))
Occurrences of:
new XXX(a:1)
are replaced by:
newXXX(({struct XXX t = {a:1, proto: &ProtoTypes_$1}; t;}))
The prototype vectors are generated by examining all the methods defined in the c file. The prototype vectors are written to the specified h file so that they can be included in the c file for use via the ▷ and ▶ operators.
See: https://github.com/philiprbrenan/C/blob/master/c/z/arenaRedBlackTree/arenaRedBlackTree.c for a working example.
Preprocessor commands
duplicate
The duplicate command generates the previous function with the changes indicated in the words following the command as in:
static char * key_$Node // Get the key for a node
(const $Node n) // Node
{return n.key;
}
duplicate s/key/data/g
which adds the following code to the current output file:
static char * data_$Node // Get the data for a node
(const $Node n) // Node
{return n.data;
}
exports
The exports command provides a name for or a collection of functions that can be included in generated output files, for instance:
exports aaa new$Node key_$Node
creates a new set of exports called aaa which contains the two functions mentioned. As these names have $ in them they will be expanded with the base name of the file into which they are being copied.
include
The include command copies the named function, structures, and exported collections from the specified file into the current output file. For instance:
include ../arenaTree.c :arena !key_$Node data_$Node
reads the relative file ../arenaTree.c and copies in all the structures mentioned in collection arena except for key_$node as well as copying the explicitly mentioned function data_$Node.
Description
Preprocess ▷ and ▶ as method dispatch operators in ANSI-C.
Version 202009010.
The following sections describe the methods in each functional area of this module. For an alphabetic listing of all methods by name see Index.
Preprocess
Preprocess ▷ and ▶ as method dispatch operators.
printData($lineNumber, $line)
Print statement
Parameter Description
1 $lineNumber Code line number
2 $line Code line
c($inputFile, $cFile, $hFile, $column)
Preprocess ▷ and ▶ as method dispatch operators in ANSI-C.
Parameter Description
1 $inputFile Input file
2 $cFile C output file
3 $hFile H output file
4 $column Optional start column for comments (80)
Example:
my $I = fpd($d, qw(includes));
my $sbc = owf(fpe($d, qw(source base c)), <<'END'); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
exports aaa new$Node key_$Node
typedef struct $Node // Node definition
{char * key; // Key
} $Node;
static char * key_$Node // Get the key for a node
(const $Node n) // Node
{return n.key;
}
duplicate s/key/data/
static void dump_$Node // Dump a node
(const $Node n)
{printf("%s", n ▷ key);
}
$Node n = new$Node(key: "a"); //TnewNode
assert(!strcmp(n ▷ key, "a"));
n ▷ dump; //Tdump
END
my $sdc = owf(fpe($d, qw(source derived c)), <<'END'); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
typedef struct $Node // Node definition
{wchar * key;
} $Node;
include base.c :aaa dump_$Node # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
END
my $bc = fpe($I, qw(base c)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
my $bh = fpe($I, qw(base h));
my $dc = fpe($I, qw(derived c)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
my $dh = fpe($I, qw(derived h));
my $r = c($sbc, $bc, $bh); # Preprocess base.c # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
# owf($logFile, readCFile($bc)); exit;
is_deeply readCFile($bc), <<'END'; # Generated base.c # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
typedef struct BaseNode // Node definition
{char * key; // Key
} BaseNode;
static char * key_BaseNode // Get the key for a node
(const BaseNode n) // Node
{return n.key;
}
static char * data_BaseNode // Get the key for a node
(const BaseNode n) // Node
{return n.data;
}
static void dump_BaseNode // Dump a node
(const BaseNode n)
{printf("%s", n.proto->key(n));
}
BaseNode n = newBaseNode(({struct BaseNode t = {key: "a", proto: &ProtoTypes_BaseNode}; t;})); //TnewNode
assert(!strcmp(n.proto->key(n), "a"));
n.proto->dump(n); //Tdump
END
# owf($logFile, readCFile($bh)); exit;
is_deeply readCFile($bh), <<END; # Generated base.h
static char * key_BaseNode
(const BaseNode n);
static char * data_BaseNode
(const BaseNode n);
static void dump_BaseNode
(const BaseNode n)
{printf("%s", n ▷ key);
}
BaseNode n = newBaseNode(key: "a");
assert(!strcmp(n ▷ key, "a"));
n ▷ dump;;
struct ProtoTypes_BaseNode {
char * (*data)( // Get the key for a node
const BaseNode n); // Node
char * (*key)( // Get the key for a node
const BaseNode n); // Node
} const ProtoTypes_BaseNode =
{data_BaseNode, key_BaseNode};
BaseNode newBaseNode(BaseNode allocator) {return allocator;}
END
my $R = c($sdc, $dc, $dh); # Preprocess derived.c # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
# owf($logFile, readCFile($dc)); exit;
is_deeply readCFile($dc), <<'END';
typedef struct DerivedNode // Node definition
{wchar * key;
} DerivedNode;
static char * key_DerivedNode //I Get the key for a node
(const DerivedNode n) // Node
{return n.key;
}
static void dump_DerivedNode //I Dump a node
(const DerivedNode n)
{printf("%s", n.proto->key(n));
}
END
# owf($logFile, readCFile($dh)); exit;
is_deeply readCFile($dh), <<'END';
static char * key_DerivedNode
(const DerivedNode n);
static void dump_DerivedNode
(const DerivedNode n)
{printf("%s", n ▷ key);
}
;
struct ProtoTypes_DerivedNode {
char * (*key)( // Get the key for a node
const DerivedNode n); // Node
} const ProtoTypes_DerivedNode =
{key_DerivedNode};
DerivedNode newDerivedNode(DerivedNode allocator) {return allocator;}
END
# owf($logFile, dump(unbless $r)); exit;
is_deeply $r,
{
methods => {
data_BaseNode => {
comment => "Get the key for a node",
flags => {},
name => "data",
parameters => [["const BaseNode", "n", "Node"]],
return => "char * ",
structure => "const BaseNode",
},
dump_BaseNode => {
comment => "Dump a node",
flags => {},
name => "dump",
parameters => [[]],
return => "void ",
structure => undef,
},
key_BaseNode => {
comment => "Get the key for a node",
flags => {},
name => "key",
parameters => [["const BaseNode", "n", "Node"]],
return => "char * ",
structure => "const BaseNode",
},
},
structureParameters => { BaseNode => { data_BaseNode => 1, key_BaseNode => 1 } },
structures => {
BaseNode => { comment => "Node definition", flags => "", name => "BaseNode" },
},
testsFound => { dump => 1, newNode => 1 },
testsNeeded => { data => 1, key => 1 },
};
# owf($logFile, dump(unbless $R)); exit;
is_deeply $R,
{
methods => {
dump_DerivedNode => {
comment => "Dump a node",
flags => { I => 1 },
name => "dump",
parameters => [[]],
return => "void ",
structure => undef,
},
key_DerivedNode => {
comment => "Get the key for a node",
flags => { I => 1 },
name => "key",
parameters => [["const DerivedNode", "n", "Node"]],
return => "char * ",
structure => "const DerivedNode",
},
},
structureParameters => { DerivedNode => { key_DerivedNode => 1 } },
structures => {
DerivedNode => { comment => "Node definition", flags => "", name => "DerivedNode" },
},
testsFound => {},
testsNeeded => {},
};
}
clearFolder($d, 10);
done_testing;
if ($localTest)
{say "TO finished in ", (time() - $startTime), " seconds";
}
PreprocessOpsMap Definition
Methods and structures in the C file being preprocessed
Output fields
methods
Methods.
structures
Structure definitions.
PreprocessOpsParse Definition
Structure of the C program being preprocessed
Output fields
methods
Methods.
structureParameters
Structures used as parameters
structures
Structure definitions.
testsFound
Tests found
testsNeeded
Tests still needed
PreprocessOpsStruct Definition
Structure declaration
Output fields
comment
Comment for structure
flags
Flags for structure
methods
Methods.
name
Name of structure
structureParameters
Structures used as parameters
structures
Structure definitions.
testsFound
Tests found
testsNeeded
Tests still needed
Private Methods
trim($s)
Remove trailing white space and comment
Parameter Description
1 $s String
method($line)
Check whether a line of C code defines a method, returning (return, name, flags, comment) if it is, else ()
Parameter Description
1 $line Line of C code
structure($line)
Check whether a line of C code defines a structure, returning (name, flags, comment) if it is, else ()
Parameter Description
1 $line Line of C code
mapCode($file)
Find the structures and methods defined in a file
Parameter Description
1 $file Input file
duplicateFunction($lineNumber, $inputFile, $code)
Duplicate the previous function with the specified changes applied
Parameter Description
1 $lineNumber Line number of line being expanded
2 $inputFile File containing line being expanded
3 $code Lines of code
includeFile($lineNumber, $inputFile, $cFile, $hFile, $code)
Expand include files so that we can pull in code and structures from other files in the includes folder.
Parameter Description
1 $lineNumber Line number of line being expanded
2 $inputFile File containing line being expanded
3 $cFile Output C file
4 $hFile Output H file
5 $code Line of code
Index
1 c - Preprocess ▷ and ▶ as method dispatch operators in ANSI-C.
2 duplicateFunction - Duplicate the previous function with the specified changes applied
3 includeFile - Expand include files so that we can pull in code and structures from other files in the includes folder.
4 mapCode - Find the structures and methods defined in a file
5 method - Check whether a line of C code defines a method, returning (return, name, flags, comment) if it is, else ()
6 printData - Print statement
7 structure - Check whether a line of C code defines a structure, returning (name, flags, comment) if it is, else ()
8 trim - Remove trailing white space and comment
Installation
This module is written in 100% Pure Perl and, thus, it is easy to read, comprehend, use, modify and install via cpan:
sudo cpan install Preprocess::Ops
Author
Copyright
Copyright (c) 2016-2019 Philip R Brenan.
This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.