wiki:ratfor

FORTRAN/TK and Ratfor

Ratfor is a FORTRAN 77 preprocessor designed in the 70s to compensate some of the weird limitations of the FORTRAN 66 (later of the 77) standard, allow entirely structural programming and implement some new functionality.
From the official homepage:

Ratfor is preprocessor for FORTRAN code that allows us to use C-like flow expressions. Jon Claerbout believes that Ratfor is the best available expository language for mathematical algorithms. Ratfor was invented by Brian Kernigham who also co-wrote the first C book. You will not really need precise definitions of Ratfor to use it. Statements on a line may be separated by a ";". Statements may be grouped together with braces {}. Do loops do not require statement numbers because {} defines the range, etc. The Fortran relational operators .gt.,.ge,.ne., etc. may be written >,<=,!=,etc. Unfortunately, the Ratfor switch statement conflicts with implicit undefined declaration, consequently we never use it. Ratfor also frees you from FORTRANs strict indentation rules. Anything from a # to the end of the line is a comment. Ratfor offers quite a few more features more and we strongly recommend it to any FORTRAN programmer.

There's a documentation about it.

It was designed for standard FORTRAN 77, so you can use it with the Open Watcom FORTRAN 77 dialect, too. However a lot of Ratfor's functionality is already implemented in the dialect through FORTRAN extensions - in a way that looks native to FORTRAN - in my opinion even better than Fortran 90. Although FORTRAN/TK makes heavy use of FORTRAN extensions - Open Watcom FORTRAN has comparable functionality to Fortran 90 - it seems to be usable with Ratfor.

Pros

  • Overcomes FORTRAN's strict source format (the separation in several source columns) from the punchcard age. These are still restrictions of the Open Watcom dialect, too. Ratfor merges a source line (with all of its continuations) and rearranges it in the strict FORTRAN source format.
  • You can write more than one statement per line by separating them with ";".
  • It implements a C-like for-statement that's not available in Open Watcom FORTRAN.
  • Ratfor's structure statements (while, repeat/until, if, switch, for) feature a C-like syntax. DO-loop blocks use {}, too. Combined with the "#"-comment (comparable to Open Watcom's/Fortran 90's "!"-comment) it even looks a bit like Perl... This might or might not be an advantage for you.
  • The define(x,y)-statement allows you to replace the "alpanumeric" string x within the source by an arbitrary string y. Open Watcom FORTRAN only supports PARAMETER-constants.

Cons

  • You might prefer Open Watcom's FORTRAN-like syntax for its extensions over Ratfor's C-like syntax.
  • The switch-statement of Ratfor has similar restrictions as Open Watcom's SELECT CASE regarding character expressions. However - though the switch-statement is translated to IF-statements - it cannot handle CHARACTER expressions at all. Also, contrary to SELECT CASE, "switch" does not support case-ranges.
  • Some of Ratfor's statements, especially "include", conflict with Open Watcom FORTRAN's statements. "include" parses a given file by the preprocessor. If you want to use Open Watcom's "include" - to include ordinary FORTRAN files - you have to use "%" as the first character of a line to force the preprocessor to insert the (unmodified) line in the output file. So %c$include, %C$include, %*$include (inserts a pragma include) or "% include" (include-statement for program units; does the same as %c$include) will do the job. If you specify the -C commandline parameter for Ratfor, you can also use #$include... everywhere on the line - it will be translated to a "C"-comment.
  • Code generated by Ratfor using a lot of the structure-statements might be (slightly) slower than code with Open Watcom statements because they are translated to relatively simple IF/GOTO-statements that might not be as highly optimized as native Open Watcom FORTRAN structure-statements.
  • Compiler errors cannot always be assigned to a Ratfor-line and -statement so easily.

Compiling Ratfor

At first download the source code and extract it - it's public domain: http://sepwww.stanford.edu/ftp/sep-distr/ratfor77.tar.gz
It's written in standard C, so it can be easily compiled with Open Watcom C or most other C compilers.
For Open Watcom C, assuming you initialized all necessary environmental variables, you would have to issue:

 wcl386 getopt.c lookup.c rat4.c -fe=ratfor77.exe -DF77 -DS_CHAR=char -DGNU

It should be compiled for your host platform. Use the -l-switch to crosscompile for a certain platform.

FORTRAN/TK macros

There's a small Ratfor file included with FORTRAN/TK (at least in the subversion repository): fortrantk.r.
Include it to be able to use some "macros" that kind of improve the readability of FORTRAN/TK code. For instance you can use a "z" at the end of character constants or separated from a variable symbol by a space instead of the "dn" to mark the end of a parameter list. Also, you can use "NL" as a newline character constant (it will be replaced by "char(13)char(10)") - this also isn't possible with ordinary PARAMETER-constants. To let FORTRAN/TK code look more like Tcl/Tk (with it's attribute-value-pairs) you can use some attributes followed by an underscore instead of the ordinary character constant followed by a colon. There are more or less just the standard attributes for now but you can easily extend the list yourself. There's an example of the FORTRAN/TK "example" program translated to Ratfor code below.

FORTRAN/TK example program

Here's the simple "example" program of FORTRAN/TK translated to Ratfor code which demonstrates some of Ratfor's features and should give you an impression of its look and feel:

# FORTRAN/TK example
# for OpenWatcom FORTRAN/77 and the Ratfor77 preprocessor
# by Robin Haberkorn

include '..\fortrantk.r'
%c$include fortrantk.fap
%c$include fortrantk.fi

character*256 dum, dum2

# Init code:
if(TkInit('runtime'c)) stop 'FORTRAN/TK couldn''t get initialized!'

call CreateWindow()

# The main event loop:
repeat {
 # we can't use switch for the event handling...
 cmpval = TkWait()
 if(cmp('Quit'c)) {
  write(*,'(''EVENT: Exiting...'')'); break

 } else if(cmp('hellobtm_click'c)) {
   dum2 = TkCget('.hellobtm'c, '-text'z)
   write(*,'(''EVENT: The "'',A,''" button was clicked!'')') dum2(:ntlen(dum2))
   dum = TkMessageBox(message_ 'You pressed the "'//dum2(:ntlen(dum2))//'" button!'//NL// _
                               'Please choose "Yes" or "No".'c, _
                      title_   'FORTRAN/TK example'c, _
                      type_    'yesno'z)
   write(*,'(''You choosed "'',A,2H".)') dum(:ntlen(dum))

 } else if(cmp('changebtm_click'c)) {
   write(*,'(''EVENT: changebtm_click'')')
   dum = TkGet('.entry1'z)
   call TkConfig('.hellobtm'c, text_ dum z)
   write(*,'(''Text of .hellobtm changed to: "'',A,1H")') dum(:ntlen(dum))

 } else write(*,'(''EVENT(unknown): '',A)') cmpval(:ntlen(cmpval))
}

call TkDestroy('.'z)            # Exit code
if(TkUnload()) write(*,'(''FORTRAN/TK couldn'',1H'',''t get unloaded!'')')

end

subroutine CreateWindow()
%c$include fortrantk.fi

 # Change the window title and its size:
 call TkWm('title'c, '.'c, 'FORTRAN/TK example'z)
 call TkWm('geometry'c, '.'c, '=300x200'z)

 # Create the buttons and entry widgets and pack them to the window:

 call TkPack(TkButton('.hellobtm'c, _
                      text_      'Hello world'c, _
                      rexx_      'hellobtm_click'z), _
             TkButton('.exitbtm'c, _
                      text_      'EXIT'c, _
                      underline_ '0'c, _
                      rexx_      'Quit'z)//'A', _
             expand_ 'yes'c, _
             fill_   'both'z)
 call TkPack(TkEntry('.entry1'z), _
             fill_   'x'z)
 call TkPack(TkButton('.changebtm'c, _
                      text_      'Change caption'c, _
                      underline_ '0'c, _
                      rexx_      'changebtm_click'z), _
             expand_ 'yes'c, _
             fill_   'both'z)

 # Set Entry widgets text:
 call TkInsert('.entry1'c, '1'c, TkCget('.hellobtm'c, '-text'z)z)

 # Display Intro-MessageBox:
 call TkTcl('update'z)                  # there's a bug in Windows Tcl/Tk
 call TkMessageBox(message_ 'Welcome to the great FORTRAN/TK example '// _
                            'program.'//NL//'*Stunning*'c, _
                   title_   'FORTRAN/TK example'c, _
                   icon_    'info'z)
end

The above code might be outdated - it's also part of the FORTRAN/TK subversion repository: example_ratfor.r.

To convert the example to FORTRAN code you would have to issue:

 ratfor77 -o example_ratfor.for example_ratfor.r

Alternatively, you can build the whole program using the makefile in FORTRAN/TK's example directory. Assuming there's a "fortrantk.lib" in the the examples-subdirectory you'd have to issue:

 wmake example_ratfor
Last modified 18 years ago Last modified on Jan 20, 2007, 12:17:06 AM
Note: See TracWiki for help on using the wiki.