Sunteți pe pagina 1din 27
‘script! \ 'tackward slant’ ‘single-character correct CHAPTER 9 SIMPLE INTERACTIVE SCRIPTS AND MSTHODS POR ERROR CORRECTION SCRIPTS AND ERRORS A set of executable TRAC language statements for performing a desirable action is called a ‘script’. As you type a TRAC script on the typewriter, you will inevitably make sone typing errors. This must be expected, If the errors are not corrected, the scripts will not perform correctly. Also erroneous scripts might spoil some of the careful work that has already been put into storage, Since typing errors are to be expected, ve must first learn rethods fot their corrections. ‘wo simple kinds of error correction will be dealt with just now. Ina later section of this chanter, several TRAC language scripts which permit more complex kinds of error correction will be givens SINGLE-CHARACTER SAROR CORRECTION The simplest error situation occurs when you are typing an input string, and you suddenly realize that the previous letter just typed was erroneous, You need an “eraser”, With a TRAC language procescor, this error situation is eastly remedied. The TRAC processor has a character, usually the "backward slant' character \ , for "single-character correction'.* With the Nodel 33 Teletype machine, the backward slant * See Appendix C for the corresponding corrective character for other keyboard devices. Copyright (€) 1972 Rockford Researeh, Tne. K-22 character 1s activated by pressing the shift key together with the letter L, Other equipment will use other characters for correction. ‘The backward slant character causes the processor to delete the innedintely preceding character of the input string of the read string prinitive, Thus if we had desired to type in APFIE" , tut 1f AFPLK had been typed, the erroneous X conld be corrected by typing the tackward slant followed by the intended letter or letters, The apostrophe meta character 1s typed when we are done: APPLY\E', As a reeult, the value string for the read string primitive will te: APPLE. The tackward slant character causes the complete erasure of the preceding character, and the slant character also disappears, However, corrections of a string cannet be made after the apostrophe has been typed. The backward slant character can be used to delete a sequence of erronsous characters, simply by using a sequence of backward slant characters. dach backward slant character deletes another character in the backwards direction, Thus, 4f AQQL fms been typed in, when APPLE is intended, the string ean be corrected by the use of three backward slant characters, followed by typing the correct characters: AQOLN\\ PPLE" . This will produce the final string value AFPL@ for the read string. o erasute effect of the backvard slant character extends back only It wil] not ever the string input for the current read string prinitiv erase anything previous to the last mata character, If too many tackward slant characters are typed, the unheeded ones will disappear and be ignored. SRROR CORABCTION BY RESTARTING INPUT The second sinple error situation occurs when it is desirable to delete the entire string up to the point of typing, and to stari the input m-3 @ ‘oormercial at! "restart character” string over again, For this kind of correction, the ‘commercial at! character @ 45 used.* When the commercial at character 1s placed in the input string, At wipes out all the previous characters of the iaput string, The conmercial at character disappears also, Thus the commercial at character 1s equivalent to a ‘restart character' for restatting the input string. This second error situation will typically occur when you are typing sone long complex expression, and a number of single-character mistakes have been made and corrected, and then you decide that things have becone so confused that you would like to start all over again. The commercial at character makes this easy te do. After use of this character, the read string primitive is still waiting for a string, so the correct string can be typed in. If you stall make additional mistakes, you can use the backward slant character and the commercial et character again and again until you are finally able to type the input string correctly. These two characters can be used in any combination. When all the single character corrections and restarts have been made, we signal to the processor to take the corrected input by use of the meta character user signal, which is usually the apostrophe. For exanple, the following string with multiple corrections might be typed on the page: LIRANNTTLALS H\WASLITTLE JQ\ACK BOTLANER' ‘The string which would be the final value string from the read string primitive after all these correction is: LITTLE JACK HORNER * See Appendix C for the corresponding corrective character for ether keyboard devices, ‘interactive script' IK -4 ‘script Since each of the corrective characters leaves 4 printed indication on the page (instead of being invisible), it is possible to trace through all their actions and to verify that the corrections will produce a result which is exactly as desired. It 18 evident that the read string primitive is incapable of getting either of these corrective characters into the processor. However, if input of these characters is ever desired, there are other methods which will be described in later chapters which can be used te take these characters in without having then disappear in the process, One of these matheds is by means of the “read character" prinitive which we have not yet stuiied, Another method of input is temporarily to "turn eff" the corrective behavior of these characters. Strictly spoaidng, these two corrective characters are not « pert of TRAC language, ‘They are only an aspect of the input interface -- the ‘thing which stands between the keyboard actions and the processor. After these characters have besn gotten into the workspace, by whatever means, they Peheve just like any other alphabetical characters, SXAMPLES OF SIMPLE INTERACTIVE SCRIPTS A TRAC Language script is a sequence of executable statenents for earrying out sone action, An ‘interactive script’ is a kind of a script which interacts with the person at the keytoard, ty carrying out » conversation with questions and responses, Usually a script of this kind 1s given a nane so that it can be stored and then later called into action by use of the call CL. In many ways, a 'seript! in TRAC language corresponds to what is usually called a "progran” in ordinary computer m-5 ‘technology. However, there are great many important differences between « TRAC language script and an ordinary computer ptogran. for this reason the terminology script is used to enphasize the difference. Many interesting interactive scripts can be written ty use of only the four TRAC prinitives IS, GL, PS and RS. we shall now see a few of ‘them, As we go along, it will be helpful for the reader to try out the scripts on his reactive typewriter as he reads this Manual, He should also try to diagram the action, step ty step, in his mind to sea how the ecripts work. The first soript is: Line Number Saript, ce +(05,FROG.A,( ze (PS, (WHAT IS YOUR NAMB? 3 » by #(DS, Kav #(R5)) Se #(FS,( be GOODELE)) Te hy 8. ‘The line nunbers in the left hand colunn are for reference purposes only, They will be used in our discussion to identify particular lines, These munbers should not be typed in at the reactive typewriter. The the lines {1 through 7 should be typed in just as they are shown. The left-hand margin of the script corresponds to the left-hand margin of the reactive typewriter. In otder to make the script easy to read, K-6 the statements are spread out generously over many lines. I suggest you will want te follow this practice of using many lines when you begin to write scripts. When this script 1s typed in, the apostrophe meta character terninates the read in, and the define string expression beginning at line t is executed, The protected third argument string, which extends fron line 1 te line 7, 48 stored in a form having the name string PROG.A . The script named PROG.A does not go into execution at this tine, It must be called in order to make it execute, ‘The asterisk * just below line 8 isa special mark to indicate where the reactive typewriter printing elenent cones to a step after the typing in and execution of the lines 1 through 7 of the script, The asterisk is not typed in, nor does 1t actually appear on the mage. It 4s used here simply for explanatory purposes (like the line numbers} to show vhere the printing element has stopped. The next character to be typed will always be located just over where the asterisk is, a to From few on, in our explanations, the asterisk my be indicate where the printing elenent pauses, while waiting for the next typing at the keyboard. After the define string is executed, a CRLF is '» together with a reloading of a new idling produced by the proces: program. The CRLF causes the printing elenent to nove from the location of the apostrophe to the new location shown at line 8. The part of the script which 1s actually recorded in nemory is the text of the define string. ‘This protected text extends from line 1 te line 7, and includes all the GR and IF characters. The actual protected text ist me? Qe CRLF# (PS ,(WHAT IS YOUR NAME? Be ? a # (DS, NAME, ## (RS) Se a(ES,( 6. coonEYe) )ERLF This text (which is all executable) ie stored with the name string PROG.A , To make this stored script go into execution, it must be called by #(GL,PROG.A}" If we call PROG.A , to cause its execution, and follow the directions it prints, this is what will happens Be #(CL,PROG.A) "WHAT IS YOUR NAME? o . ‘The parts which are doubly underlined are printed by the processor. The printing element waits at line 9, We now respond, and answer the questions o CALVIN’ What is typed in. 10. SCODEYE what is printed by the processor. Me . Printing element stops here. At this point, the script mamed PROG.A has completed 1ts action, and the processor 1s waiting for sosathing more to be typed in, Notice that every tine something 1s typed in to the processor, the meta character apostrophe nust ba provided to terminate the imput string. At the very end, after the GOODBYE fron the processor, the idling progran is reloaded, and the typewriter signal, which is the CRLF, is emitted to show that the processor is ready for more input. The occurrence of this signal is indleated by the new location of the printing elenent above the asterisk at line 11, Ix - 8 You should study through the lines 2 to 6 of the recorded text, determining how each of the primitives executes, Try to discover how each Line, or group of lines, of the script causes the action which is shown in the lines 8 through 11, For exanple, study how the print string in lines 2 and 3 causes the printing of WHAT IS TOUR NAMET . It also causes movement of the printing element to a new line after the question nark, Why? What does line 4 do? Is the form NAME ever called in this script? Could you call it after this script finishes its execution? What do lines 5 and 6 do? Why 4s there no need to have the typewriter signal characters at the end of the string GOODBYE ? Byerything in the script which does not disappear in the scanning (J4ke sone of the CR and LF characters) produces sone action. For example, at line 2 after the question mark, there 1s aa CHLF . Those forrat characters are observed in the output only by their result. They move ‘the printing element to the left margin of a new line, The same characters occur after the two end parentheses of line 3, However, in the scanning of the script during its execution, these format characters disappear since they are no longer protected. At line 4, text CALVIN is typed in for the RS primitive, and it 1s then stored with the name string NAME in memory, At line 5, the two protected format characters reposition ‘the point of printing to a naw line, and then the text GOGDBYE 4s printed out ty the FS primitive. At this point, the action of the script PROG.A is completed, and the workspace is down to the empty print string primitive of the ‘dling program. This printe out a mull string (nothing), the processor then omits the typewriter signal CALF, reloads 2 new idling program, scans it, and then waits for nore m-9 input at the RS of the new 4@ling program, This wait is indicated by the asterick at Line 11. ‘The text stored in the form with name MAME was not called in the PROG.A sertpt, We can now find out what this text is, by use of the eall #(CL.NAME)" . If we try it, we gets 4, #(GL NAME) "GALVIN 12. 2 ANOTHER INTERACTIVE SCRIPT A slightly more elaborate interactive script is the following: ae #(08,PROG.B,( 2. #( PSC de WHAT IS YOUR WAMET 4 » be #(DS,N,##(RS)) 6. #(PS,( Ve HELLO )#(CL,B)) a #(DD,N) oe » 10, Tho character string which 1s actually stored by the outer define string can be written linearly as a string of cheracters arranged horizontally, hat is stored extends from the protective parenthesis in line i to the matehing protective parenthesis in line 9. This 4s what is stored: CRLP# (ps, (CRLFAHAT Is YOUR WaMe?GRLF) )CRDFe(DS.N, (RS) )CALFe (PS, (CRLF HELLO )#(CL, 4) }ORLF#( DD,N)GRLF Ix = 10 Notice that each GHLF pair in the script has teen written out in full. ‘The linear string was so long that it had to go on to the second line. This linear form is hard to read and hard to work with, For this reason, we will use the spread-out format, with the script extending over a number of lines. When the script FROG.B fs called, we get the following action, Bach pause of the printing element is shown by the asterisk: 10. #(CL,PROG,B)* the WHAT IS YOUR NAME? 12. CALVIN" . 136 HELLO CALVIN the In explanation, the string CALVIN 4s stored with name N at line 5. This string 1s recalled and combined «ith the string HELIO to form the second argument of the print string at lines 6 and 7, Note that there is & space character after HELLO, The two strings, when combined are Printed out at line 13 on the page, The form with the name string N is finally deleted at line 6. If you want to try this script again, do #(CL,PROG.B}' , and you can type ina different nane, and you will get a different action at the final line of the response. The form PROC.B 4s not destroyed when it is called, Only a copy 46 moved fron storage into the workspace. Thus the script of the form with the nane FROG.E stays in the memory until it ds deleted by a delete definition primitive DD , or until sone different script te stored with the sane name string FROG.B , ‘There are usually a number of different ways to write a TRAC language K-42 Seript to produce the sane effect. For example, the script for PROG.B could te written eo that all the primitives are inside the single print string prinitive beginning at line 6: te #(08 ,PRGO.C, ( 2 #(F5 #(PS,( 3 WHAT TS YOUR NAME? be ) W(DS #4 (RS) } ( Se HELLO )#(CL,N)#(DD,N)) be aM ‘This ecript has exactly the same effect as FROG.2 as seen at the typewriter, However, it fs mere difficult to understand, unless you are arachine, or unless you carefully diagran it step by step. The steps in the execution of this script, when 1t is called, go like this: First the second FS at Line 2 is executed, typing out WHAT 15 YOUR NAME? with format characters at the beginning and end of the text, Thie PS leaves nothing tehind, Next, the define stting in line & takes over, and causes the read in ty the RS, The read-in text is stored with the name string N, The DS primitive leaves nothing behind, At this point, the second argunent of the first PS in line 2 begins to form in the neutral string, The string is bullt up: first the protected fornat characters at the end of line 4, then HELLO with a following space character, and finally the value string from the call #(CL,N) . After the call has been executed, there is no further need for the form with the name string N, so it 4s deleted with the DD primitive, The 0D leaves nothing behind in the workspace, At this point, the string for output is fully formed in the sacond argument of the first PS of line 2. Printing ecours from this PS, and the action of the script is conplete. mH - 12 A SCRIPT TO CONTROL ENPUT ERNORS By this tins, if you have tried out these soripts on the reactive typewriter, you are probably thoroughly annoyed with the problems of typing dn an entire script without error, ‘Typing and retyping in an attenpt to finally get everything correct ina script is no fun, Even use of the single letter correction \ , and use of the restart character @ , does not solve the problem, Fortunately, the TRAC processor and the reactive typexriter can ba put to work dn two additional ways to remedy mistakes made at the keyboard. In the first way, it 1s possible to record an entire serint without gausing define string execution, Then the recorded script can te called out and carefully checked over before any further action 1s allowed to take place. In the second way, {% is possible to write a sorirt for making corrections in a recorded soript. With it, deletions and insertions can be mde where errors are discovered, These two nethods can be used together. Recording the string before execution is desiratie because othervine execution, such as of the define string step, begins to take place the nonent the apostrophe ie typed in. This may cause trouble, since 1f only a single letter, or conn, or parenthesis, 1s wrong, the script say be useless, What 1s worse, At is possible for peculiar and undesirable ‘things to happen during execution due to such mintakes, Execution of the typed-in script can be prevented by first reading in the entire script ty means of the double sharp read string, and then ty storing this seript under a tenporary nane, such as TEMP . Because ‘of the double sharp read in, nothing in the seript can go into execution, mx - 13 and there will be ne unexpected actions, no matter what mistakes were nade during type in, This 4s how we do its te # (08 , TEMP HAC RS))* After the apostrophe was typed, the processor first tries to execute the ##(HS) and waits on line 1 for more type in. We can now type our script. 1. #(DS Tach, A(RS) ) '#(05,NAMS OF SCHIPT, 2. (TEXT OF SCRIPT))* Be ‘The sequence of characters between the tuo apostrephes is now stored as = form with name TSXP. After the second apostrophe, a new idling program reloads. we can now verify what is stoted in the menory by means of a double sharp call to the form TEMP. This is what happens: 3. #4(CL, TEMP) "#(D5 NAME OF _SCHIPT, 4 (TEKT_OF SCAIPT)) The text after the call in line 3 should be exactly the way we want it dn the final seript. If 4t is not, the expression in line 1 can be set up again, and we can try again to type in the script correctly -~ using of course the single-character and restart correction techniques as necessary. After careful checking, Sf it 1s found that the script has been correctly recorded in TEP, we ean go en and cause the script to be set up for operation. we do this ty a single sharp call to 1 3e #(CL,TaHP)* te = 14 stored: TAXT OF SCRIPT name: al OF SCRIPT ‘The call #(SL,TaF)" ‘brought the string from the form with nane TsAP into the workspace, where it was scanned and executed. This caused the execution of the DS of line 1, with the sterago of tho working seript. Finally, when ve want te use the script, as we used PROG.B , we call the soript with a single sharp call #(CL,NAMS OF ScuIPT)" . With such a call, the seript will go into action. At this tine, the forn TaP can be deleted with OD. This method for the control of input errors, by the use of the exprossion with Ta&P , is an illustration of the power of THAC language to separate an activity into several stages. Then each stage can be dealt with carefully, and everything does not happen at once, In the firet stage, we want te gat the typestn done correctly, without any execution whatsoever. we do this with the TEMP expression which uses sured a double sharp read string. In the second stage, after we have ourselves that the input was correct, we then create the form with the desired acript. We do thic with #(CL,TEMP)' . ‘This sets up the form so that it can be called and used, The form can now be used over and over again, merely by calling 1t each time, We call it with #(CL,YAME OF SCRIPT)" or with whatever name the form with the script has, FIXING MISTARES == CHE "FIXER" SCRIPT If a seript is of any length at all, there is a high probability hat it will still contain sone mistakes, If the seript 1s long, it is a nuisance to have to retyne the whole script just in order to fix one K-15 or two little errors, Mistakes can be repaired without retyping the Whole text ty means of the handy interactive script called FIM . The script FIXER illustrates « capability of TRAC language that we have not seen before, It 16 the capability of one soript te act upon another script, and to change it. Hy extensions of this principle, with certain seripts acting upon, controlling, or modifying other seripte, many of the unusual and useful abilities of TRAC language are realized. The FIKER seript makes use of & new TRAC lenguage prinitive with mnemonic SS. This prinitive will not be expleined until Chapter i1, However, we should take this prinitive on faith just now, and make use of 1t without worrying about it. The FILER script 4s exceedingly useful. When I am working with seripts at the reactive typewriter, one of the first things I almost always. do is to set up a FIXER seript to help me correct the mistakes that I expect to ccour in my typing. This is the FLUR script: Le #(D5, FIXER ,( 2. #(PS,( 3s DELETS+))#(SS,,NAME,##(RS)) a a(PS,( Se INSERT-))# (DS, WAM, A#(CL, NAME, HACRS)) ) 6. Py a $(33, FIXER NAME)! Type in FIXGR with the help of TEMP. If you are able to get FIXEX typed in correctly, then you have it madel You will be able to make 211 sorts of changes and corrections in the texts of forms. Tk = 16 Now let us see how FIXst is used. We will use it to make change in tho stored text of the form with name NAM OF SCRIPT . Let us change the string TsXT to MESSAGE. The steps that we take to do this are shown below, just as they would be recorded on the page of the reactive typewriter: 1. #(CL,PINER NAME OF SCRIPT)" 2. zee: * In the eall to FIXER, the nane of the form to be acted upon is put into the third arzunent of the call, After the apostrophe, the reactive typewriter responds ty typing out JDEIETE= . By doing so, 1t asks for the string we wish to delete. The printing elenent waits at the location above the asterisk, Now we should type in the string that we want to renove, or to have replaced by something else. We type TEXT! + 2. DSLETE-TEXT* ce INSERT- Tt 1s now waiting for the string to be inserted, We type MESSAGE" + 3 INSERT-wessaga! 4 ‘The whole sequence, without my comments or underlining, leoks like this: #(CL,PIZEA NAME OF SCRIPT)! ‘DSLSRS- TERT" TNSERT-HESS G2" The fact that the printing elenent stops on the following line indicates mx -17 that the action of FIUER has been completed, and that the processor is ready for something nore. Wo can verify the correctness of the modification that has been nade by typing 4 double sharp call to the form. We gots #4(CL,WAME OF SCRIPT)*HESSAGE OF SCRIPT If there is still any error, we can use FIXER again, Each time it is used, it works on the last version of the text 4n the form, At any tine, the text can be displayed by the double sharp call. Since FL “writes over" or redefines the old form, the earlier versions are all dest~syed. There is one precaution in the use of FIXER . It comes from the ch and every fact that FILER makes its deletions by searching for occurrence of the string typed in after the DELETS-. In the stored text of the form, there may be several occurrences of the string in the text of the form, Thus, if you are net careful, you may be surprised with the result, For example, the original form may have been set up by (05,41, THIS IS AN APPLE)" . Suppose wa wish to change the IS to WAS. We use FICER 1 #(CL, FIXER AL)? DELETE-Is* INSHRT-Was' P#CGL,AL)"THWAS WAS AN APPLE Clearly, this result was not intended! However FIXER dia exactly what Lt was told to do, It replaced all the occurrences of IS xith WAS. It can be sald that reactive typewriters do not make mistakes, but do! ‘that peop! ‘context characters? = 18 "branching! Woat we should have done in the use of FIXER was to indicate sone additional characters in the delete string, se thet the location of the deletion would be uniquely defined. Such additional characters are called ‘eontext characters’, Here we can use the space character as the context character, and wo can use it immediately preceding and following the string to be changed, which is 15. Note that we have te replace the context characters in the fellowing insertion step, To show how this goes, we start over, and do it right: #(05,AL,T805 1S AN APPLE)" (CL, Fg, AL)" ‘Osteté- Is ' INSERT- WAS ' p#(CL,AL)'THIS WAS AN APPLE How that you have these two techniques -- the use of TEMP to receive typed-in seripts, and FIXEX to make corrections in text == you whI1 find it much easter to work your reactive typewiter and to experiment with the examples in this Manuel. ‘TH2 AZACTIV2 TEPEWAITER MAKES A DECISION ‘The "magic brain" reputation of computers cane about because computers can be progranned to take one course of action instead of another, depending upon the circumstances. Taking one course of action, or another, in this fashion is called ‘branching’. The expression ‘branching! comes fron the ides of « tree; in a path up a tree, you often have to choose to Branching is easy to do in TRAC language, go along one branch or anothe Woon branching occurs in the operation of any machine, it gives the appoarance m- 19 of rudimentary thinking. Howsver, 4t 4s really not "thinking", Tho processor morely compares one string with another, and then follows its rules of operation which cause it to take one branch or another. Tt 4s possible to produce an interactive seript which makes a decision to branch in one direction or another using enly the four prinktives 0S, CL, #5 and PS. Making decisions with only these four prinitives is not the easiest way to do it, but it does make an interesting stunt, and at helps te illustrate some of the things that THAC language can do. In the later chapters, we will study the two TAAC primitives which ate especially suited to the making of decisions. ‘The decision-making script is: Le (bs ,DacIDs,( 2 #(P5,.( 3. TEP A STRING te Y)A(DS.,4 HE(RS)) Se #FS.( és TYPE ANOTHER STRING Pe ) MDS .2 CRS )) 3. $(05,A#(CL,1), (THEY ARE DIFFERENT) Qe HCDS HA(CL,2), (THEY ARB THE SAMs) 10. #(F8,( Ade DACCL AACEL A) 12. ye After you have typed this in, carefully re-check the parentheses of each line, it 4s best to use the TsMP rethod of input, If it is all right, set up the operative script with #(CL, TEMP)". mm - 20 This 1s what will happen when the script is executed. As always, ‘the processor output is shown underlined, and the pauses are shown ‘oy the asterisks: #(cL.pecrDe)* The secret of this brainy action lies in the trick contained dn lines 6 and 9 of the script. It will take @ little bit of stuly before you are Likely to catch of to just how this works. I will try my best to explain ite At the beginning, the first string typed in ASC is stored with name string 1 by the DS at line 4, The second string ABD is stored with nano string 2 by the DS at line 7, at line 8, ##(CL,1) is used to recall the first name string, and this string is used as the nme of the form set up at line 8, In other words, ASC is now the nane of the form with the text THEY ARS DIFFERENT . At line 9, the second string, which is ASD, is recalled, and this string 4s used as the name of a form with text THEY ARE THE Saks , The branching action now follows, It takes place in the printout at lines 10 and 11. The call #(CL,1) produces ABC, and this string is used as the nane string in the enclosing call, and forms the expression X= at ‘recursion’ ‘panic stop" #({CL,ABC) » The call in turn produces the string THEY ARE DIFFERENT which 4s printed out on the page. However, if the second string had been the same as the first string, that is, if both strings were ABC, then at line 9 the define string primitive would have created a form with text THEY ARE THE SAME with name string ABC, In the process, 1t would have destroyed the form ereated at line 8, since two forms cannot have the same name, The string ABC is now the name string for the form with text THEY ARE THE SAME created at Line 9, Finally, the output expression at lines 10 and iL causes the typeout of THEY ARE THE SAME , ‘The key to the branching process of this seript is the fact that ‘to forms cannct have the game name, and the last form which is defined with a name 1s the one which remains in the memory, TO DO IT AGAIN AND AGAIN == *REOURSION' Many times we desire te perform some action over and over again, For example, if we wanted te use the previous seript DECIDE as a gane, At would be a great nuisance to have to type in #(CL,DECINE)' each and every time ve wanted to run it again. Fortunately in TRAC Language 4t is very easy to get an action te repeat itself over and over again. However it doss take some care and thought so that the repetitive action 4s controlled, and can be made to stop "gracefully". Remember the Sourcerer's Apprentice and his salt mill. Take precautions! A seript which 41lustrates a nonstop action is the following. However, don't try it unless you know how te perforn the 'panie stop’ described in Chapter 2 and in Appendix Cy IX = 22 The script is: Le #08 ,GOGOGO.< 2 #73. Be #(CL,900090) a7 PD If you call G0G0GO inte execution, your page will look like this: #0 CL..G0G0G0 ) ;GouOQcOCOOO0COROCOWOCCOROCUCCOCOOCOOCO ‘The typing will not stop by itself. It will help to dlagran just what happens when GOG0G0 4s called. when the script is typed in, the protected string composed of lines 2 and 3 is stored. ‘GREFe (ps ,x)CREFF(CL,cosodo ORL goaeds sto: nam ‘The steps in the execution following #(CL,GOGOGO) are diagrammedr a, =f (s L/CRGE+ Ps x) CRLF # (CL, Go00Ge) CRLF) /= af! ee [FRIF#( cL, 900060) CRLF )}/= printed out: X He, ? =/ 'ps'/_ cL Goose /CRLF) /= At this ppint, « new copy of the string from form GQG0G0 is inserted into the verkspace: aC, =f {a (EGRLF¢( ps , x) GRDF* (cL, coceco) CREF / CREF) /= a =/ ps {/GALF+(Ps .x)GRLF#(CL,a0G0G0 )RLFCREF) /= *ddngnostic" m= 23 “Scar ‘string capacity alert" ‘etring capacity exceeded! ‘This last line is the same as the first line in the diagram, with the ‘exception of an extra CRLF at the very end. The process repeats itself, with another X being printed out, and so on, Kowever, at each stage, another CRLF 4s added at the ent, Although these format characters disappear fron the middle of the script, because they are scanned, they do not dieappear at the end of the script, This will eventually cause trouble because the workspace has only a finite size, The first thing that will happen, as the workspace is nearly filled up, is that the typewriter will print out a warning ‘ataenostic! , This neans ‘string capacity alert’, ani it warne you that the processor has room for only iff more characters in the workspaces At this time, 1f you don"t take a corrective action, such as the panic stop, the processor will continue without further warning until there ie ne mere room left. Then, because the processor cannot continue, it takes a drastic emergency step. It clears the workspace to =////= , and 1t prints out the diagnostic , meaning ‘string capacity exceaded’. ‘The purpose of the diagnostic, and the eleating of the workspace, is necessary because otherwise the filled-up processor would te “stuck” with ne possibility of doing anything, You would be left sitting before your silent typewriter wondering what had happened, IX = 2h Several lessons in writing TRAC language soripts can be learned from GOGOCO, The first lessen is that for any script which ts to te performed a great nunber of times, 1% 1s desirable to make sure that characters do not pile up at the end of the workspace, In the GOOOCO example, thts could have been prevented by combining the lines 3 and & inter #(CL,GOCOCG)}) . In this form, all the CR and TF characters are scanned, and we have solved the proble Gther seripts may be worse in this respect, They my have lengthy expressions following the call prinitive that causes the repeat action, Such expressions will pile up. When this is the case, the scripts should be carefully modified so that there is no undesired text Llowing the call causing the repeat. Several technical desc¥iptive tetas are used with seripts Like GOGOGO, When a script of this sort 1s called into execution, and it repeats without stop, 1t is said that 1t "goes into a "loop". By this it is nant that when the specified action has been accomplished, the script "loops" back and starts all over again, repeating without end, A script ike GOGOGO 4¢ also called a recursive soript. By this is neant that the definition of the script GOG0GO makes use of a call to GOG0GO. Another xay of stating the situation 1e that the action defined by GOGOCO has within its definition e call to the actual thing (the script of GOG0GO) which 4 being defined, It "Mtes 1ts tail". When technical people use the descriptive tern 'recursive', they ate really using & fancy word for what 1s ordinarily described by the phrase “Lifting yourself ‘by your own tootstrapa". However, unlike the bootstraps situation, recursion in TRAC language works -- and conetines ali too well, as in cocogor, mK = 25 "Loop" CONTROLLING A LooF A seript which is able to call iteelf is said te contain 4 ‘loop’. Such a loop must be carefully controlled, or we are spt to get behavior like the script GcG0d0 . There are many ways in which the actien of a leop may be controlled, Ordinarily, TRAC language primitives other than the limited set 0S, CL, PS and RS are used in such control. However, contrel can be secured with only these primitives. A simple script will Allustrate how this can be done: le (55,7, ( 2. 2(PS,¢ 3. x) 4 #(CL,eH(RS)) Se » When called with #(CL,Y)' , this seript goes into action. It oes to 8 new line and prints an X. It then waite for input without Lf just the one letter ¥! 4s typed in, the going to the next lin call at Line 4 is performed, bringing a new copy of this script into the vorkspace and repeating the action. This continues, so long as "is typed in at each pause, If anything else is typed, the call at line 4 fails to retrieve a copy of this seript, and the action stops. in this manner, control of the loop is obtained. ‘on the page, the action looks like this: #(cL,Y)! am om Xaaat At this point, the action stops. TX = 26 A much more interesting behavior is provided when ene seript is used to control the action of another. Consider the contre) of the script FROG.B. In order to initiate the action of this seript, it is necessary to stve the command 4#(CL,PROG,3)" at the keyboard each tine the action 1s Jesired. Howaver, instead of calling the script from the keyboard, it 4s possible te eall the seript with anther script. For example, the seript named Y could be medified so that the print string primitive at line 2 is replaced by a call to PAOG.2. However, to make the new control seript more pleasant te use, we can have it ask us D0 YoU #aNT TO TAY AGAIKT . If we mane the new control seript YES , then we csn fet it up eo that our anower ‘YES! acts te continue the action of the loop. Such @ control seript is: 1 #(0S,Y8s 0 a #(CL,PROS.B) 3. HPS, ( he DO YOU WANT TO TRY AGAIN? = )) Se (cL, ##(RS)) & ye Since we de not expect this soript to be repeated hundreds of tines, the line 6 was not combined with Line 5, The accumilation of « few dozen format characters will not cause ary trouble. This seript 1s put inte action with the call #(CL,Xa5)'. The action specified by seript YES is described in lines 2 through 5. The first action is the call to FROG.B , and we assume that the seript for PROG.B is already in the memory, At the end ef execution of PROG.B the lines 3 and 4 4ndteate a TH and IF format action. The processer then im = 27 types out Do YoU WaT To TRY I? AGAIN? , If your answer is Yss' , the call at line 5 becomes the expression #(CL,¥sS) . This in turn reloads 8 new copy of the text named YES inte the workspace, and execution begins again. Again PHOG.E is executed, The action goes on and on, so long as the answer continues to be Yes! . if any other answer is given, the repeat action stops, and the idling program is reloaded. SIGRCISES TO TRY AT YOUR QSACTIVE TYPUNATTER 4, Try each of the scripts in this chapter, Sxperiment by making your oun variations on them, 2, write « seript named ANALYST which takes your answer at each stage and repeats dt back to you in a question. here your ansver is indiested by "answer", have the seript come back each tine with: WHEN YOU SAID "answer" DID YOU REALLY MEAN "answer"? 3. Can you write « script which uses your two preceding answers with suitable "analytical" filler? For example, if the ast answer was FATHER, and the one before that was MOTHER, such 4 seript might give the respons WABN YOU SAID "FATHER" DID YOU REALLY MEAN "MOTHER"? (Hint: You can keep the two strings available by storing the string Just coming in under the nane CURRENT, However, before u input oceurs, the old CURRENT string is defined into another form naned PAST. Then in the output, calls to these two forms will provide the strings.)

S-ar putea să vă placă și