Ratfor attempts to retain the merits of Fortran (universality, portability, efficiency) while hiding the worst Fortran inadequacies. The language is Fortran except for two aspects. First, since control flow is central to any program, regardless of the specific application, the primary task of Ratfor is to conceal this part of Fortran from the user, by providing decent control flow structures. These structures are sufficient and comfortable for structured programming in the narrow sense of programming without GOTO's. Second, since the preprocessor must examine an entire program to translate the control structure, it is possible at the same time to clean up many of the ``cosmetic'' deficiencies of Fortran, and thus provide a language which is easier and more pleasant to read and write.
Beyond these two aspects _ control flow and cosmetics _ Ratfor does nothing about the host of other weaknesses of Fortran. Although it would be straightforward to extend it to provide character strings, for example, they are not needed by everyone, and of course the preprocessor would be harder to implement. Throughout, the design principle which has determined what should be in Ratfor and what should not has been Ratfor doesn't know any Fortran. Any language feature which would require that Ratfor really understand Fortran has been omitted. We will return to this point in the section on implementation.
Even within the confines of control flow and cosmetics, we have attempted to be selective in what features to provide. The intent has been to provide a small set of the most useful constructs, rather than to throw in everything that has ever been thought useful by someone.
The rest of this section contains an informal description of the Ratfor language. The control flow aspects will be quite familiar to readers used to languages like Algol, PL/I, Pascal, etc., and the cosmetic changes are equally straightforward. We shall concentrate on showing what the language looks like.
Fortran provides no way to group statements together, short of making them into a subroutine. The standard construction ``if a condition is true, do this group of things,'' for example,
Ratfor eliminates this error-prone and confusing back-and-forth translation; the first form is the way the computation is written in Ratfor. A group of statements can be treated as a unit by enclosing them in the braces { and }. This is true throughout the language: wherever a single Ratfor statement can be used, there can be several enclosed in braces. (Braces seem clearer and less obtrusive than begin and end or do and end, and of course do and end already have Fortran meanings.)
Cosmetics contribute to the readability of code, and thus to its understandability. The character ``>'' is clearer than ``.GT.'', so Ratfor translates it appropriately, along with several other similar shorthands. Although many Fortran compilers permit character strings in quotes (like "x>100"), quotes are not allowed in ANSI Fortran, so Ratfor converts it into the right number of H's: computers count better than people do.
Ratfor is a free-form language: statements may appear anywhere on a line, and several may appear on one line if they are separated by semicolons. The example above could also be written as
Of course, if the statement that follows the if is a single statement (Ratfor or otherwise), no braces are needed:
Although a free-form language permits wide latitude in formatting styles, it is wise to pick one that is readable, then stick to it. In particular, proper indentation is vital, to make the logical structure of the program obvious to the reader.
Ratfor provides an else statement to handle the construction ``if a condition is true, do this thing, otherwise do that thing.''
The Fortran equivalent of this code is circuitous indeed:
As before, if the statement following an if or an else is a single statement, no braces are needed:
The syntax of the if statement is
Since the statement that follows an if or an else can be any Ratfor statement, this leads immediately to the possibility of another if or else. As a useful example, consider this problem: the variable f is to be set to -1 if x is less than zero, to +1 if x is greater than 100, and to 0 otherwise. Then in Ratfor, we write
This code says what it means. Any version written in straight Fortran will necessarily be indirect because Fortran does not let you say what you mean. And as always, clever shortcuts may turn out to be too clever to understand a year from now.
Following an else with an if is one way to write a multi-way branch in Ratfor. In general the structure
There is one thing to notice about complicated structures involving nested if's and else's. Consider
This is a genuine ambiguity in Ratfor, as it is in many other programming languages. The ambiguity is resolved in Ratfor (as elsewhere) by saying that in such cases the else goes with the closest previous un-else'ed if. Thus in this case, the else goes with the inner if, as we have indicated by the indentation.
It is a wise practice to resolve such cases by explicit braces, just to make your intent clear. In the case above, we would write
The switch statement provides a clean way to express multi-way branches which branch on the value of some integer-valued expression. The syntax is
Each case is followed by a list of comma-separated integer expressions. The expression inside switch is compared against the case expressions expr1, expr2, and so on in turn until one matches, at which time the statements following that case are executed. If no cases match expression, and there is a default section, the statements with it are done; if there is no default, nothing is done. In all situations, as soon as some block of statements is executed, the entire switch is exited immediately. (Readers familiar with C[4] should beware that this behavior is not the same as the C switch.)
The do statement in Ratfor is quite similar to the DO statement in Fortran, except that it uses no statement number. The statement number, after all, serves only to mark the end of the DO, and this can be done just as easily with braces. Thus
The Ratfor statement part will often be enclosed in braces, but as with the if, a single statement need not have braces around it. This code sets an array to zero:
Ratfor provides a statement for leaving a loop early, and one for beginning the next iteration. break causes an immediate exit from the do; in effect it is a branch to the statement after the do. next is a branch to the bottom of the loop, so it causes the next iteration to be done. For example, this code skips over negative values in an array:
break and next can be followed by an integer to indicate breaking or iterating that level of enclosing loop; thus
One of the problems with the Fortran DO statement is that it generally insists upon being done once, regardless of its limits. If a loop begins
A more serious problem with the DO statement is that it encourages that a program be written in terms of an arithmetic progression with small positive steps, even though that may not be the best way to write it. If code has to be contorted to fit the requirements imposed by the Fortran DO, it is that much harder to write and understand.
To overcome these difficulties, Ratfor provides a while statement, which is simply a loop: ``while some condition is true, repeat this group of statements''. It has no preconceptions about why one is looping. For example, this routine to compute sin(x) by the Maclaurin series combines two termination criteria.
Notice that if the routine is entered with term already smaller than e, the loop will be done zero times, that is, no attempt will be made to compute x**3 and thus a potential underflow is avoided. Since the test is made at the top of a while loop instead of the bottom, a special case disappears _ the code works at one of its boundaries. (The test i<100 is the other boundary _ making sure the routine stops after some maximum number of iterations.)
As an aside, a sharp character ``#'' in a line marks the beginning of a comment; the rest of the line is comment. Comments and code can co-exist on the same line _ one can make marginal remarks, which is not possible with Fortran's ``C in column 1'' convention. Blank lines are also permitted anywhere (they are not in Fortran); they should be used to emphasize the natural divisions of a program.
The syntax of the while statement is
The while encourages a style of coding not normally practiced by Fortran programmers. For example, suppose nextch is a function which returns the next input character both as a function value and in its argument. Then a loop to find the first non-blank character is just
The for statement is another Ratfor loop, which attempts to carry the separation of loop-body from reason-for-looping a step further than the while. A for statement allows explicit initialization and increment steps as part of the statement. For example, a DO loop is just
The for and while versions have the advantage that they will be done zero times if n is less than 1; this is not true of the do.
The loop of the sine routine in the previous section can be re-written with a for as
The syntax of the for statement is
The for statement is particularly useful for backward loops, chaining along lists, loops that might be done zero times, and similar things which are hard to express with a DO statement, and obscure to write out with IF's and GOTO's. For example, here is a backwards DO loop to find the last non-blank character on a card:
This code is rather nasty to write with a regular Fortran DO, since the loop must go forward, and we must explicitly set up proper conditions when we fall out of the loop. (Forgetting this is a common error.) Thus:
The increment in a for need not be an arithmetic progression; the following program walks along a list (stored in an integer array ptr) until a zero pointer is found, adding up elements from a parallel array of values:
In spite of the dire warnings, there are times when one really needs a loop that tests at the bottom after one pass through. This service is provided by the repeat-until:
The until part is optional, so a bare repeat is the cleanest way to specify an infinite loop. Of course such a loop must ultimately be broken by some transfer of control such as stop, return, or break, or an implicit stop such as running out of input with a READ statement.
As a matter of observed fact[8], the repeat-until statement is much less used than the other looping constructions; in particular, it is typically outnumbered ten to one by for and while. Be cautious about using it, for loops that test only at the bottom often don't handle null cases well.
break exits immediately from do, while, for, and repeat-until. next goes to the test part of do, while and repeat-until, and to the increment step of a for.
The standard Fortran mechanism for returning a value from a function uses the name of the function as a variable which can be assigned to; the last value stored in it is the function value upon return. For example, here is a routine equal which returns 1 if two arrays are identical, and zero if they differ. The array ends are marked by the special value -1.
In many languages (e.g., PL/I) one instead says
As we said above, the visual appearance of a language has a substantial effect on how easy it is to read and understand programs. Accordingly, Ratfor provides a number of cosmetic facilities which may be used to make programs more readable.
Statements can be placed anywhere on a line; long statements are continued automatically, as are long conditions in if, while, for, and until. Blank lines are ignored. Multiple statements may appear on one line, if they are separated by semicolons. No semicolon is needed at the end of a line, if Ratfor can make some reasonable guess about whether the statement ends there. Lines ending with any of the characters
Any statement that begins with an all-numeric field is assumed to be a Fortran label, and placed in columns 1-5 upon output. Thus
Text enclosed in matching single or double quotes is converted to nH... but is otherwise unaltered (except for formatting _ it may get split across card boundaries during the reformatting process). Within quoted strings, the backslash `\' serves as an escape character: the next character is taken literally. This provides a way to get quotes (and of course the backslash itself) into quoted strings:
Any line that begins with the character `%' is left absolutely unaltered except for stripping off the `%' and moving the line one position to the left. This is useful for inserting control cards, and other things that should not be transmogrified (like an existing Fortran program). Use `%' only for ordinary statements, not for the condition parts of if, while, etc., or the output may come out in an unexpected place.
The following character translations are made, except within single or double quotes or on a line beginning with a `%'.
Any string of alphanumeric characters can be defined as a name; thereafter, whenever that name occurs in the input (delimited by non-alphanumerics) it is replaced by the rest of the definition line. (Comments and trailing white spaces are stripped off). A defined name can be arbitrarily long, and must begin with a letter.
define is typically used to create symbolic parameters:
It is generally a wise practice to use symbolic parameters for most constants, to help make clear the function of what would otherwise be mysterious numbers. As an example, here is the routine equal again, this time with symbolic constants.
The statement
Ratfor catches certain syntax errors, such as missing braces, else clauses without an if, and most errors involving missing parentheses in statements. Beyond that, since Ratfor knows no Fortran, any errors you make will be reported by the Fortran compiler, so you will from time to time have to relate a Fortran diagnostic back to the Ratfor source.
Keywords are reserved _ using if, else, etc., as variable names will typically wreak havoc. Don't leave spaces in keywords. Don't use the Arithmetic IF.
The Fortran nH convention is not recognized anywhere by Ratfor; use quotes instead.