blib/lib/Preprocess/Ops.pm

filter: Statementstmt 70.4% 234 / 332 Branchbran 37.9% 44 / 116 Conditioncond 14.2% 2 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 84.0% 21 / 25 Podpod 80.0% 8 / 10 totaltotal 61.0% 309 / 506 CC 86 SCAR 46.1
CC
86
Cov
61%
includeFile
380.0
duplicateFunction
72.0
c
64.0
CRAP 100.5
↑ Summary
1 #!/usr/bin/perl -I/home/phil/perl/cpan/DataTableText/lib
2 #-------------------------------------------------------------------------------
3 # Preprocess ◁, ◀, ▷ and ▶ as operators in ANSI-C.
4 # Philip R Brenan at gmail dot com, Appa Apps Ltd Inc., 2020
5 #-------------------------------------------------------------------------------
6 # podDocumentation
7 package Preprocess::Ops;
8 1 our $VERSION = 20201117;
9 1 ▶use warnings FATAL => qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Table::Text qw(:all !trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use feature qw(say current_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 my $logC = q(/home/phil/c/z/z/zzz.c); # Log to this file if present
19 1 my $logH = q(/home/phil/c/z/z/zzz.h);
20  
21 1 binModeAllUtf8; # Helps with debugging unicode characters
22  
23 #D1 Preprocess # Preprocess ◁, ◀, ▷ and ▶ as operators in ANSI-C.
24  
25 sub trimComment($) #P Remove trailing white space and comment
26 6 ▶ {my ($s) = @_; # String
Subroutine trimComment: called CC=1 SCAR=0.0
Pod documented
27 6 $s =~ s(\s*//.*\n) ()r;
28 }
29  
30 sub method($) #P Check whether a line of C code defines a method, returning (return, name, flags, comment) if it is, else ()
31 84 ▶ {my ($line) = @_; # Line of C code
Subroutine method: called CC=4 SCAR=13.9
Pod documented
32 84 ▶ return () if $line =~ m(test.*//T\S); # Tests are never methods
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ m[test.*//T\S] 0 84 84
33 84 ▶ if ($line =~ m(\Astatic\s*(.*?)((?:\w|\$)+)\s+//(\w*)\s*(.*)\Z)) # Static function is always a method
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[\Astatic\s*(.*?)((?:\w|\$)+)\s+//(\w*)\s*(.*)\Z]) 2 82 84
34 2 {return ($1, $2, $3, $4)
35 }
36 82 ▶ if ($line =~ m(\A(.*?)(new(?:\w|\$)+)\s+//(\w*)\s*(.*)\Z)) # Constructor is always a method
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[\A(.*?)(new(?:\w|\$)+)\s+//(\w*)\s*(.*)\Z]) 1 81 82
37 1 {return ($1, $2, $3, $4);
38 }
39 ()
40 81 }
41  
42 sub structure($) #P Check whether a line of C code defines a structure, returning (name, flags, comment) if it is, else ()
43 84 ▶ {my ($line) = @_; # Line of C code
Subroutine structure: called CC=2 SCAR=6.9
Pod documented
44  
45 84 ▶ if ($line =~ m(\A(typedef\s+)?struct\s+((?:\w|\$)+)\s*//(w*)\s*(.*)\Z)) # struct name, comment start, flags, comment
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[\A(typedef\s+)?struct\s+((?:\w|\$)+)\s*//(w*)\s*(.*)\Z]) 1 83 84
46 1 {return ($2, $3, $4)
47 }
48 ()
49 83 }
50  
51 sub mapCode($) #P Find the structures and methods defined in a file
52 0 ▶ {my ($file) = @_; # Input file
Subroutine mapCode: not called CC=5 SCAR=34.0
Pod documented
53  
54 0 my %methods; # Method descriptions
55 my %structures; # Structures defined
56  
57 0 my @code = readFile($file); # Read input file
58 0 for my $line(@code) # Index of each line
59 0 ▶ {next if $line =~ m(\A//); # Ignore comment lines
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ m[\A//] 0 0 0
60  
61 0 my ($return, $name, $flags, $comment) = method($line); # Parse function return, name, description comment
62 0 ▶ if ($name)
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 0 0 0
63 0 {$methods{$name}++ # Return type
64 }
65 else
66 0 {my ($name, $flags, $comment) = structure($line); # Parse structure definition
67 0 ▶ if ($name)
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 0 0 0
68 0 {$structures{$name}++
69 }
70 }
71 }
72  
73 0 genHash(q(PreprocessOpsMap), # Methods and structures in the C file being preprocessed
74 methods => \%methods, # Methods.
75 structures => \%structures, # Structure definitions.
76 );
77 }
78  
79 sub printData($$) #P Print statement
80 0 ▶ {my ($lineNumber, $line) = @_; # Code line number, code line
Subroutine printData: not called CC=3 SCAR=24.8
Pod documented
81  
82 0 my ($command, @w) = split m/\s+/, $line; # Parse print line
83 0 my @f;
84 0 for my $w(@w) # Each variable to be printed
85 0 ▶ {push @f, join ' ', $w, "=", $w =~ m((\A|\.|\->)[i-n]) ? "%lu" : "%s";
Branch: 1 branch
BranchTrueFalseTotal
$w =~ /(\A|\.|\->)[i-n]/ ? : 0 0 0
86 }
87 0 my $f = join " ", @f;
88 0 my $w = join ", ", @w;
89 0 my $l = $lineNumber + 1;
90 0 qq(fprintf(stderr, "Line $l: $f\\n", $w);\n);
91 }
92  
93 sub duplicateFunction($$$) #P Duplicate the previous function with the specified changes applied
94 0 ▶ {my ($lineNumber, $inputFile, $code) = @_; # Line number of line being expanded, file containing line being expanded, lines of code
Subroutine duplicateFunction: not called CC=8 SCAR=42.8
Pod documented
95 0 ▶ if ($$code[$lineNumber] =~ m(\A(duplicate)\s+)) # Parse duplicate statement: the words after are comma separated lists of regular expressions that change the text of the preceding function
Branch: 1 branch
BranchTrueFalseTotal
if ($code->[$lineNumber] =~ /\A(duplicate)\s+/) 0 0 0
96 0 {my ($command, @changes) = split /\s+/, $$code[$lineNumber];
97 0 my @c;
98 0 ▶ for(my $i = $lineNumber - 1; $i >= 0; --$i) # Text of preceding function to duplicate
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 0 0 0
99 0 {unshift @c, my $c = $$code[$i];
100 0 ▶ last if $c =~ m(\A\S);
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /\A\S/ 0 0 0
101 }
102 0 my @r; # Resulting code
103 0 for my $change(@changes) # Apply changes
104 0 {my @C; # Code after each change
105 0 for my $c(@c) # Each change
106 0 {local $_ = $c;
107 0 for my $r(split/,/, $change) # Each re in the change
108 0 {eval $r;
109 0 ▶ confess "Cannot make change: $r in: $change\n$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
110 }
111 0 push @C, $_; # Save accumulated changes
112 }
113  
114 0 my $l = $lineNumber + 2; # Save duplicate code with accumulated changes
115 0 push @r, join '', @C;
116 0 push @r, qq(#line $l "$inputFile"\n);
117 }
118  
119 0 my $r = join '', @r; # Changed code
120 0 return $r;
121 }
122  
123 0 confess $$code[$lineNumber]," is not a 'duplicate' command";
124 }
125  
126 sub includeFile($$$$$) #P Expand include files so that we can pull in code and structures from other files in the includes folder.
127 0 ▶ {my ($lineNumber, $inputFile, $cFile, $hFile, $code) = @_; # Line number of line being expanded, file containing line being expanded, output C file, output H file, line of code
Subroutine includeFile: not called CC=19 SCAR=59.4
Pod documented
128 0 ▶ if ($code =~ m(\A(include)\s+)) # Parse preprocessor statement
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\A(include)\s+/) 0 0 0
129 0 {my ($command, $relFile, @items) = split /\s+/, $code;
130 0 my %items = map {$_=>1} @items;
131 0 my $file = sumAbsAndRel($inputFile, $relFile);
132 0 ▶ -e $file or confess "Cannot find include file: $file\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $file or confess("Cannot find include file: $file\n") 0 0 0
133  
134 0 my @code = readFile($file);
135 # my $map = mapCode($inputFile);
136  
137 0 ▶ for(my $i = 0; $i < @code; ++$i) # Expand duplicate commands
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @code) 0 0 0
138 0 ▶ {if ($code[$i] =~ m(\Aduplicate )) # Duplicate the previous function with changes
Branch: 1 branch
BranchTrueFalseTotal
if ($code[$i] =~ /\Aduplicate /) 0 0 0
139 0 {$code[$i] = duplicateFunction($i, $inputFile, \@code);
140 }
141 }
142  
143 0 my @c;
144 0 ▶ for(my $i = 0; $i < @code; ++$i) # Expand exports/include commands in included file
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @code) 0 0 0
145 0 {my $c = $code[$i]; # With trailing comment
146 0 my $d = $c =~ s(//.*\Z) ()gsr; # Without trailing comment
147 0 ▶ if ($c =~ m(\Ainclude)) # Expand include files so that we can pull in code and structures from other files in the includes folder.
Branch: 3 branches
BranchTrueFalseTotal
if ($c =~ /\Ainclude/) { } 0 0 0
elsif ($c =~ /\Aexports\s/) { } 0 0 0
elsif (&Preprocess::Ops::method($c) or &structure($c)) { } 0 0 0
148 0 {push @c, &includeFile($i, $file, $cFile, $hFile, $d);
149 }
150 elsif ($c =~ m(\Aexports\s)) # Add exports from included package if named in the include list
151 0 {my ($command, $name, @exports) = split m/\s+/, $d; # Export command, list name, exports in list
152 0 ▶ if ($items{qq(:$name)}) # Requested this list
Branch: 1 branch
BranchTrueFalseTotal
if ($items{":$name"}) 0 0 0
153 0 {for my $e(@exports) # Add exports unless they have been excluded
154 0 ▶ {$items{$e} ++ unless $items{qq(!$e)};
Branch: 1 branch
BranchTrueFalseTotal
unless $items{"!$e"} 0 0 0
155 }
156 }
157 }
158 ▶ elsif (method($c) or structure($c)) # Method or structure definition
Condition0%0/3
&Preprocess::Ops::method($c) or &structure($c)
l!l&&r!l&&!r
000
MC/DC0%0/2
&Preprocess::Ops::method($c) or &structure($c)
&Preprocess::Ops::method($c) &structure($c)
Truth table0%0/3
A or B
A= &Preprocess::Ops::method($c)B= &structure($c)
ABresult
1X1
011
000
159 0 ▶ {if ($c =~ m((\S+)\s*//)) # Method or structure name
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ m[(\S+)\s*//]) 0 0 0
160 0 {my $item = $1;
161 0 ▶ if ($command =~ m(include) && $items {$item}) # Include specifies the exact name of the thing we want
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ /include/ and $items{$item}) 0 0 0
Condition0%0/3
$command =~ /include/ and $items{$item}
!ll&&!rl&&r
000
MC/DC0%0/2
$command =~ /include/ and $items{$item}
$command =~ /include/ $items{$item}
Truth table0%0/3
A and B
A= $command =~ /include/B= $items{$item}
ABresult
0X0
100
111
162 0 {push @c, join ' ', "#line", $i+2, qq("$file"), "\n";
163 0 my @l;
164 0 ▶ for(; $i < @code; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @code) 0 0 0
165 0 {push @l, $code[$i];
166 0 ▶ last if $code[$i] =~ m(\A })
Branch: 1 branch
BranchTrueFalseTotal
if $code[$i] =~ /\A }/ 0 0 0
167 }
168 0 ▶ if (@l) # Save included struct or method
Branch: 1 branch
BranchTrueFalseTotal
if (@l) 0 0 0
169 0 {$l[0] =~ s(//) (//I); # Mark as included
170 # $l[0] =~ s/\Astatic /static __attribute__ ((unused)) /; # Mark included methods as potentially unused
171 0 push @c, @l;
172 }
173 }
174 }
175 }
176 }
177 0 my $l = $lineNumber + 2; # Adjust line numbers to reflect unexpanded source
178 0 return join '', @c, qq(#line $l "$inputFile" // CC\n);
179 # return join '', @c;
180 }
181 0 confess "Unable to parse include statement:\n$code";
182 } # includeFile
183  
184 sub c($$$;$) # Preprocess ▷ and ▶ as method dispatch operators in ANSI-C.
185 4 ▶ {my ($inputFile, $cFile, $hFile, $column) = @_; # Input file, C output file, H output file, optional start column for comments (80)
Subroutine c: called CC=47 SCAR=41.6
Pod documented
186  
187 4 my $baseFile = fn $inputFile; # The base name of the file
188 4 my($shortBaseFile)= split /_/, $baseFile; # Base name of the file preceding first underscore
189 4 my $packageName = ucfirst $baseFile; # The package name which is used to replace $
190 4 ▶ my $commentColumn = ($column // 80) - 1; # Column in which to start comments
Condition50%1/2
$column // 80
l!l
04
MC/DC0%0/1
$column // 80
$column
Truth table50%1/2
A
A= $column
Aresult
11
00
191  
192 4 my %methods; # Method descriptions
193 my %structures; # Structures defined
194 4 my %structureParameters; # Structures used as parameters
195 4 my %testsFound; # Tests found
196 4 my %testsNeeded; # Tests needed
197 4 my @forwards; # Forward declarations of functions used as methods
198 4 my @code = readFile($inputFile); # Read code
199 4 my %exports; # Export statements encountered
200  
201 4 for my $i(keys @code) # Execute preprocessor commands found in the source
202 84 {my $c = $code[$i];
203 84 ▶ if ($c =~ m(\A(include)\s+)) # Expand include files so that we can pull in code and structures from other files in the includes folder.
Branch: 4 branches
BranchTrueFalseTotal
if ($c =~ /\A(include)\s+/) { } 0 84 84
elsif ($c =~ /\Aduplicate /) { } 0 84 84
elsif ($c =~ /\A(exports)\s+/) { } 0 84 84
elsif ($c =~ /\Aprint/) { } 0 84 84
204 0 {$code[$i] = includeFile($i, $inputFile, $cFile, $hFile, $c);
205 }
206 elsif ($c =~ m(\Aduplicate )) # Duplicate the previous function with changes
207 0 {$code[$i] = duplicateFunction($i, $inputFile, \@code);
208 }
209 elsif ($c =~ m(\A(exports)\s+)) # Skip export commands in open source
210 0 {$exports{$c} = $i+1;
211 0 $code[$i] = "\n";
212 }
213 elsif ($c =~ m(\Aprint)) # Expand print statements
214 0 {$code[$i] = printData($i, $c);
215 }
216 }
217  
218 4 @code = map {"$_\n"} split /\n/, join '', @code; # Resplit code plus any additions into lines
219  
220 my sub expand($) # Expand $ and @
221 84 ▶ {$_[0] =~ s(\$\$) ($baseFile)gs; # $$ is base file name with first char lower cased
Subroutine expand: called CC=1 SCAR=0.0
222 84 $_[0] =~ s(\$) ($packageName)gs; # $ is base file name with first character uppercased
223 84 $_[0] =~ s(\@) (${shortBaseFile}_)gs; # @ is translated to short base file name followed by underscore to support Gnome naming conventions
224 }
225  
226 4 expand($_) for @code; # Replace $ with package name.
227  
228 4 if (1) # Parse source code
229 4 {my %duplicates; my @duplicates; # Duplication check for first parameter plus short method name
230 4 for my $i(keys @code) # Index of each line
231 84 {my $line = $code[$i];
232 84 ▶ next if $line =~ m(\A//); # Ignore comment lines
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ m[\A//] 0 84 84
233  
234 84 my ($return, $name, $flags, $comment) = method($line); # Parse function return, name, description comment
235 84 ▶ if ($name)
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 3 81 84
236 3 {$methods{$name}{return} = $return; # Return type
237 3 $methods{$name}{flags} = {map {$_=>1} split //, $flags}; # Flags after comment start
238 3 $methods{$name}{comment} = $comment; # Comment
239 3 ($methods{$name}{name}) = split /_/, $name; # Short name as used after call operator
240 3 push @forwards, join ' ', trimComment($line); # Save function definition for forward declaration
241  
242 3 ▶ if ($line !~ m(\(void\))) # Parse parameter definitions if there are any
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /\(void\)/) 2 1 3
243 2 {for $i($i+1..$#code) # Parameter definitions
244 3 {$line = $code[$i];
245 3 ▶ if ($line =~ m(\A\s*[(]?(.*?)\s*(\w+)[,)]\s*//\s*(.*)\Z)) # Variable: Get type, parameter name, comment
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ m[\A\s*[(]?(.*?)\s*(\w+)[,)]\s*//\s*(.*)\Z]) { } 3 0 3
elsif ($line =~ m[\A\s*(.*?)\s*\(\*(\s*(const)?\s*\w+)\)\s*(.*?)[,\)]\s*//\s*(.*)\Z]) { } 0 0 0
elsif ($line =~ m[\A\s*\.\.\.\)\s*//\s*(.*)\Z]) { } 0 0 0
246 3 {push $methods{$name}{parameters}->@*, [$1, $2, $3];
247 }
248 elsif ($line =~ m(\A\s*(.*?)\s*\(\*(\s*(const)?\s*\w+)\)\s*(.*?)[,\)]\s*//\s*(.*)\Z)) # Function: Get type, parameter name, comment
249 0 {push $methods{$name}{parameters}->@*, ["$1 (*$2) $4", $2, $5];
250 }
251 elsif ($line =~ m(\A\s*\.\.\.\)\s*//\s*(.*)\Z)) # Variadic list
252 0 {push $methods{$name}{parameters}->@*, ["", "...", $1];
253 }
254  
255 3 push @forwards, trimComment($line); # Save function definition for forward declaration
256 3 ▶ last if $line =~ m(\)\s*//); # End of parameter list
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ m[\)\s*//] 2 1 3
257 }
258 }
259  
260 3 $forwards[-1] .= ';'; # Terminate forward declaration
261 3 ▶ if (my $o = $methods{$name}{structure} = $methods{$name}{parameters}[0][0]) # Structure parameter
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = $methods{$name}{"structure"} = $methods{$name}{"parameters"}[0][0]) 2 1 3
262 2 {$o =~ s((\A|\s+)const\s+) ()g; # Remove const from structure name
263 2 $o =~ s([*]) ()g; # Remove pointer from structure name
264 2 $o = nws($o); # Normalize white space
265 2 $structureParameters{$o}{$name}++; # Record methods in each structure
266 2 ▶ if (my $d = $duplicates{"$name$o"}) # Check for duplicate
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $duplicates{"$name$o"}) 0 2 2
267 0 {push @duplicates, [$name, $o, $i, $d]; # Record duplicate
268 }
269 2 $duplicates{"$name$o"} = $i;
270 }
271 }
272 84 if (1)
273 84 {my ($name, $flags, $comment) = structure($line); # Parse structure definition
274 84 ▶ if ($name)
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 1 83 84
275 1 {$structures{$name} = genHash(q(PreprocessOpsStruct), # Structure declaration
276 name => $name, # Name of structure
277 flags => $flags, # Flags for structure
278 comment => $comment); # Comment for structure
279 }
280 }
281 }
282 4 ▶ if (@duplicates) # Print duplicates
Branch: 1 branch
BranchTrueFalseTotal
if (@duplicates) 0 4 4
283 0 {for my $i(keys @code) # Index of each line
284 0 {my $line = $code[$i];
285 0 say STDERR sprintf("%06d %s\n", $i, $line);
286 }
287 0 confess join "\n", "Duplicates:", dump(\@duplicates);
288 }
289 4 if (1) # Locate tests for each method
290 4 {my %m; # Methods that need tests
291 4 for my $m(sort keys %methods)
292 3 {my $flags = $methods{$m}{flags}; # Flags for method
293 3 ▶ next if $$flags{I} or $$flags{P}; # Ignore private methods marked with P and included methods marked with I
Branch: 1 branch
BranchTrueFalseTotal
if $flags->{"I"} or $flags->{"P"} 0 3 3
Condition33%1/3
$flags->{"I"} or $flags->{"P"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$flags->{"I"} or $flags->{"P"}
$flags->{"I"} $flags->{"P"}
Truth table33%1/3
A or B
A= $flags->{"I"}B= $flags->{"P"}
ABresult
1X1
011
000
294 3 $testsNeeded{$methods{$m}{name}}++;
295 }
296  
297 4 for my $l(@code) # Each code line
298 84 {my @t = $l =~ m((//T\w+))g; # Tests located
299 84 for my $t(@t) # Each test marker //T...
300 3 {my $n = $t =~ s(\A//T) ()r; # Remove //T
301 3 delete $testsNeeded{$n}; # Test found for this method
302 3 $testsFound{$n}++; # The tests we have found
303 }
304 }
305  
306 4 ▶ if (keys %testsNeeded) # Report methods that need tests
Branch: 1 branch
BranchTrueFalseTotal
if (keys %testsNeeded) 0 4 4
307 0 {lll "The following methods need tests:\n", join "\n", sort keys %testsNeeded;
308 }
309 }
310 }
311  
312 4 if (1) # Write prototypes
313 4 {my @h; # Generated code
314 4 for my $struct(sort keys %structures) # Each structure regardless of whether it is actually used in a method
315 1 {push @h, "struct ProtoTypes_$struct {"; # Start structure
316 1 for my $m(sort keys $structureParameters{$struct}->%*) # Methods in structure
317 2 {my $method = $methods{$m}; # Method
318 2 my $s = join '', ' ', $$method{return}, ' (*', $$method{name}, ')('; # Start signature
319 2 my $t = join ' ', pad($s, $commentColumn), '//', $$method{comment};
320 2 push @h, $t;
321 2 my @p = $$method{parameters}->@*; # Parameters for method
322 2 for my $i(keys @p) # Each parameter
323 3 {my ($return, $name, $comment) = $p[$i]->@*;
324  
325 3 my $cc = $commentColumn; # Comment column
326 3 ▶ my $comma = $i == $#p ? ');' : ','; # Comma as separator
Branch: 1 branch
BranchTrueFalseTotal
$i == $#p ? : 2 1 3
327 3 my $Comment = "// $comment"; # Format comment
328 3 my $off = " " x 4;
329 3 ▶ if ($return =~ m(\(*\s*(const)?\s*\w+\))) # Function parameter
Branch: 1 branch
BranchTrueFalseTotal
if ($return =~ /\(*\s*(const)?\s*\w+\)/) { } 0 3 3
330 0 {push @h, join ' ', pad(qq($off$return$comma), $cc), $Comment;
331 }
332 else # Variable parameter
333 3 {push @h, join ' ', pad(qq($off$return $name$comma), $cc), $Comment;
334 }
335 }
336 }
337 1 push @h, join '', " } const ProtoTypes_", $struct, ' ='; # Load prototype
338 1 my $j = join(', ', sort keys $structureParameters{$struct}->%*); # List of functions
339 1 push @h, join '', "{$j};";
340 1 push @h, <<END; # Add a constructor for each structure
341 $struct new$struct($struct allocator) {return allocator;}
342 END
343 }
344 4 owf($hFile, join "\n", @forwards, @h, '');
345 }
346  
347 4 if (1) # Preprocess here documents in C code
348 4 {my $state; # True if we are in a here document
349 4 ▶ for(my $i = 0; $i < @code; ++$i) # Each start line
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @code) 84 4 88
350 84 ▶ {next if $code[$i] =~ m(\A//◉); # Skip previously scanned here document
Branch: 1 branch
BranchTrueFalseTotal
if $code[$i] =~ m[\A//\x{25c9}] 7 77 84
351 77 ▶ if ($code[$i] =~ s((\A.*\S\s*)◉(.*)\Z) ($1)) # Start of here document
Branch: 1 branch
BranchTrueFalseTotal
if ($code[$i] =~ s/(\A.*\S\s*)\x{25c9}(.*)\Z/$1/) 3 74 77
352 3 {my ($open, $close) = ($1, $2); # open and close of here doc expression
353 3 my @h; # Lines of here document
354 3 my $j = $i + 1; # Scan forwards
355 3 for my $k($j..$#code) # Skip previously extracted here documents
356 3 ▶ {last unless $code[$j = $k] =~ m(\A//◉);
Branch: 1 branch
BranchTrueFalseTotal
unless $code[$j = $k] =~ m[\A//\x{25c9}] 3 0 3
357 }
358 3 for $j($j..$#code) # Scan forwards
359 7 {my $c = $code[$j];
360 7 ▶ if ($code[$j] =~ m(\A◉)) # Close
Branch: 1 branch
BranchTrueFalseTotal
if ($code[$j] =~ /\A\x{25c9}/) { } 3 4 7
361 3 { $code[$j] = q(//).$code[$j]; # Comment close out
362 3 my $h = join ' ', @h; # Equivalent string for here document
363 3 $code[$i] = qq($open$h$close\n); # Rebuild here doc line with one string
364 3 last;
365 }
366 else # One line of here document
367 4 {my $c = $code[$j]; # Remove new line
368 4 chomp $c; # Remove new line
369 4 $c =~ s(") (\\\")g; # Protect quotes
370 4 push @h, qq("$c\\n"); # Save line
371 4 $code[$j] = q(//◉).$code[$j]; # Comment line out
372  
373 }
374 }
375 }
376 }
377 }
378  
379 4 if (1) # Preprocess input C file
380 4 {my $e = q([a-z0-9𝗮-𝘇¹²³⁴⁵⁶⁷⁸⁹⁰₁₂₃₄₅₆₇₈₉₀ᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖʳˢᵗᵘᵛʷˣʸᶻ\$_>.*[\]-]); # Elements of a name
381 4 for my $c(@code) # Source code lines
382 84 {$c =~ s{($e+)\s*◀\s*(.*?);} {typeof($2) $1 = $2;}gis; # Variable creation
383 84 $c =~ s{($e+)\s*◁\s*(.*?);} {const typeof($2) $1 = $2;}gis; # Constant creation
384  
385 84 $c =~ s{new\s*(\w+\s*)\(([^:)]*:[^)]*)\)} # Constructor with named arguments in parenthesis based on: https://gcc.gnu.org/onlinedocs/gcc-10.2.0/gcc/Designated-Inits.html#Designated-Inits
386 {new$1(({struct $1 t = {$2, proto: &ProtoTypes_$1}; t;}))}gs;
387  
388 84 $c =~ s{new\s*(\w+\s*)(\(\))?([,;])} # Constructor followed by [,;] calls for default constructor.
389 {new$1(({struct $1 t = {proto: &ProtoTypes_$1}; t;}))$3}gs;
390  
391 84 $c =~ s{($e+)\s*▶\s*(\w+)\s*\(} {$1->proto->$2($1, }gis; # Method call with arguments from pointer
392 84 $c =~ s{($e+)\s*▶\s*(\w+)} {$1->proto->$2($1)}gis; # Method call with no arguments from pointer
393 84 $c =~ s{($e+)\s*▷\s*(\w+)\s*\(} {$1.proto->$2(&$1, }gis; # Method call with arguments
394 84 $c =~ s{($e+)\s*▷\s*(\w+)} {$1.proto->$2(&$1)}gis; # Method call with no arguments
395  
396 84 if (1) # String compare and assignment
397 84 {$c =~ s{($e+)\s* !≈ \s*([^;)]*)} { strcmp($1, $2)}gisx; # String not equal
398 84 $c =~ s{($e+)\s* ≈≈ \s*([^;)]*)} {!strcmp($1, $2)}gisx; # String equal
399 84 $c =~ s{($e+)\s* ≋ \s*([^;)]*)} {char $1\[$2\]}gisx; # Create string
400 84 $c =~ s{($e+)\s* ≈ \s*([^;)]*)} { strcpy($1, $2)}gisx; # String copy
401 84 $c =~ s{($e+)\s* \+≈\s*([^;)]*)} { stpcpy($1, $2)}gisx; # String concatenation
402 84 $c =~ s{($e+)\s* ∼ \s*([^;)]*)} { strstr($1, $2)}gisx; # Find second string in first string and return a pointer to it
403 }
404  
405 84 $c =~ s{[☑✓✔✅]([^;]*)} {assert($1)}gis; # Tick becomes assert
406 84 $c =~ s{[☒✕❎✗✘✖❌]([^;]*)} {assertNot($1)}gis; # Cross becomes assert not
407  
408 84 if (1) # Memory compare and assignment
409 84 {my $s = 'source'x3; my $t = 'target'x3; # Unlikely variable names
410 84 $c =~ s{($e+)\s*!≞\s*([^;)]*)} {({typeof($1) $t = $1, $s = $2; memcmp((void *)&$t, (void *)&$s, sizeof($1));})}gis; # Memory not equal
411 84 $c =~ s{($e+)\s*≞≞\s*([^;)]*)} {({typeof($1) $t = $1, $s = $2; !memcmp((void *)&$t, (void *)&$s, sizeof($1));})}gis; # Memory equal
412 84 $c =~ s{($e+)\s*≞\s*0\s*} {memset((void *)&$1, 0, sizeof($1))}gis; # Clear memory if equal zero
413 84 $c =~ s{($e+)\s*≞\s*([^;]*)} {({typeof($1) $s = $2; memcpy((void *)&$1, (void *)&$s, sizeof($1));})}gis; # Memory equals becomes memcpy if copying something not zero
414 }
415  
416 84 if (1) # Memory compare and assignment where one side of the equation (black) is a pointer to void
417 84 {my $s = 'source'x3; my $t = 'target'x3; # Unlikely variable names # Type set by left hand, right hand is void *
418 84 $c =~ s{($e+)\s*!◧\s*([^;)]*)} {({typeof($2) $s = $2; memcmp((void *)$1, (void *)&$s, sizeof($2));})}gis; # Memory not equal
419 84 $c =~ s{($e+)\s*◧◧\s*([^;)]*)} {({typeof($2) $s = $2; !memcmp((void *)$1, (void *)&$s, sizeof($2));})}gis; # Memory equal
420 84 $c =~ s{($e+)\s*◧\s*([^;]*)} {({typeof($2) $s = $2; memcpy((void *)$1, (void *)&$s, sizeof($2));})}gis; # Copy memory to make equal
421 # Type set by right hand size
422 84 $c =~ s{($e+)\s*!◨\s*([^;)]*)} {({ memcmp((void *)&$1, (void *)$2, sizeof($1));})}gis; # Memory not equal
423 84 $c =~ s{($e+)\s*◨◨\s*([^;)]*)} {({ !memcmp((void *)&$1, (void *)$2, sizeof($1));})}gis; # Memory equal
424 84 $c =~ s{($e+)\s*◨\s*0\s*} { memset((void *)&$1, 0, sizeof($1))}gis; # Clear memory if equal zero
425 84 $c =~ s{($e+)\s*◨\s*([^;]*)} { memcpy((void *)&$1, (void *)$2, sizeof($1))}gis; # Memory equals becomes memcpy if copying something not zero
426 }
427  
428 84 $c =~ s( +\Z) ()gs; # Remove trailing spaces at line ends
429 }
430 }
431  
432 4 if (1) # Report export requests for methods that are missing
433 4 {my @m;
434 4 for my $x(sort keys %exports)
435 0 {my ($command, $list, @e) = split /\s+/, $x;
436  
437 0 for my $e(@e)
438 0 {expand($e);
439 0 ▶ next unless $e =~ m(\A[a-z])i;
Branch: 1 branch
BranchTrueFalseTotal
unless $e =~ /\A[a-z]/i 0 0 0
440 0 ▶ push @m, [$exports{$x}, $e] unless $methods{$e} or $structures{$e};
Branch: 1 branch
BranchTrueFalseTotal
unless $methods{$e} or $structures{$e} 0 0 0
Condition0%0/3
$methods{$e} or $structures{$e}
l!l&&r!l&&!r
000
MC/DC0%0/2
$methods{$e} or $structures{$e}
$methods{$e} $structures{$e}
Truth table0%0/3
A or B
A= $methods{$e}B= $structures{$e}
ABresult
1X1
011
000
441 }
442 }
443 4 ▶ if (keys @m)
Branch: 1 branch
BranchTrueFalseTotal
if (keys @m) 0 4 4
444 0 {say STDERR formatTable(\@m, <<END,
445 Line Line on which the missing method was exported
446 Export Method requested but missing
447 END
448 title => q(Missing exports));
449 }
450 }
451  
452 4 owf($cFile, qq(#line 1 "$inputFile"\n).join('', @code)); # Output C file
453  
454 4 genHash(q(PreprocessOpsParse), # Structure of the C program being preprocessed
455 methods => \%methods, # Methods.
456 structures => \%structures, # Structure definitions.
457 structureParameters => \%structureParameters, # Structures used as parameters
458 testsFound => \%testsFound, # Tests found
459 testsNeeded => \%testsNeeded) # Tests still needed
460 }
461  
462 #D0
463 #-------------------------------------------------------------------------------
464 # Export
465 #-------------------------------------------------------------------------------
466  
467 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
468  
469 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
470  
471 1 @ISA = qw(Exporter);
472 1 @EXPORT_OK = qw(
473 );
474 1 %EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]);
475  
476 # podDocumentation
477  
478 =pod
479  
480 =encoding utf-8
481  
482 =head1 Name
483  
484 Preprocess::Ops - Preprocess ◁, ◀, ▷ and ▶ as operators in ANSI-C.
485  
486 =head1 Synopsis
487  
488 See the final lines of:
489 L<https://github.com/philiprbrenan/C/blob/master/c/z/arenaTree/arenaTree.c>
490 for working examples of the following operators.
491  
492 =head2 Method dispatch operators: ▷ and ▶
493  
494 Preprocess ▷ and ▶ as method dispatch by translating:
495  
496 p = node ▷ key("a");
497  
498 to:
499  
500 p = node . proto->key(&node, "a");
501  
502 and:
503  
504 p = node ▶ key("a");
505  
506 to:
507  
508 p = node -> proto->key(node, "a");
509  
510 =head2 Constant and variable creation operators: ◁ and ◀
511  
512 Preprocess instances of ◁ as a constant creation operator:
513  
514 c ◁ sfc("cba");
515  
516 to get:
517  
518 const typeof(sfc("cba")) c = sfc("cba");
519  
520 Preprocess instances of ◀ as a variable creation operator:
521  
522 d ◀ sfc("cba");
523  
524 to get:
525  
526 typeof(sfc("cba")) c = sfc("cba");
527  
528 which, in effect, produces:
529  
530 const char c = sfc("cba");
531 char d = sfc("cba");
532  
533 in the context of:
534  
535 char sfc(char *s) {return *s;}
536  
537 int main(void) {
538 c ◁ sfc("cba");
539 d ◀ sfc("cba");
540 assert(c == 'c');
541 }
542  
543 =head2 Here documents via ◉
544  
545 Preprocess instances of ◉ as starting a L<here|https://www.perldoc.pl/perlop#Quote-and-Quote-like-Operators> document:
546  
547 char *c = ◉;
548 a
549 b
550 ◉
551  
552 to get:
553  
554 char *a =
555 "a\n"
556 " b\n"
557 ;
558  
559 =head2 Using ✓ or ✔ for assert(...);
560  
561 Convert instances of B<✓> or B<✔> as in:
562  
563 ✔ a == 1;
564  
565 to:
566  
567 assert(a == 1);
568  
569 to make B<assert> function calls more prominent in tests.
570  
571 Conversely B<✗> as in:
572  
573 a ◁ 0;
574 ✗ a;
575  
576 converts to:
577  
578 const int a = 0;
579 assert(!a);
580  
581 =head2 Using ≞ for memcpy(...);
582  
583 Convert instances of B<≞> as in:
584  
585 Colour c;
586 c ≞ makeColour(0,1,0,1);
587  
588 to:
589  
590 Colour c;
591 memcpy(
592 ((void *)&c,
593 ({typeof(c) s = makeColour(0,1,0,1); (void *)&s;}),
594 sizeof(c));
595  
596 to allow piece-wise initialization of structures in memory with constant elements.
597  
598 The special case of initialization with a constant zero:
599  
600 c ≞ 0;
601  
602 produces:
603  
604 memset(c, 0, sizeof(c));
605  
606 =head2 Using ≞≞ for !memcmp(...)
607  
608 Convert instances of B<≞≞> and B<!≞> as in:
609  
610 typedef struct
611 {int a;
612 int b;
613 } S;
614 S t, s; s.a = 1; s.b = 2;
615 t ≞ s; // A
616 ✓ s ≞≞ t; // B
617 t ≞ 0; // C
618 ✓ s !≞ t; // D
619  
620 to get:
621  
622 // A
623 ({typeof( t) sourcesourcesource = s;
624 memcpy(&t, &sourcesourcesource,
625 sizeof( t));
626 });
627  
628 // B
629 assert(
630 ({typeof(s) targettargettarget = s,
631 sourcesourcesource = t;
632 !memcmp(&targettargettarget, &sourcesourcesource, sizeof(s));
633 }));
634  
635 // C
636 memset(&t, 0, sizeof(t));
637  
638 // D
639 assert(
640 ({typeof(s)targettargettarget = s, sourcesourcesource = t;
641 memcmp( &targettargettarget, &sourcesourcesource,
642 sizeof(s));
643 }));
644  
645 to allow compact comparisons of structures in memory.
646  
647 =head2 Using ≈≈ for !strcmp(...);
648  
649 Convert instances of B<≈≈> and B<!≈> as in:
650  
651 { a ≋ 12; b ≋ 12; c ≋ 12;
652 a ≈ "aaaa";
653 b ≈ "bbbb";
654 A ◁ c +≈ a; B ◁ A +≈ b;
655 C ◁ c ∼ b;
656  
657 ✓ a ≈≈ "aaaa"; // AAAA
658 ✓ b ≈≈ "bbbb";
659 ✓ b !≈ a; // BBBB
660 ✓ c ≈≈ "aaaabbbb";
661 ✓ A ≈≈ b;
662 ✓ B ≈≈ "";
663 ✓ C ≈≈ b;
664  
665 to get:
666  
667 assert(!strcmp(a, "aaaa"); // AAAA
668  
669 assert( strcmp(b, "bbbb"); // BBBB
670  
671 to create readable string equality operations.
672  
673 =head3 Using ≋ to declare strings.
674  
675 Strings can be declared using the B<≋> operator to supply a length, so that:
676  
677 N ◁ 12;
678 a ≋ N+1;
679  
680 becomes:
681  
682 char a[13];
683  
684 via:
685  
686 const int N = 12;
687 char a[N + 1];
688  
689 =head3 Using +≈ to concatenate strings.
690  
691 Strings can be concatenated with the B<+≈> operator, so that:
692  
693 c ≋ 12;
694 a ◁ c +≈ "aaaa";
695 b ◁ a +≈ "aaaa";
696 ✓ c ≈≈ "aaaabbbb";
697  
698 becomes:
699  
700 char c[12];
701 char *a = stpcpy(c, "aaaa");
702 char *b = stpcpy(a, "bbbb");
703 assert(!strcmp(c, "aaaabbbb");
704  
705 =head3 Using ∼ to search strings.
706  
707 Strings can be searched using the B<∼> operator to supply a search string as in:
708  
709 C ◁ c ∼ b;
710  
711 which becomes:
712  
713 char *C = strstr(a, b);
714  
715 =head2 Replacing $ with the base file name.
716  
717 Occurrences of the B<$> character are replaced by the base name of the file
718 containing the source with the first letter capitalized, so that:
719  
720 typedef struct $Node {...} $Node;
721  
722 in a file called B<tree.c> becomes:
723  
724 typedef struct TreeNode {...} TreeNode;
725  
726 =head2 new operator
727  
728 Occurrences of:
729  
730 new XXX
731  
732 are replaced by:
733  
734 newXXX(({struct XXX t = {proto: &ProtoTypes_XXX}; t;}))
735  
736 Occurrences of:
737  
738 new XXX(a:1)
739  
740 are replaced by:
741  
742 newXXX(({struct XXX t = {a:1, proto: &ProtoTypes_XXX}; t;}))
743  
744 The prototype vectors are generated by examining all the methods defined in the
745 B<c> file. The prototype vectors are written to the specified B<h> file which
746 should be included in the B<c> file for use via the ▷ and ▶ operators.
747  
748 =head2 Marking tests with //T
749  
750 B<//T> immediately followed by the name of a method up to its first B<_> (if
751 any or the end of the name otherwise) marks a function as testing all the
752 methods that start with that name:
753  
754 void test10() //Tsystem //TprintsAs
755 { a ◁ make$FromString("uname");
756 a ▷ system;
757 ✓ a ▷ containsString("Linux");
758 ✓ a ▷ printsAs(◉);
759 Linux
760 ◉
761 a ▷ free;
762 }
763  
764 Function B<test10> is marked as testing both B<system_string> and
765 B<printsAs_stringBuffer_string>. Functions that are declared B<static> but
766 have no associated tests are listed in the preprocessor output as in:
767  
768 The following methods need tests:
769 parseXmlFromString
770  
771 after preprocessing a file called xml.c containing:
772  
773 static $Parse parse$FromString_$Parse_StringBuffer
774 (StringBuffer string)
775 {return make$ParseFromString(0, string, strlen(string));
776 }
777  
778 with no test function marked with B<//TparseXmlFromString>.
779  
780 =head2 Preprocessor commands
781  
782 =head3 duplicate
783  
784 The B<duplicate> command generates the previous function with the changes
785 indicated in the words following the command as in:
786  
787 static char * key_$Node // Get the key for a node
788 (const $Node n) // Node
789 {return n.key;
790 }
791 duplicate s/key/data/g
792  
793 which adds the following code to the current output file:
794  
795 static char * data_$Node // Get the data for a node
796 (const $Node n) // Node
797 {return n.data;
798 }
799  
800 =head3 exports
801  
802 The B<exports> command provides a name for or a collection of functions that
803 can be B<include>d in generated output files, for instance:
804  
805 exports aaa new$Node key_$Node
806  
807 creates a new set of exports called B<aaa> which contains the two functions
808 mentioned. As these names have B<$> in them they will be expanded with the base
809 name of the file into which they are being copied.
810  
811 =head3 include
812  
813 The B<include> command copies the named function, structures, and exported
814 collections from the specified file into the current output file. For instance:
815  
816 include ../arenaTree.c :arena !key_$Node data_$Node
817  
818 reads the relative file B<../arenaTree.c> and copies in all the structures
819 mentioned in collection B<arena> except for B<key_$node> as well as copying the
820 explicitly mentioned function B<data_$Node>.
821  
822 =head2 Public Version Control on GitHub
823  
824 Please submit changes as pull requests on
825 L<https://github.com/philiprbrenan/PreprocessOps>
826  
827 =head1 Description
828  
829 Preprocess ◁, ◀, ▷ and ▶ as operators in ANSI-C.
830  
831  
832 Version 20201117.
833  
834  
835 The following sections describe the methods in each functional area of this
836 module. For an alphabetic listing of all methods by name see L<Index|/Index>.
837  
838  
839  
840 =head1 Preprocess
841  
842 Preprocess ◁, ◀, ▷ and ▶ as operators in ANSI-C.
843  
844 =head2 c($inputFile, $cFile, $hFile, $column)
845  
846 Preprocess ▷ and ▶ as method dispatch operators in ANSI-C.
847  
848 Parameter Description
849 1 $inputFile Input file
850 2 $cFile C output file
851 3 $hFile H output file
852 4 $column Optional start column for comments (80)
853  
854 B<Example:>
855  
856  
857 if (88) {
858 my $d = q(zzz);
859 my $ds = fpd($d, qw(source));
860 my $dd = fpd($d, qw(derived));
861  
862 my $sc = fpe($ds, qw(node c)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
863  
864  
865 my $dc = fpe($dd, qw(node c)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
866  
867 my $dh = fpe($dd, qw(node h));
868  
869 owf($sc, <<END);
870 #include <stdio.h>
871  
872 typedef struct Node // Node
873 {const struct ProtoTypes_Node *proto;
874 int data;
875 } Node;
876  
877 #include "node.h"
878  
879 static Node by // New from node * number
880 (const Node * n, // Node
881 const int i) // Multiplier
882 {return new Node(data: i * n->data);
883 }
884  
885 static void dump // Dump a node to stdout
886 (const Node * n) // Node to dump
887 {printf("data=%d\
888 ", n->data);
889 }
890  
891 int main(void) //TnewNode //Tdump //Tby
892 {a ◁ new Node(data: 6);
893 b ◁ a ▷ by(7);
894 b ▷ dump;
895 return 0;
896 }
897 END
898  
899  
900 my $r = c($sc, $dc, $dh); # Preprocess source c to get derived c # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
901  
902  
903 my $c = qq((cd $dd; gcc node.c -o a; ./a)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
904  
905  
906 is_deeply scalar(qx($c)), "data=42
907 "; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
908  
909  
910  
911 is_deeply readCFile($dc), <<'END'; # Generated base.c # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
912  
913  
914 #line 1 "node.c" # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
915  
916 #include <stdio.h>
917  
918 typedef struct Node // Node
919 {const struct ProtoTypes_Node *proto;
920 int data;
921 } Node;
922  
923 #include "node.h"
924  
925 static Node by // New from node * number
926 (const Node * n, // Node
927 const int i) // Multiplier
928 {return newNode(({struct Node t = {data: i * n->data, proto: &ProtoTypes_Node}; t;}));
929 }
930  
931 static void dump // Dump a node to stdout
932 (const Node * n) // Node to dump
933 {printf("data=%d
934 ", n->data);
935 }
936  
937 int main(void) //TnewNode //Tdump //Tby
938 {const typeof(newNode(({struct Node t = {data: 6, proto: &ProtoTypes_Node}; t;}))) a = newNode(({struct Node t = {data: 6, proto: &ProtoTypes_Node}; t;}));
939 const typeof(a.proto->by(&a, 7)) b = a.proto->by(&a, 7);
940 b.proto->dump(&b);
941 return 0;
942 }
943 END
944  
945 is_deeply readCFile($dh), <<END; # Generated include file
946 static Node by
947 (const Node * n,
948 const int i);
949 static void dump
950 (const Node * n);
951 int main(void);
952 struct ProtoTypes_Node {
953 Node (*by)( // New from node * number
954 const Node * n, // Node
955 const int i); // Multiplier
956 void (*dump)( // Dump a node to stdout
957 const Node * n); // Node to dump
958 } const ProtoTypes_Node =
959 {by, dump};
960 Node newNode(Node allocator) {return allocator;}
961 END
962  
963 clearFolder($d, 10);
964 }
965  
966 if (36) {
967 my $d = q(zzz);
968  
969 my $c = owf(fpe($d, qw(source c)), <<'END'); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
970  
971 #include <assert.h>
972 #include <stdio.h>
973 int main(void)
974 {a ◁ ◉;
975 a
976 b
977 ◉
978 ✓ a[0] == 'a';
979 printf("%s", a);
980 }
981 END
982  
983 my $h = fpe($d, qw(source h));
984  
985 my $g = fpe($d, qw(derived c)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
986  
987  
988  
989 my $r = c($c, $g, $h); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
990  
991  
992  
993 is_deeply scalar(qx(cd $d; gcc derived.c -o a; ./a)), <<END, 'aaaa'; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
994  
995 a
996 b
997 END
998  
999 is_deeply readCFile($g), <<'END', 'bbbb';
1000  
1001 #line 1 "source.c" # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1002  
1003 #include <assert.h>
1004 #include <stdio.h>
1005 int main(void)
1006 {const typeof("a
1007 " " b
1008 ") a = "a
1009 " " b
1010 ";
1011 //◉a
1012 //◉ b
1013 //◉
1014 assert( a[0] == 'a');
1015 printf("%s", a);
1016 }
1017 END
1018 clearFolder($d, 10);
1019 }
1020  
1021 if (26) {
1022 my $d = q(zzz);
1023  
1024 my $c = owf(fpe($d, qw(source c)), <<'END'); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1025  
1026 #include <assert.h>
1027 #include <stdio.h>
1028 #include <stdlib.h>
1029 #include <string.h>
1030 int main(void)
1031 {typedef struct
1032 {int a;
1033 int b;
1034 } S;
1035 S s; s.a = 1; s.b = 2;
1036 S t;
1037 t ≞ s; ✓ s ≞≞ t;
1038 t ≞ 0; ✓ s !≞ t;
1039  
1040 S *ps = &s, *pt = &t;
1041  
1042 t ≞ 0; ✓ t.a == 0; ✓ t.b == 0; ✓ pt !◧ s;
1043 pt ◧ s; ✓ t.a == s.a; ✓ t.b == s.b; ✓ pt ◧◧ s;
1044  
1045 s ≞ 0; ✓ s.a == 0; ✓ s.b == 0; ✓ t !◨ ps;
1046 ps ◧ t; ✓ s.a == t.a; ✓ s.b == t.b; ✓ t ◨◨ ps;
1047 printf(◉);
1048 success
1049 ◉
1050 }
1051  
1052 if (26) {
1053 my $d = q(zzz);
1054  
1055 my $c = owf(fpe($d, qw(source c)), <<'END'); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1056  
1057 #include <assert.h>
1058 #include <stdio.h>
1059 #include <stdlib.h>
1060 #include <string.h>
1061 int main(void)
1062  
1063 { a ≋ 12; b ≋ 12; c ≋ 12; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1064  
1065 a ≈ "aaaa";
1066 b ≈ "bbbb";
1067  
1068 A ◁ c +≈ a; B ◁ A +≈ b; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1069  
1070  
1071 C ◁ c ∼ b; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1072  
1073  
1074 ✓ a ≈≈ "aaaa";
1075 ✓ b ≈≈ "bbbb";
1076 ✓ b !≈ a;
1077  
1078 ✓ c ≈≈ "aaaabbbb"; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1079  
1080 ✓ A ≈≈ b;
1081 ✓ B ≈≈ "";
1082 ✓ C ≈≈ b;
1083  
1084 printf(◉);
1085 success
1086 ◉
1087 }
1088 END
1089  
1090 my $h = fpe($d, qw(source h));
1091  
1092 my $g = fpe($d, qw(derived c)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1093  
1094  
1095 my $r = c($c, $g, $h); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1096  
1097  
1098 is_deeply scalar(qx(cd $d; gcc -g -Wall derived.c -o a; ./a)), <<END; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
1099  
1100 success
1101 END
1102 # clearFolder($d, 10);
1103 }
1104  
1105  
1106  
1107 =head2 PreprocessOpsMap Definition
1108  
1109  
1110 Methods and structures in the C file being preprocessed
1111  
1112  
1113  
1114  
1115 =head3 Output fields
1116  
1117  
1118 =head4 methods
1119  
1120 Methods.
1121  
1122 =head4 structures
1123  
1124 Structure definitions.
1125  
1126  
1127  
1128 =head2 PreprocessOpsParse Definition
1129  
1130  
1131 Structure of the C program being preprocessed
1132  
1133  
1134  
1135  
1136 =head3 Output fields
1137  
1138  
1139 =head4 methods
1140  
1141 Methods.
1142  
1143 =head4 structureParameters
1144  
1145 Structures used as parameters
1146  
1147 =head4 structures
1148  
1149 Structure definitions.
1150  
1151 =head4 testsFound
1152  
1153 Tests found
1154  
1155 =head4 testsNeeded
1156  
1157 Tests still needed
1158  
1159  
1160  
1161 =head2 PreprocessOpsStruct Definition
1162  
1163  
1164 Structure declaration
1165  
1166  
1167  
1168  
1169 =head3 Output fields
1170  
1171  
1172 =head4 comment
1173  
1174 Comment for structure
1175  
1176 =head4 flags
1177  
1178 Flags for structure
1179  
1180 =head4 methods
1181  
1182 Methods.
1183  
1184 =head4 name
1185  
1186 Name of structure
1187  
1188 =head4 structureParameters
1189  
1190 Structures used as parameters
1191  
1192 =head4 structures
1193  
1194 Structure definitions.
1195  
1196 =head4 testsFound
1197  
1198 Tests found
1199  
1200 =head4 testsNeeded
1201  
1202 Tests still needed
1203  
1204  
1205  
1206 =head1 Private Methods
1207  
1208 =head2 trimComment($s)
1209  
1210 Remove trailing white space and comment
1211  
1212 Parameter Description
1213 1 $s String
1214  
1215 =head2 method($line)
1216  
1217 Check whether a line of C code defines a method, returning (return, name, flags, comment) if it is, else ()
1218  
1219 Parameter Description
1220 1 $line Line of C code
1221  
1222 =head2 structure($line)
1223  
1224 Check whether a line of C code defines a structure, returning (name, flags, comment) if it is, else ()
1225  
1226 Parameter Description
1227 1 $line Line of C code
1228  
1229 =head2 mapCode($file)
1230  
1231 Find the structures and methods defined in a file
1232  
1233 Parameter Description
1234 1 $file Input file
1235  
1236 =head2 printData($lineNumber, $line)
1237  
1238 Print statement
1239  
1240 Parameter Description
1241 1 $lineNumber Code line number
1242 2 $line Code line
1243  
1244 =head2 duplicateFunction($lineNumber, $inputFile, $code)
1245  
1246 Duplicate the previous function with the specified changes applied
1247  
1248 Parameter Description
1249 1 $lineNumber Line number of line being expanded
1250 2 $inputFile File containing line being expanded
1251 3 $code Lines of code
1252  
1253 =head2 includeFile($lineNumber, $inputFile, $cFile, $hFile, $code)
1254  
1255 Expand include files so that we can pull in code and structures from other files in the includes folder.
1256  
1257 Parameter Description
1258 1 $lineNumber Line number of line being expanded
1259 2 $inputFile File containing line being expanded
1260 3 $cFile Output C file
1261 4 $hFile Output H file
1262 5 $code Line of code
1263  
1264  
1265 =head1 Index
1266  
1267  
1268 1 L<c|/c> - Preprocess ▷ and ▶ as method dispatch operators in ANSI-C.
1269  
1270 2 L<duplicateFunction|/duplicateFunction> - Duplicate the previous function with the specified changes applied
1271  
1272 3 L<includeFile|/includeFile> - Expand include files so that we can pull in code and structures from other files in the includes folder.
1273  
1274 4 L<mapCode|/mapCode> - Find the structures and methods defined in a file
1275  
1276 5 L<method|/method> - Check whether a line of C code defines a method, returning (return, name, flags, comment) if it is, else ()
1277  
1278 6 L<printData|/printData> - Print statement
1279  
1280 7 L<structure|/structure> - Check whether a line of C code defines a structure, returning (name, flags, comment) if it is, else ()
1281  
1282 8 L<trimComment|/trimComment> - Remove trailing white space and comment
1283  
1284 =head1 Installation
1285  
1286 This module is written in 100% Pure Perl and, thus, it is easy to read,
1287 comprehend, use, modify and install via B<cpan>:
1288  
1289 sudo cpan install Preprocess::Ops
1290  
1291 =head1 Author
1292  
1293 L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com>
1294  
1295 L<http://www.appaapps.com|http://www.appaapps.com>
1296  
1297 =head1 Copyright
1298  
1299 Copyright (c) 2016-2019 Philip R Brenan.
1300  
1301 This module is free software. It may be used, redistributed and/or modified
1302 under the same terms as Perl itself.
1303  
1304 =cut
1305  
1306  
1307  
1308 # Tests and documentation
1309  
1310 sub test
1311 1 ▶ {my $p = __PACKAGE__;
Subroutine test: called CC=5 SCAR=16.1
Pod undocumented
1312 1 binmode($_, ":utf8") for *STDOUT, *STDERR;
1313 1 ▶ return if eval "eof(${p}::DATA)";
Branch: 1 branch
BranchTrueFalseTotal
if eval "eof(${p}::DATA)" 0 1 1
1314 1 my $s = eval "join('', <${p}::DATA>)";
1315 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
1316 1 ▶ eval $s;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine readCFile: called CC=1 SCAR=0.0
Pod undocumented
1317 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
1318 1 1
1319 }
1320  
1321 1 ▶test unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
1322  
1323 1 1;
1324 # podDocumentation
1325 __DATA__
↑ Summary