Skip to content

Commit

Permalink
DRY in EQUIVCPOFFS
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Mar 2, 2024
1 parent dbf1528 commit 0a2f99a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
57 changes: 23 additions & 34 deletions Basic/Gen/PP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,27 @@ sub extract_signature_from_fulldoc {
return;
}

sub equivcpoffs_parse {
my ($good, $bflag, $from, $to) = @_;
my $bad = $good;
# parse 'good' code
$good =~ s/
\$EQUIVCPOFFS\(([^()]+),([^()]+)\)
/do { \$PP($to)[$1] = \$PP($from)[$2]; } while (0)/gx;
$good =~ s/
\$EQUIVCPTRUNC\(([^()]+),([^()]+),([^()]+)\)
/do { \$PP($to)[$1] = ($3) ? 0 : \$PP($from)[$2]; } while (0)/gx;
return $good if !$bflag;
# parse 'bad' code
$bad =~ s/
\$EQUIVCPOFFS\(([^()]+),([^()]+)\)
/do { if( \$PPISBAD($from,[$2]) ) { \$PPSETBAD($to,[$1]); } else { \$PP($to)[$1] = \$PP($from)[$2]; } } while (0)/gx;
$bad =~ s/
\$EQUIVCPTRUNC\(([^()]+),([^()]+),([^()]+)\)
/do { if( ($3) || \$PPISBAD($from,[$2]) ) { \$PPSETBAD($to,[$1]); } else {\$PP($to)[$1] = \$PP($from)[$2]; } } while (0)/gx;
'if ( $PRIV(bvalflag) ) { ' . $bad . ' } else { ' . $good . '}';
}

# function to be run by real pp_def so fake pp_def can do without other modules
sub load_deftable {
# Build the valid-types regex and valid Pars argument only once. These are
Expand Down Expand Up @@ -1417,25 +1438,7 @@ EOD

PDL::PP::Rule->new("Code", ["EquivCPOffsCode","BadFlag"],
"create Code from EquivCPOffsCode",
# NOTE: EQUIVCPOFFS and EQUIVCPTRUNC both suffer from the macro-block
# wart of C preprocessing. They look like statements but sometimes
# process into blocks, so if/then/else constructs can get broken.
# Either (1) use blocks for if/then/else, or (2) get excited and
# use the "do {BLOCK} while(0)" block-to-statement conversion construct
# in the substitution. I'm too Lazy. --CED 27-Jan-2003
sub {
my $good = shift;
my $bflag = shift;
my $bad = $good;
# parse 'good' code
$good =~ s/\$EQUIVCPOFFS\(([^()]+),([^()]+)\)/\$PP(CHILD)[$1] = \$PP(PARENT)[$2]/g;
$good =~ s/\$EQUIVCPTRUNC\(([^()]+),([^()]+),([^()]+)\)/\$PP(CHILD)[$1] = ($3) ? 0 : \$PP(PARENT)[$2]/g;
return $good if !$bflag;
# parse 'bad' code
$bad =~ s/\$EQUIVCPOFFS\(([^()]+),([^()]+)\)/if( \$PPISBAD(PARENT,[$2]) ) { \$PPSETBAD(CHILD,[$1]); } else { \$PP(CHILD)[$1] = \$PP(PARENT)[$2]; }/g;
$bad =~ s/\$EQUIVCPTRUNC\(([^()]+),([^()]+),([^()]+)\)/ if( ($3) || \$PPISBAD(PARENT,[$2]) ) { \$PPSETBAD(CHILD,[$1]); } else {\$PP(CHILD)[$1] = \$PP(PARENT)[$2]; }/g;
'if ( $PRIV(bvalflag) ) { ' . $bad . ' } else { ' . $good . '}';
}),
sub { equivcpoffs_parse(@_, qw(PARENT CHILD)) }),

PDL::PP::Rule->new("BackCode", ["EquivCPOffsCode","BadFlag"],
"create BackCode from EquivCPOffsCode",
Expand Down Expand Up @@ -1466,21 +1469,7 @@ EOD
# forward code puts BAD/0 into the child, and reverse code refrains
# from copying.
# --CED 27-Jan-2003
#
# this just reverses PARENT & CHILD in the expansion of
# the $EQUIVCPOFFS macro (ie compared to Code from EquivCPOffsCode)
sub {
my ($good, $bflag) = @_;
my $bad = $good;
# parse 'good' code
$good =~ s/\$EQUIVCPOFFS\(([^()]+),([^()]+)\)/\$PP(PARENT)[$2] = \$PP(CHILD)[$1]/g;
$good =~ s/\$EQUIVCPTRUNC\(([^()]+),([^()]+),([^()]+)\)/if(!($3)) \$PP(PARENT)[$2] = \$PP(CHILD)[$1] /g;
return $good if !$bflag;
# parse 'bad' code
$bad =~ s/\$EQUIVCPOFFS\(([^()]+),([^()]+)\)/if( \$PPISBAD(CHILD,[$1]) ) { \$PPSETBAD(PARENT,[$2]); } else { \$PP(PARENT)[$2] = \$PP(CHILD)[$1]; }/g;
$bad =~ s/\$EQUIVCPTRUNC\(([^()]+),([^()]+),([^()]+)\)/if(!($3)) { if( \$PPISBAD(CHILD,[$1]) ) { \$PPSETBAD(PARENT,[$2]); } else { \$PP(PARENT)[$2] = \$PP(CHILD)[$1]; } } /g;
'if ( $PRIV(bvalflag) ) { ' . $bad . ' } else { ' . $good . '}';
}),
sub { equivcpoffs_parse(@_, qw(CHILD PARENT)) }),

PDL::PP::Rule::Returns::Zero->new("CanVaffine", "EquivCPOffsCode"),
PDL::PP::Rule::Returns::One->new("CanVaffine"),
Expand Down
2 changes: 1 addition & 1 deletion Libtmp/Minuit/minuit.pd
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ Example:
$iflag = mn_excm('set strategy',$arglist);
# each command can be specified by a minimal string that uniquely
# identifies it (see Chapter 4 of Minuit manual). The comannd above
# identifies it (see Chapter 4 of Minuit manual). The command above
# is equivalent to:
$iflag = mn_excm('set stra',$arglis);
Expand Down

0 comments on commit 0a2f99a

Please sign in to comment.