Quale versione del fortran utilizzi?
A cura di Giuseppe Ciaburro
Pubblicato il 20/11/2001
Uno script perl che aggiunge dello spazio allo statement IF.
Lo script presente in questa pagina ci permette di modificare un codice fortran aggiungendo dello spazio in un costrutto IF eseguendo ad esempio il seguente cambiamento:
if(i.lt.20.or.j.gt.30)then
diventa
if (i .lt. 20 .or. j .gt. 30) then
L'autore dello
script è Kate Hedstrom
(kate@ahab.rutgers.edu).
Per poter utilizzare lo scritp salvarlo con estensione .pl e cambiare la prima
riga dello script con il path del compilatore perl installato sul vostro sistema.
Quindi digitare la seguente stringa al prompt di comando:
ifspace [-l linelength] [-c] [file ...] > outfile
L'opzione -l specifica
la lunghezza massima di ogni linea di codice (di default è di 72 caratteri).Quindi
sostituire linelength con il numerodi caratteri desiderato.
L'opzione -c converte nel formato del fortran 90 come mostrato di seguito:
if (i < 20 .or. j > 30) then
Al posto di file il nome del file da convertire.
![]()
#!/usr/local/bin/perl
#
# Usage: ifspace [-l linelength] [-c] [file ...] > outfile
#
# Add space around 'if' statements in a Fortran program, for instance
# changing:
#
# if(i.lt.20.or.j.gt.30)then
#
# to
#
# if (i .lt. 20 .or. j .gt. 30) then
#
# The -l linelength option specifies the maximum length of your
# Fortran lines. The default is 72.
#
# The -c option converts to f90 style, i.e., the above example becomes
#
# if (i < 20 .or. j > 30) then
#
# It will strip off trailing blanks.
#
# Bugs:
# It checks to see if lines are too long, but complains
# to STDERR instead of fixing them.
# It doesn't always do exactly what you want for multi-line if
# statements. For instance,
#
# if ((plot_s .or. plot_t .or. plot_rho .or. plot_curl)
# & .and. .not. bigpix) call frame
#
# becomes
#
# if ((plot_s .or. plot_t .or. plot_rho .or. plot_curl)
# & .and. .not. bigpix) call frame
#
# Written by:
# Kate Hedstrom
# kate@ahab.rutgers.edu
#
#
$line_len = 72;
require 'getopts.pl';
do Getopts('l:c');
if ($opt_l) {
$line_len = $opt_l;
}
if ($opt_c) {
$convert = 1;
}
$* = 1; #enable multi-line mode
#
# Loop over all statements
#
$lookahead = <>;
statement: while ($_ = do get_line()) {
#
# Delete trailing spaces
#
s/ *$//;
#
# Skip comments and blank lines
#
if (/^[*c#!]|^$/i) {
print;
next;
}
#
# Replace tabs with spaces
#
s/\t/ /g;
#
# Put space around =
#
s/\s*=\s*/ = /;
#
# Look for if-then-else and do loops
#
if (/^(.{6} *(else)? *if) *\(/i) {
$_ = $';
$before = $1;
$left = 1;
do find_match();
if ($left != 0) { die "Illegal if statement"; }
# add spaces inside parentheses
$statement = '';
$_ = $parexp;
while (/( *)\.(gt|lt|eq|ne|le|ge|and|or|not|eqv|neqv)\.( *)/i) {
$begin = $`;
$middle = '.'.$2.'.';
$end = $';
if ($convert) {
MATCH: {
if ($middle =~ /\.gt\./) { $middle = '>'; last MATCH; }
if ($middle =~ /\.ge\./) { $middle = '>='; last MATCH; }
if ($middle =~ /\.lt\./) { $middle = '<'; last MATCH; }
if ($middle =~ /\.le\./) { $middle = '<='; last MATCH; }
if ($middle =~ /\.eq\./) { $middle = '=='; last MATCH; }
if ($middle =~ /\.ne\./) { $middle = '/='; last MATCH; }
} }
$statement .= $begin.' '.$middle.' ';
$_ = $end;
}
$statement .= $_;
} else {
print;
next;
}
#
# Put string back together
#
$after =~ s/^ *//;
$_ = $before.' ('.$statement.' '.$after;
#
# Check for falling off the edge of the world
#
@lines = split(/^/);
foreach $line (@lines) {
if (length($line) > $line_len+1 && # +1 for '\n'
!(substr($_,0,73) =~ /!/)) { # don't complain about long comments
print STDERR "Line ", $.-1, " too long. Fix it\n";
}
#
# Delete trailing spaces, then print
#
$line =~ s/ *$//;
print $line;
}
}
#
# end main, begin subs
# get a line, combining continuation lines
#
sub get_line {
$thisline = $lookahead;
if ($lookahead) {
line: while ($lookahead = <>) {
if ($lookahead =~ /^ \S/) {
# if ($lookahead =~ /^ \S|^$|^[*c#!]/i) {
$thisline .= $lookahead;
}
else {
last line;
}
}
}
$thisline;
}
#
# find matching parentheses
#
sub find_match {
$parexp = '';
while (/[()]/) {
$parexp .= $`;
$parexp .= $&;
$_ = $';
if ($& eq "(") { $left++; }
else { $left--; }
if ($left == 0) { last; }
}
$after = $_;
}
![]()