What is RATFOR?
RATFOR statements
IF-ELSE statement
WHILE statement
FOR statement
REPEAT-UNTIL statement
BREAK and NEXT statements
SWITCH construct
Statement Grouping and Null Statments
FREE-FORM input
Comments
Character Translation
STRING Data Type
Quoted Character Strings
DEFINE macro
Other macros
INCLUDE statement
Implementation
Conclusions
Other Reference Literature
:What is RATFOR?

RATFO�  i�  � preprocesso� fo� FORTRAN� It� primar� purpos� i� �
t� encourag� readabl� an� well-structure� cod� whil� takin� �
advantag堠  o栠 th堠  universality����  portability���  an�  �
efficienc��  o� FORTRAN�� Thi� i� don� b��  providin�  th�  �
contro�  structure�  no�  availabl�  i�  bar� FORTRAN�� an� b� �
improvin� th� "cosmetics� o� th� language�

RATFOҠ  allow� fo� al� th� feature� o� norma� FORTRAN�� plu� �
make� availabl� thes� contro� structures�

    "if"-"else"
    "while", "for", and "repeat"-"until" for looping
    "break" and "next" for controlling loop exits
    "switch" construct for allowing selection of alternatives
     statement grouping with braces







Th� cosmeti� aspect� o� RATFO� hav�  bee�  designe�  t�  mak�  �
i� concis� an� reasonabl� pleasin� t� th� eye�

         free form input
         unobtrusive comment convention
         translation of >, <=, etc. into .GT., .LE., etc.
         string data type
         quoted character strings
         "define" statement for symbolic constants
         "include" statement for including source files

RATFOҠ  i�  implemente�  a�  �  preprocesso� whic� translate� �
th� abov� feature� int� FORTRAN�� whic� ca� the� b�  fe�  int�  �
almos�  an� FORTRAN compiler�

:RATFOR statements

Eac�  o�  th� RATFO� feature� wil� no� b� discusse� i� mor� �
detail�� I� th� following�� �  "statement�  i�  an��  lega�  �
statemen�  i�  FORTRAN�� assignment�  declaration�  subroutin�  �
call��  I/O� etc.� o� an� o� th� RATFO� statement� themselves�  �
An��  FORTRAN  o�  RATFO�  statemen�  o� grou�  o�  thes� ca� �
b� enclose� i� brace� ({}� o� bracket� ([]� -- t� mak� i� � �
compoun� statement�� whic� i� the�  equivalen�  t�  �  singl� �
statemen� an� usabl� anywher� � singl� statemen� ca� b� used�


:IF-ELSE statement

RATFOҠ�  provide�  a�  "else�� statemen� t� handl堠 th� �
constructio� "i� � conditio� i� true� d� thi� thing� otherwis� �
d� tha� thing"�  Th� synta� i�

         if (legal FORTRAN condition)
              statement(s)
         else
              statement(s)

wher�  th�  els�  par�  i�  optional�� Th� "lega� FORTRAN �
condition�� i� anythin� tha� ca� legall� g� int� � FORTRAN  �
logica�  IF��  Th�  RATFO� statement�  ma�  b�  on� o� mor� �
vali� RATFO� o� FORTRAN statement� o� an� kind�� I� mor� tha� �
on� statemen� i� desired� th�  statement�  mus� b� enclose� b� �
braces�

See the example on the next screen.





IF-ELSE example�

         if (a > b)
              {
              k = 1
              call remark (...)
              }
         else if (a < b)
              {
              k = 2
              call remark (...)
              }
         else
              return �

:WHILE statment

Th� whil� statemen� i�  simpl� � loop�� "whil� som� conditio� �
i� true� repea� thi� grou� o�  statements"�  Th� synta� i�

         while (legal FORTRAN condition)
                statement(s)

A�  wit�  th�  if��  "lega� FORTRAN condition� i� somethin� �
tha� ca� g� int� � FORTRAN logica� IF�� Th� conditio� i�  �
teste�  befor�  executio� o�  an�  o� th� RATFOҠ statements� �
s� i� th� conditio� i� no� met� th� loo� wil� b� execute� zer� �
times�� Also�� a� wit�  th�  IF��  th�  RATFO� statement�  ca�  �
b�  an�  vali�  RATFO� o� FORTRAN constructs� I� mor� tha� on� �
statemen� i� desired�� th�  statement�  mus�  b�  enclose�  b� �
braces� Fo� example�

         while (getc(c) != EOF)
              {
              c = cnvt (c)
              call putc (c)
              }
:FOR statment

Th�  "for�  statemen�  i�  simila�  t� th� "while� excep� tha� �
i� allow� explici� initializatio� an� incremen�  step�  a�  �
par�  o�  th� statement� Th� synta� i�

         for (init; condition; increment)
              statement(s)

wher�  "init�  i�  an�  singl�  FORTRAN statemen� whic� get� �
don� onc� befor� th� loo� begins�� "Increment� i� an��  singl�  �
FORTRAN�  statemen� whic�  get�  don� a� th� en� o� eac� pas� �
throug� th� loop�� befor� th� test�� "Condition�� i� agai� �
anythin� tha� i� lega�  i�  �  logica�  IF�� An��  o�  init�  �
condition���  an� incremen� ma�� b� omitted�� althoug� th� �
semicolon� mus� remain� � non-existen� conditio� i� treate� a�  �
alway� true��  s� "for� �� �� )� i� a� indefinit� repeat�� Th� �
"for�� statemen� i� particularl� usefu� fo� backwar� loops� �
chainin�  alon�  lists��  loop� tha�  migh�  b�  don� zer� �
times�� an� simila� thing� whic� ar� har� t� expres� wit� � D� �
statement�

See the next screen for examples.

Her� ar� tw� example� o� "for� loops�

         for (i=1; getarg(i, file, MAXLINE) != EOF; i=i+1)
              {
              int = open (file, READ)
              while (getlin (line, int) != EOF)
                   { �                    for (j=80; j>0; j=j-1)
                      call putc (line(j))
                   }
              call close (int)
              }

Th� abov� cod� simpl�� read� card� fro� � lis� o� files�  �
reverse�  th� orde�  o�  th� characters�� an� write� th� card� �
ont� � standar� outpu� file� (Th� "!=� mean� .NE.�


:REPEAT-UNTIL statement

Th� "repeat-until� statement� allo�  fo�  repetitio�  o�  �  �
grou�  o� statement� unti� � specifie� conditio� i� met�� Th� �
synta� is�
         repeat
              statement(s)
         until
              condition

Th�  "until�  i�  optional�  Onc�  again�  i�  mor�  tha�  on�  �
RATFO� statemen� i� desired�� th� statement� mus� b� enclose� �
b� brackets��  I� th�  "until�  par�  i�  omitted�  th� resul� �
i� a� infinit� loo� whic� mus� b� broke� wit� � "break�� o�  �
"next�  statemen�  (se�  below)�  E.g.:

         repeat
              {
              call putc (BLANK)
              col = col + 1
              }
              until (tabpos(col,tabs) == YES)
:BREAK and NEXT

RATFOҠ  provide�  statement�  fo�  leavin�  �  loo� earl� an� �
fo� beginnin� th� nex� iteration�

"Break� cause�  a�  immediat�  exi�  fro�  whateve�  loo�  i�  �
i� containe�  i�  (whic�  ma��  b� � "while"�� "for"�� o� �
"repeat")�� Contro� resume� wit� th� nex� statemen� afte�  th�  �
loop��  Onl�  on�  loo�  i� terminate�  b�  �  "break"��  eve�  �
i�  th� "break� i� containe� insid� severa� neste� loops�� Fo� �
example�

         repeat
              {
              if (getc(c) == EOF)  break
              ...
              }








"Next�� i� � branc� t� th� botto� o� th� loop�� s�  i�  cause�  �
th� nex�  iteratio�  t�  b�  done��  "Next�� goe� t� th� �
conditio� par� o� � "while� o� "until"�� t� th� to� o� a� �
infinit� "repeat�  loop��  an�  t� th� reinitializ� par� o� � �
"for"� Fo� example�

         for (i=1; i<10; i=i+1)
              {
              if (array(i) == BLANK)  next
              ...
              }


:SWITCH Construct

A�  assortmen�  o�  alternative�  ma�  b�  specifie�  usin� �
th� switc� construct� Th� synta� is�

    switch (expr):
       {
       case a: statement
       case b: statement
       case c: statement
       ....
       default: statement
       }

Th� "default� cas� i� optional�� �

    switch (i):
       {
       case 25: do_this_stuff
       case -2: do_this_stuff
       default: otherwise do_this
       }
:Statement Grouping and Null Statements

RATFO� allow� � grou� o� statement� t� b� treate� a� � uni� b� �
enclosin� the� i� brace� -- � an� }�� Thi� i� tru� throughou� �
th� language�� whereve� � singl� RATFO� statemen� ca� b� used� �
ther� coul� als� b� severa� enclose� i� braces.

For example:

              if (x > 100)
              {
              call error (...)
              err = 1
              return
              }
    �I�  brace�  ar�  no�  vali�  character�  i�  th�  loca� �
operatin� system�� th� character� "$(� an� "$)� ma�� b� use� �
instea�  o�  "{� an� "}� respectively�




RATFOҠ  als�  allow�  fo� nul� statements�� mos� usefu� afte� �
"for�� an� "while� statements�� � semicolo� alon� indicate� �  �
nul� statement� Fo� instance�

              while (getlin(line, int) != EOF) ;

woul�  rea�  line�  fro� � fil� unti� th� end-of-fil� wa� �
reached and

              for (i=1; line(i) == BLANK; i=i+1) ;

position� afte� leadin� blank� i� � line�

:FREE-FORM input

Statement� ma� b� place� anywher� o� � lin� an� severa�  ma� �
appea�  o�  on�  lin�  i�  the�  ar�  separate� b� semicolons� �
N� semicolo� i� neede� a�  th�  en�  o�  eac�  lin�  becaus�  �
RATFO� assume�  ther�  i�  on� statemen� pe� lin� unles� tol� �
otherwise�� RATFO� will� however� continu� line� whe� i� seem�  �
obviou�  tha� the� ar� no� ye� done�

An��  statemen�  tha�  begin�  wit�  a�  all-numeri� fiel� i� �
assume� t� b� � FORTRAN labe� an� i� place� i� column�  1-�  �
upo� output�

Statement�  ma��  b�  passe� throug� th� RATFOҠ compile� �
unaltere� b�� insertin� � percen� sig� (%�� a� th�  firs�  �
characte�  o�  th� line��  Th�  percen� wil� b� removed�� th� �
res� o� th� lin� shifte� on� positio� t� th� left�� an�  th�  �
lin堠  sen�  ou�  withou�  an�� changes���  Thi�  i�  �  �
convenien�  wa�� t� pas� regula� FORTRAN� o� assembl�� cod� �
throug� th� RATFO� compiler�


:Comments

�� shar� characte� "#� i� � lin� mark�  th�  beginnin�  o�  � �
commen�  an�  th�  res�  o�  th�  lin�  i�  considere� t� b� �
tha� comment�� Comment� an� cod� ca� co-exis� o�  th�  sam�  �
line�  For example,

       function dummy (x)
       # I made up this function to show some comments
       dummy = x         #I am simply returning the parameter
       return
       end
              �
:Character Translation

Sometime�  th�  character� >�� <=�� etc� ar� easie� t� rea� i� �
FORTRAN� conditio� statement�  tha�  th�  standar�  FORTRAN  �
EQ.�� .LT.��  etc.��  RATFO�  allow�  eithe�  convention�  I� �
th� specia� character�  ar�  used��  the�  ar�  translate�  i�  �
th�  followin� manner�

              ==          .EQ.
              != ^= ~=    .NE.
              <           .LT.
              >           .GT.
              <=          .LE.
              >=          .GE.
              |           .OR.
              &           .AND.

For example:
              for (i=1; i<= 5; i=i+1)
              ...
              if (j != 100)
              ...
:STRING Data Type

Al�   characte�   array�   i�   RATFO�  ar�  sequence�  o�  �
ASCIɠ characters��� store� right-adjusted�� on� pe�  arra�  �
element��  wit� th�  strin�  terminate�  wit�  a� EO� marker� �
A� automati� wa� t� initializ� strin� character� array� i� �
provided� Th� synta� is�

    string name "characters"
    or
    string name(n) "characters"

RATFOҠ wil� defin� nam� t�  b�  �  characte�  (or��  mor�  �
likely�� integer��  arra� lon� enoug� t� accomodat� th� ASCI� �
code� fo� th� give� characte� string�� on� pe� element�� Th� �
las�  wor�  o�  nam� i�  initialize�  t�  EOS�� I� � siz� i� �
given�� nam� i� declare� t� b� a� intege� arra� o� siz� 'n'�  �
I�  severa�  strin�  statement� appea�  consecutively��  th�  �
generate� declaration� fo� th� arra� wil� preced� th� dat� �
statement� tha� initializ� them�

See the next screen for examples.

For example, the declarations:

         string errmsg "error"
         string done "bye"

would be converted by RATFOR into the FORTRAN:

         integer error(6)
         integer done(4)
         data error(1), error(2), error(3), error(4), �          error(5), error(6) /LETE, LETR, LETR, LETO, LETR, EOS/
         data done(1), done(2), done(3), done(4) /LETD, LETO,
         LETN, LETE, EOS/


:Quoted Character Strings

Tex� enclose� i� matchin� doubl� o� singl� quote� i� converte� �
t� nH..� format� bu� i� otherwis� unaltered�  Fo� instance,

         call remark ("Error detected")
would translate to
         call remark (14hError detected)

and
         data string /"Can't find answer"/
would become
         data string /17hCan't find answer/

I� th� loca� operatin� syste� doe� no� suppor� bot�  uppe�  �
an�  lowe� cas�  hollerit�  strings�� a� escap� mechanis� i� �
generall� provide� t� allo� th� use� t� indicat� case�

Som� operatin� system� ar� no� capabl� o� findin� th�  en�  o�  �
� FORTRAN�  hollerit�  string��  I�  thi� cas� i� ma�� b� �
necessar�� fo� th� use� t� mar� th� en� o� he� quote� strin� �
wit� �  specifi�  character� suc� a� � period�
:DEFINE

An��  strin�  o� alphanumeri� character� ca� b� define� a� � �
name�� thereafter�� wheneve� tha� nam�  occur�  i�  th�  inpu�  �
(delimite�  b� non-alphanumerics�  i� i� replace� b� th� res� �
o� th� definitio� line� Th� synta� is�

define(name, replacement string)

whic蠠  defin�  "name��  a�  �  macr�  whic�  wil젠  b�   �
replace�   wit� "replacemen�  string�  whe�  encountere�  i�  �
th�  sourc�  files� A� a simple example:

         define(ROW,10)
         define(COLUMN,25)

         dimension array (ROW, COLUMN)
and

         define(EOF,-1)
         if (getlin(line, fd) == EOF) ....


Definition� ma� b� include� anywher� i�  th�  code��  a�  lon�  �
a� the�  appea�  befor�  th�  define� nam� occurs�� Th� name� �
o� macr� ma� contai� letters�� digits�� periods� an� underlin� �
characters��  bu�  mus� star�  wit�  � letter� Uppe� an� lowe� �
case� AR� significan� (thu� EO� i� no� th� sam� a� eof)� �
An�� occurrence� o� th� string� '$n� i� th� replacemen� text�  �
wher�  � <�  �  <�  9�� wil� b� replace� wit� th� nt� argumen� �
whe� th� macr� i� actuall� invoked� Fo� example�

              define(bump, $1 = $1 + 1)

will cause the source line

              bump(i)

to be expanded into

              i = i + 1


:Other macros

In addition to define, four other built-in macros are provided:

arith(x,op,y) perform�  th�  "integer�  arithmeti�  specifie�  �
��������������b�  o� (+,-,*,/�  o�  th�  tw�  numeri� operand� �
��������������an� return� th� resul� a� it� replacement�

incr(x)�������convert� th� strin� � t�  �  number��  add�  on�  �
��������������t  it���  an� return�  th� valu� a� it� �
��������������replacemen� (a� � characte� string)�

ifelse(a,b,c,d�� compare� � an� � a� characte� strings��  i�  �
��������������the�  ar� th�  same�� � i� pushe� bac� ont� th� �
��������������input� els� � i� pushe� back�

substr(s,m,n) produce� th� substrin� o� � whic� start� a� �
��������������positio�  � (wit�  origi�  one)��  o� lengt� n� �
��������������I� � i� omitte� o� to� big�� th� res� o� th� �
��������������strin� i� used�� whil�  i� � i� ou� o� rang� th� �
��������������resul� i� � nul� string�
:INCLUDE

File�  ma��  b�  inserte�  int� th� inpu� strea� vi� th� �
"include� command� Th� statemen�

         include filename
or
         include "filename"

insert� th� fil� foun� o� inpu� fil� "filename�� int� th� �
RATFOҠ  inpu� i�  plac�  o�  th�  includ�  statement��  Thi�  �
i� especiall� usefu� i� insertin� commo� blocks�

See the example on the next screen.








Include example:

         function exampl (x)
         include comblk
         exampl = x + z
         return
         end

might translate into


         function exampl (x)
         common /comblk/ q, r, z
         exampl = x + z
         return
         end

:Implementation

RATFO� wa� originall� writte� i� C��  �  high-leve�  language�  �
o� th�  Uni�  operatin�  system�� Ou� versio� i� writte� i� �
RATFO� itself� originall� brough� u� b� � bootstra� writte� i� �
FORTRAN�

RATFOҠ generate� cod� b� readin� inpu� file� an� translatin�  �
an� RATFO�  keyword�  int�  standar�  FORTRAN�  Thus�  i�  th� �
firs� toke� (word� o� � sourc� lin� i� no� � keywor� (lik� �
"for"��  "while"��  etc.�� th�  entir�  statemen� i� simpl� �
copie� t� th� outpu� wit� appropriat� characte�  translatio�  �
an�  formatting��  RATFO�  know�  ver�   littl� FORTRAN�  an�  �
thu� doe� no� handl� an� FORTRAN erro� detection�� Error� i� �
RATFO� keywor� synta� ar� generall�  note�  b�  �  messag�  t�  �
th� user'�  termina�  alon�  wit�  a� indicatio� o� th� sourc� �
lin� numbe� whic� cause� th� problem�

:Conclusions

RATFOҠ  demonstrate�  tha�  wit�  modes�   effor�   FORTRAN-�
base� programmer�  ca�  increas� thei� productivit� b� usin� � �
languag� tha� provide� the�  wit�  th�  contro�  structure�  �
an�  cosmeti�  feature� essentia�  fo� structure� programmin� �
design�� Debuggin� an� subsequen� revisio�  time�  ar�  muc�  �
faste�  tha�  th�  equivalen�  effort�  i� FORTRAN��  mainl�  �
becaus� th� cod� ca� b� easil� read�� Thu� i� become� easie�  �
t�  writ�  cod�  tha�  i�   readable�   reliable�   an�   eve� �
estheticall��   pleasing��   a�   wel�   a�   bein�  portabl�  �
t�  othe� environments�

:Other Reference Literature

1)  Kernighan��  Bria�  W.��  "RATFOR--�  Preprocesso�  fo�  �  ������Rationa� FORTRAN"��  Softwar� - Practic� an� Experience� �
����Vol� 5� � (Oct-De� 75)� pp� 395-406�

2)  Kernighan��  Bria�  W��  an�  P�  J�  Plauger�   "Softwar�   �
����Tools"� Addison-Wesle� Publishin� Company� Reading� Mass.� �
����1976�

3) �Th� RATFO� use� documen�

4) �Th� Uni� comman� "rc� i� th� Uni� Manua� (RC(I)�

5)� Thi� HEL� fil� wa� buil� fro� th� RATFOR.PR� fil� fro� th� �
����"Softwar� Tools� CP/� disks� Volum� 24� fil� 2� i� th� BD� �
����"C� onlin� catalogue� BDSCAT.ALL.

Th� onl� on� o� thes� whic� i� likel� t� b� readil� availabl� �
t� mos� microcompute� user� i� th� "Softwar� Tools� book� Thi� �
i� highl� recommende� a� � genera� tex� eve� i� yo�� ar� no� �
particularl� intereste� i� RATFOR.