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:
newXXX
are replaced by:
newXXX(({proto: &Prototypes_XXX}})
The prototype vectors are generated by examining all the methods defined in the Cfile and written to the specified h file so that they can be used via the ▷ and ▶ operators.
See: https://github.com/philiprbrenan/C/blob/master/c/z/xml/xml.c for a working example.
Description
Preprocess ▷ and ▶ as method dispatch operators in ANSI-C.
Version 20200823.
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.
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 $s = writeTempFile(<<END);
struct Node // Node definition
{char * key; // Key for the node
}
Node newNode //C Create a new node
(const struct Node node) // Input key
{return node;
}
static Node getKey // Get the key for a node
(const Node n) // Node to dump
{return n.key;
}
static Node dump_node // Dump a node
(const Node n) // Node to dump
{print(n ▷ key);
}
struct Node n = newNode(key: "a"); //TnewNode
struct Node o = newNode(); //TnewNode
struct Node p = newNode; //TnewNode
n ▷ dump; //Tdump
END
my $c = temporaryFile.'.c'; # Translated C file # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
my $h = temporaryFile.'.h'; # Prototypes in H file
my $r = c($s, $c, $h); # Preprocess # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
# owf($logFile, readFile($c)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
# owf($logFile, readFile($h));
is_deeply scalar(readFile($h)), <<END; # Generated prototypes
Node newNode
(const struct Node node);
static Node getKey
(const Node n);
static Node dump_node
(const Node n);
struct ProtoTypes_Node {
Node (*dump)( // Dump a node
const Node n); // Node to dump
Node (*getKey)( // Get the key for a node
const Node n); // Node to dump
} const ProtoTypes_Node =
{dump_node, getKey};
END
# dumpFile($logFile, unbless $r);
is_deeply $r,
{
methods => {
dump_node => {
comment => "Dump a node",
flags => {},
name => "dump",
parameters => [["const Node", "n", "Node to dump"]],
return => "Node ",
structure => "const Node",
},
getKey => {
comment => "Get the key for a node",
flags => {},
name => "getKey",
parameters => [["const Node", "n", "Node to dump"]],
return => "Node ",
structure => "const Node",
},
newNode => {
comment => "Create a new node",
flags => { C => 1 },
name => "newNode",
parameters => [["const struct Node", "node", "Input key"]],
return => "Node ",
structure => "const struct Node",
},
},
structureParameters => {
"Node" => { dump_node => 1, getKey => 1 },
"struct Node" => { newNode => 1 },
},
structures => {
Node => { comment => "Node definition", flags => "", name => "Node" },
},
testsFound => { dump => 1, newNode => 3 },
testsNeeded => { getKey => 1 },
};
}
done_testing;
if ($localTest)
{say "TO finished in ", (time() - $startTime), " seconds";
}
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
Index
1 c - Preprocess ▷ and ▶ as method dispatch operators in ANSI-C.
2 method - Check whether a line of C code defines a method, returning (return, name, flags, comment) if it is, else ()
3 structure - Check whether a line of C code defines a structure, returning (name, flags, comment) if it is, else ()
4 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.