📄 m2
字号:
.NHLANGUAGE DESCRIPTION.SHDesign.PPRatforattempts to retain the merits of Fortran(universality, portability, efficiency)while hiding the worst Fortran inadequacies.The language.ulisFortran except for two aspects.First,since control flow is central to any program,regardless of the specific application,the primary task ofRatforis to conceal this part of Fortran from the user,by providing decent control flow structures.These structures are sufficient and comfortablefor structured programming in the narrow sense of programming without.UC GOTO 's.Second, since the preprocessor must examine an entire programto 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 easierand more pleasant to read and write..PPBeyond these two aspects _ control flow and cosmetics _Ratfordoes nothing about the host of other weaknesses of Fortran.Although it would be straightforward to extend itto providecharacter strings,for example,they are not needed by everyone,and of coursethe preprocessor would be harder to implement.Throughout, the design principle which has determinedwhat should be inRatforand what should not hasbeen.ulRatfor.uldoesn't know any Fortran.Any language feature which would require thatRatforreally understand Fortran has been omitted.We will return to this point in the sectionon implementation..PPEven within the confines of control flow and cosmetics,we have attempted to be selectivein what features to provide.The intent has been to provide a small set of the most usefulconstructs,rather than to throw in everything that has ever been thought usefulby someone..PPThe rest of this section contains an informal descriptionof theRatforlanguage.The control flow aspects will bequite familiar to readers used to languages likeAlgol, PL/I, Pascal, etc.,and the cosmetic changes are equally straightforward.We shall concentrate on showing what the language looks like..SHStatement Grouping.PPFortran 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,.P1if (x > 100) { call error("x>100"); err = 1; return }.P2cannot be written directly in Fortran.Insteada programmer is forced to translate this relativelyclear thought into murky Fortran,by stating the negative conditionand branching around the group of statements:.P1 if (x .le. 100) goto 10 call error(5hx>100) err = 1 return10 ....P2When the program doesn't work,or when it must be modified,this must be translated back intoa clearer form before one can be sure what it does..PPRatforeliminates this error-prone and confusing back-and-forth translation;the first form .ulisthe way the computation is written in Ratfor.A group of statements can be treated as a unitby enclosing them in the braces { and }.This is true throughout the language:wherever a single Ratforstatement can be used,there can be several enclosed in braces.(Braces seem clearer and less obtrusive than.UL beginand.UL end or.UL doand.UL end ,and of course .UL doand.UL endalready have Fortran meanings.).PPCosmeticscontribute to the readability of code,and thus to its understandability.The character ``>'' is clearer than.UC ``.GT.'' ,soRatfortranslates it appropriately,along with several other similar shorthands.Although many Fortran compilers permit character strings in quotes(like.UL """x>100""" ),quotes arenot allowed in .UC ANSIFortran,so Ratforconverts it into the right number of.UL H 's:computers count better than people do..PPRatforis a free-form language:statements may appear anywhere on a line,and several may appear on one lineif they are separated by semicolons.The example above could also be written as.P1if (x > 100) { call error("x>100") err = 1 return}.P2In this case, no semicolon is needed at the end of each line becauseRatforassumes there is one statement per lineunless told otherwise..PPOf course,if the statement that follows the.UL ifis a single statement(Ratforor otherwise),no braces are needed:.P1if (y <= 0.0 & z <= 0.0) write(6, 20) y, z.P2No continuation need be indicated because the statement is clearly not finished on the first line.In generalRatforcontinues lines when it seems obvious that they are not yet done.(The continuation convention is discussed in detail later.).PPAlthough 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..sp.SHThe ``else'' Clause.PPRatforprovides an.UL "else"statement to handle the construction``if a condition is true,do thisthing,.ulotherwisedo that thing.''.P1if (a <= b) { sw = 0; write(6, 1) a, b }else { sw = 1; write(6, 1) b, a }.P2This writes out the smaller of.UL aand.UL b ,then the larger, and sets.UL swappropriately..PPThe Fortran equivalent of this code is circuitous indeed:.P1 if (a .gt. b) goto 10 sw = 0 write(6, 1) a, b goto 2010 sw = 1 write(6, 1) b, a20 ....P2This is a mechanical translation;shorter forms exist, as they do for many similar situations.But all translations suffer from the same problem:since they are translations,they are less clear and understandable than codethat is not a translation.To understand the Fortran version,one must scan the entire program to makesure that no other statement branchesto statements 10 or 20before one knows that indeed this is an .UL if-elseconstruction.With theRatforversion,there is no question about how one gets to the parts of the statement.The.UL if-elseis a single unit,which can be read, understood, and ignored if not relevant.The program says what it means..PPAs before, if the statement following an.UL ifor an.UL elseis a single statement, no braces are needed:.P1if (a <= b) sw = 0else sw = 1.P2.PPThe syntax of the.UL ifstatement is.P1if (\fIlegal Fortran condition\fP) \fIRatfor statement\fPelse \fIRatfor statement\fP.P2where the .UL elsepart is optional.The.ullegal Fortran conditionisanything that can legally go into a Fortran Logical.UC IF .Ratfordoes not check this clause,since it does not know enough Fortranto know what is permitted.The.ulRatfor.ulstatementis any Ratfor or Fortran statement, or any collection of themin braces..SHNested if's.PPSince the statement that follows an.UL ifor an.UL elsecan be any Ratfor statement, this leads immediatelyto the possibility of another.UL ifor.UL else .As a useful example, consider this problem:the variable.UL fis to be set to\-1 if.UL xis less than zero,to+1if.UL xis greater than 100,and to 0 otherwise.Then in Ratfor, we write.P1if (x < 0) f = -1else if (x > 100) f = +1else f = 0.P2Here the statement after the first.UL elseis another.UL if-else .Logically it is just a single statement,although it is rather complicated..PPThis code says what it means.Any version written in straight Fortranwill necessarily be indirectbecause Fortran does not let you say what you mean.And as always, clever shortcuts may turn outto be too clever to understand a year from now..PPFollowing an.UL elsewith an.UL ifis one way to write a multi-way branch in Ratfor.In general the structure.P1if (...) - - -else if (...) - - -else if (...) - - - ...else - - -.P2provides a way to specify the choice of exactly one of several alternatives.(Ratfor also provides a.UL switchstatement which does the same jobin certain special cases;in more general situations, we have to make dowith spare parts.)The tests are laid out in sequence, and each oneis followed by the code associated with it.Read down the listof decisions until one is found that is satisfied.The code associated with this condition is executed,and then the entire structure is finished.The trailing.UL elsepart handles the ``default'' case,where none of the other conditions apply.If there is no default action, this final.UL elsepartis omitted:.P1if (x < 0) x = 0else if (x > 100) x = 100.P2.SHif-else ambiguity.PPThere is one thing to notice about complicated structuresinvolving nested.UL if 'sand.UL else 's.Consider.P1if (x > 0) if (y > 0) write(6, 1) x, y else write(6, 2) y.P2There are two.UL if 'sandonly one.UL else .Which.UL ifdoes the.UL elsego with?.PPThis is a genuine ambiguity in Ratfor, as it is in many other programminglanguages.The ambiguity is resolved in Ratfor(as elsewhere) by saying that in such cases the.UL elsegoes with the closest previous.UL else 'ed un-.UL if .Thus in this case, the.UL elsegoes with the inner.UL if ,as we have indicated by the indentation..PPIt is a wise practice to resolve such cases by explicit braces,just to make your intent clear.In the case above, we would write.P1if (x > 0) { if (y > 0) write(6, 1) x, y else write(6, 2) y}.P2which does not change the meaning, but leavesno doubt in the reader's mind.If we want the other association, we.ulmustwrite.P1if (x > 0) { if (y > 0) write(6, 1) x, y}else write(6, 2) y.P2.SHThe ``switch'' Statement.PPThe.UL switchstatementprovides a clean way to express multi-way brancheswhich branch on the value of some integer-valued expression.The syntax is.P1\f3switch (\fIexpression\|\f3) { case \fIexpr1\f3 : \f2statements\f3 case \fIexpr2, expr3\f3 : \f2statements\f3 ... default: \f2statements\f3}.P2.PPEach.UL caseis followed by alist of comma-separated integer expressions.The.ulexpressioninside.UL switchis compared against the case expressions.ulexpr1,.ulexpr2, and so on in turnuntil one matches,at which time the statements following that.UL caseare executed.If no cases match.ulexpression,and there is a.UL default section,the statements with it are done;if there is no.UL default,nothing is done.In all situations,as soon as some block of statements is executed,the entire.UL switchis exited immediately.(Readers familiar with C[4] should beware that thisbehavior is not the same as the C.UL switch .).SHThe ``do'' Statement.PPThe.UL dostatement inRatforis quite similar to the.UC DOstatement in Fortran,except that it uses no statement number.The statement number, after all, serves only to mark the endof the.UC DO ,and this can be done just as easily with braces.Thus.P1 do i = 1, n { x(i) = 0.0 y(i) = 0.0 z(i) = 0.0 }.P2is the same as.P1 do 10 i = 1, n x(i) = 0.0 y(i) = 0.0 z(i) = 0.010 continue.P2The syntax is:.P1do \fIlegal\(hyFortran\(hyDO\(hytext\fP \fIRatfor statement\fP.P2The part that follows the keyword.UL dohas to be something that can legally go into a Fortran.UC DOstatement.Thus if a local version of Fortran allows.UC DOlimits to be expressions(which is not currently permitted in.UC ANSIFortran),they can be used in aRatfor.UL do..PPThe.ulRatfor statementpart will often be enclosed in braces, butas with the.UL if ,a single statement need not have braces around it.This code sets an array to zero:.P1do i = 1, n x(i) = 0.0.P2Slightly more complicated,.P1do i = 1, n do j = 1, n m(i, j) = 0.P2sets the entire array.UL mto zero, and.P1do i = 1, n do j = 1, n if (i < j) m(i, j) = -1 else if (i == j) m(i, j) = 0 else m(i, j) = +1.P2sets the upper triangle of.UL mto \-1, the diagonal to zero, and the lower triangle to +1.(The operator == is ``equals'', that is, ``.EQ.''.)In each case, the statement that follows the.UL dois logically a.ulsinglestatement, even though complicated,and thus needs no braces..sp.SH``break'' and ``next''.PPRatforprovides a statement for leaving a loop early,and one for beginning the next iteration..UL "break"causes an immediate exit from the.UL do ;in effect it is a branch to the statement.ulafterthe.UL do ..UL nextis 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:.P1do i = 1, n { if (x(i) < 0.0) next \fIprocess positive element\fP}.P2.UL breakand.UL nextalso work in the other Ratfor looping constructionsthat we will talk about in the next few sections..PP.UL breakand.UL nextcan be followed by an integer to indicate breaking or iteratingthat level of enclosing loop; thus.P1break 2.P2exits from two levels of enclosing loops,and.UL break\ 1is equivalent to.UL break ..UL next\ 2iterates the second enclosing loop.(Realistically, multi-level.UL break 'sand.UL next 'sarenot likely to be much usedbecause they lead to code that is hard to understandand somewhat risky to change.).sp.SHThe ``while'' Statement.PPOne of the problems with the Fortran.UC DOstatementis that it generally insists upon being done once,regardless of its limits.If a loop begins.P1DO I = 2, 1.P2this will typically be done once with.UL Iset to 2,even though common sense would suggest that perhaps it shouldn't be.Of course aRatfor.UL docan easily be preceded by a test.P1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -