Simple nesting preprocessor in Perl

Steve Smythe ssmythe at docent.com
Thu Oct 11 19:04:40 CDT 2001


I just wrote a simple nesting preprocessor in Perl.  You guys have anything
you can add to it?
 
Steve
 
 
Example file to pre-process:
  _____  

Hello is not yet defined
!ifndef HELLO
Hello is not defined
!else
Hello is defined
!endif
Defining HELLO
!define HELLO
!ifdef HELLO
Hello is defined
!else
Hello is not defined
!endif

  _____  

 
 
Here's the code for the pre-processor:
  _____  

#/usr/local/bin/perl -w
 
############################################################################
##
# Init
############################################################################
##
sub Init {
 $meta="!";
 
 $sp=0;
 @s_var=();
 @s_if=();
 @s_else=();
 @s_pr=();
}
 

############################################################################
##
# Process
############################################################################
##
sub Process {
 my @F=();
 my $nf=0;
 my $var="";
 my $val="";
 my $pr_flag=1;
 
 while(<>) {
  chomp;
#  print "$sp: $_\n";
  if (/^${meta}/) {
   if (/^${meta}define /) {
    @F=split;
     $nf=@F <mailto:$nf=@F> ;
    if($nf == 2) {
     $var=$F[1];
     $ENV{$var}="TRUE";
    }
    if($nf == 3) {
     $var=$F[1];
     $val=$F[2];
     $ENV{$var}=$val;
    }
 
   }
   if (/^${meta}undef /) {
    @F=split;
     $nf=@F <mailto:$nf=@F> ;
    if($nf == 2) {
     $var=$F[1];
     delete($ENV{$var});
    }
 
   }
   if (/^${meta}ifdef /) {
    @F=split;
     $nf=@F <mailto:$nf=@F> ;
    if($nf == 2) {
     $s_pr[$sp]=$pr_flag;
     $sp++;
     $s_var[$sp]=$F[1];
     if (exists($ENV{$s_var[$sp]})) {
      $s_if[$sp]=1;
      $s_else[$sp]=0;
      $pr_flag=1;
     } else {
      $s_if[$sp]=0;
      $s_else[$sp]=1;
      $pr_flag=0;
     }
    }
   }
   if (/^${meta}ifndef /) {
    @F=split;
     $nf=@F <mailto:$nf=@F> ;
    if($nf == 2) {
     $s_pr[$sp]=$pr_flag;
     $sp++;
     $s_var[$sp]=$F[1];
     if (!(exists($ENV{$s_var[$sp]}))) {
      $s_if[$sp]=1;
      $s_else[$sp]=0;
      $pr_flag=1;
     } else {
      $s_if[$sp]=0;
      $s_else[$sp]=1;
      $pr_flag=0;
     }
    }
   }
   if (/^${meta}else/) {
    if($s_else[$sp] == 1) {
     $pr_flag=1;
    } else {
     $pr_flag=0;
    }
   }
   if (/^${meta}endif/) {
    $sp--;
    if ($sp < 0) {
     $sp=0;
     $pr_flag=1;
    }
    $pr_flag=$s_pr[$sp];
   }
  } else {
   if ($pr_flag == 1) {
    print "$_\n";
   }
  }
 }
}
 

############################################################################
##
# MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN
#
############################################################################
##
Init();
Process();

  _____  

 
 
 

 


Steve Smythe
Sr. CM Engineer
ssmythe at docent.com <mailto:ssmythe at docent.com> 
W:650-934-9546
C:925-699-6822 
Docent, Inc.
2444 Charleston Road
Mountain View, CA 94042-1622
http://www.docent.com <http://www.docent.com/> 

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/archives/pikes-peak-pm/attachments/20011011/67c88fcf/attachment.htm


More information about the Pikes-peak-pm mailing list