Path: utzoo!utgpu!watserv1!watmath!att!dptg!ulysses!andante!princeton!udel!wuarchive!zaphod.mps.ohio-state.edu!usc!ucsd!ucbvax!cck.coventry.ac.uk!esx038 From: esx038@cck.coventry.ac.uk ("W. J. G. Overington") Newsgroups: comp.sys.transputer Subject: (none) Message-ID: <20843.9007160836@cck.cov.ac.uk> Date: 16 Jul 90 10:36:09 GMT Sender: usenet@ucbvax.BERKELEY.EDU Organization: The Internet Lines: 260 ALCOR NEWS Issue Number 2 August 1987 CONDITIONAL CONSTRUCTS IN ALCOR. ----------- ---------- -- ------ The alcor language has a number of constructs that involve a conditional feature. These conditional constructs are the repeat statement, the while statement, the various formats of the if statement and the two types of for statement. All of these conditional constructs may only be used within a definition. The two types of for statement are respectively for an increasing count and for a decreasing count. If constructs in alcor. -- ---------- -- ------ define J if C then X endif ; define K if C then X1 else X2 endif ; define L if C1 then X1 elseif C2 then X2 endif ; define M if C1 then X1 elseif C2 then X2 else X3 endif ; define N if C1 then X1 elseif C2 then X2 elseif C3 then X3 endif ; In order to facilitate the explanation of the various if constructs of the alcor language, let us first define a few simple words. define C Idup 4 I= ; The word C will duplicate the top number on the integer stack of the current processor, put the number 4 on the integer stack and then perform the I= operation. If the number on the top of the integer stack when the word C was called was, say, 3, then a false will be placed on the boolean stack of the processor. If the original number on the integer stack were a 4, then a true would be placed on the boolean stack, otherwise a false would be placed on the boolean stack. The net effect of C is to leave the integer stack unchanged and to add one item to the boolean stack. define U Idup I* ; The word U will square the number on the integer stack. define V 17 I+ ; The word V will add 17 to the number on the integer stack. Suppose now that we wish to define a word called magic which will look at the top number on the integer stack and, if it is a 4 will square it. We simply use an if construct in alcor. define magic if C then U endif ; We may then type 4 magic and the number 16 will be left on the integer stack. Sheet 1 of 4, issue 2. If we type 7 magic then the 7 will be left on the integer stack, unaltered. Let us look at how this occurs in detail. Consider the case of 4 magic first. The 4 is put on the integer stack. Now the alcor system seeks to obey the word if. As it happens, the word if does not do anything in alcor and could be omitted without any effect whatsoever in the running of the alcor program. This is not advised however as the presence of the word if helps anyone looking at an alcor program to understand what is happening. The alcor system next seeks to obey the word C, and, finding it is a user defined word, proceeds to work its way through the definition of C until it reaches a ; word. The first word in C to be obeyed is the word Idup which duplicates the 4 on the top of the stack. Now the number 4 is added to the integer stack, so that there are now three 4s on the integer stack. The word I= takes the top two numbers from the integer stack and compares them to see if they are equal to each other. In this case they are equal to each other and so a true is placed on the boolean stack. At this stage there is only the original 4 on the integer stack. The ; word returns us to the definition of the word magic. The next alcor word to be obeyed is then. The alcor word then takes one item from the top of the boolean stack. In this case the item is a true and so then does nothing. The next word to be obeyed is U, which is a user defined word and this has the effect of squaring the 4 on the integer stack to give 16. The word endif is just a marker and is ignored. The ; after the endif tells us that we have finished obeying the word magic. Let us now consider the case of 7 magic. The 7 is put on the integer stack. Now the alcor system seeks to obey the word if. As it happens, the word if does not do anything in alcor and could be omitted without any effect whatsoever in the running of the alcor program. This is not advised however as the presence of the word if helps anyone looking at an alcor program to understand what is happening. The alcor system next seeks to obey the word C, and, finding it is a user defined word, proceeds to work its way through the definition of C until it reaches a ; word. The first word in C to be obeyed is the word Idup which duplicates the 7 on the top of the stack. Now the number 4 is added to the integer stack, so that there are now two 7s and a 4 on the integer stack. The word I= takes the top two numbers from the integer stack and compares them to see if they are equal to each other. In this case they are not equal to each other and so a false is placed on the boolean stack. At this stage there is only the original 7 on the integer stack. The ; word returns us to the definition of the word magic. The next alcor word to be obeyed is then. The alcor word then takes one item from the top of the boolean stack. In this case the item is a false and so the alcor system searches through the word magic until it comes across the word endif, though as we will see, the word then, in fact, looks for the next occurrence Sheet 2 of 4, issue 2. of any of the words else, elseif, endif. The next word to be obeyed is the ; after the endif, which tells us that we have finished obeying the word magic. An interesting, and important, point is that the alcor system, in carrying out the search for the next occurrence of else, elseif, endif searches only through the word in which the alcor word then occurs. That is, in this case, the alcor system searches through the word magic, and does not search through the word U to see if there are any occurrences of else, elseif, endif in the word U. This means that structured programming may be used, as U could, if we wished, contain an alcor if construct without that alcor if construct interfering with the operation of the alcor if construct in the word magic. An alcor user defined word could contain more than one alcor if construct, as long as the endif of one alcor if construct precedes the word if of the next alcor if construct. Please know that when the alcor system, in performing a search for any of the words else, elseif, endif, the next word to be obeyed is the word after the found word. This is important to understand, because although the word endif, when encountered to be obeyed, does, in fact, do nothing; other words, when encountered to be obeyed, do, in fact, have their own operational actions. For example, the words elseif and else cause a search to be made for the word endif directly, without any reference to any intervening occurrences of any other words. Repeat loop construct in alcor. ------ ---- --------- -- ------ define hundred Idup 100 I= ; define sum 0 repeat 1 I+ hundred until ; While loop construct in alcor. ----- ---- --------- -- ------ define C Idup 100 I= not ; define X 1 I+ ; define ninetynine 1 while C do X endwhile ; For loop constructs in alcor. --- ---- ---------- -- ------ It is helpful, in describing the syntax of the alcor for loops, to first of all define a few alcor words with user-helpful names. define start 20 ; define finishup 40 ; define stepup 2 ; define stepdown 0 3 I- ; define finishdown 8 ; define X 100 I+ ; It is then possible to show some examples of alcor for loops in a manner that helps convey the underlying syntax. define elevensums start for stepup step X finishup to ; define fivesums start for stepdown step X finishdown downto ; Please know, however, that it is not necessary to use definitions of individual numbers in order to use an alcor for loop, though the for loop itself needs to be within a definition. Sheet 3 of 4, issue 2. define XIsums 20 for 2 step X 40 to ; define Vsums 20 for 0 3 I- step X 8 downto ; In the alcor language, when using a for statement, the user need not concern himself or herself with the way in which the control counter of the loop is stored. The control counter of the loop is stored in a special internal array of the alcor language system, there being a control counter available for each level of nesting of alcor words. It does not matter whether a for loop is used or is not used at any given level of nesting, the control counter location is always available but is only used if a for loop is encountered during operation. In the event that the user merely wishes to use a for loop in order to obey a loop of instructions a certain number of times without any reference to the value of the control counter, then the existence of the control counter may be disregarded. However, should the user, at any level of nesting, use the alcor word control, then a copy of the current value of the for loop control counter of that level of nesting will be placed on top of the integer stack. For example, suppose that we wish to place the integers 4, 6, 8 and 10 on the integer stack and that, as a demonstration of the use of the alcor word control, we choose to use a for loop. We may use define placethem 4 for 2 step control 10 to ; placethem Please notice the way that the alcor word control is after the alcor word step. Here is an example of how one could use three nested for loops. The example also uses a level of nesting without a for loop in it, just to show what can be programmed. define p 20 for 1 step q 100 to ; define q 60 for 0 2 I- step r s 40 downto ; define r t ; define s 3 ; define t 1 for 10 step 101 to ; p Application example. ----------- -------- Here is an example alcor program which computes a factorial. This example demonstrates both the forward referencing and the recursive features of the alcor language. define C Idup 1 I= not ; define factorial if C then 1 Iswap F Idrop endif ; define F Iswap Iover I* Iswap 1 I- if C then F endif ; 7 factorial Alcor news is written by W. J. G. Overington, Robotics Group, Department of Electrical, Electronic and Systems Engineering, Coventry Polytechnic, Priory Street, Coventry CV1 5FB, England. Sheet 4 of 4, issue 2.