         Edinburgh IMP77 Compiler - Version 8.4

    1  !* Program name : LOOP
    2  
    3  !*   Title      :  XI    The Imp  Iterative Statements
    4  !*   Version    :  II
    5  !*   Date       :  24 August 1985
    6  !*   Description:  Tests the use of the %FOR, %WHILE and %UNTIL cycles.
    7   
    8  %Begin
    9  
   10      %string(255) param    { Used to get input from CLI}
   11  
   12      %Integer  J
   13      %Integer  K
   14      %Integer  L
   15      %Integer  M
   16      %Integer  N
   17      %Integer  P
   18      %Integer  Q
   19  
   20      %Integer  Acc
   21  
   22  
   23  %owninteger failures=0
   24  
   25  %routine pass
   26    printstring("Pass")
   27    New Line
   28  %end
   29  %routine fail
   30    printstring("FAIL")
   31    New Line
   32    failures = failures+1
   33  %end
   34  
   35  
   36     ! Code to find out where the output is to be sent
   37     ! -----------------------------------------------
   38     ! Where the output goes to depends on the parameter after the
   39     ! name used to run the program.
   40     !                       Parameter   |   Result
   41     !                       ------------|-------------
   42     !                     No parameter  |  No output
   43     !                          "s"      |  The screen
   44     !                          "j"      |  "journal"
   45     !                     anything else |  file name entered
   46  
   47     ! NOTE THAT THIS CODE USES STRING COMPARISONS, WHICH MAY NOT HAVE BEEN
   48     ! TESTED YET. IF THE PROGRAM FAILS CHECK IT WAS NOT IN THIS PART.
   49  
   50     param=cliparam
   51     %if param = "" %then select output (2) %c
   52+    %else %if param="s" %or param="S" %then select output(1) %c
   53+    %else %if param="j" %or param="J" %then  %c
   54+       open output(2,"journal") %and select output (2) %c
   55+    %else open output(2,param) %and select output (2)
   56     
   57  
   58  
   59     New Lines(2)
   60     Print String ("Iterative Statements");   New Line
   61     Print String ("====================");   New Lines (3)
   62  
   63  
   64  
   65      Print String ("Test the %FOR Statement");   New Line
   66      Print String ("-----------------------");   New Line
   67  
   68      Acc = 0
   69      %For K = -10, 1, 10 %cycle  
   70          Acc = Acc + 1
   71      %Repeat
   72      %If Acc = 21 %then pass %else fail
   73  
   74  
   75  
   76      Acc = 0
   77      J = 1
   78      %For K = 2, 1, J %cycle
   79          Acc = Acc + 1
   80      %Repeat
   81      %If Acc = 0 %and K = 1 %then pass %else fail
   82   
   83  
   84  
   85      Acc = 0
   86      L   = 1000000
   87      M   = 5
   88      N   = 3
   89      %For K = Acc, L // 10, 10 ^^ M * (N * 3 + 1) %cycle
   90         Acc = Acc + 1
   91      %Repeat
   92      %If Acc = 11 %and K = 1000000 %then pass %else fail
   93  
   94  
   95  
   96      Acc = 0
   97      %For K = -1, 2, 1 %cycle
   98          %For L = 0, 3, 6 %cycle
   99              %For M = 1, 1, 4 %cycle
  100                  %For N = 4, -2, -2 %cycle
  101                      %For P = -1000, 1000, 1000 %cycle
  102                          Acc = Acc + 1
  103                          %For Q = 2, 1, 1 %cycle
  104                               Acc = Acc + 1
  105                          %Repeat
  106                      %Repeat
  107                  %Repeat
  108              %Repeat
  109          %Repeat
  110      %Repeat
  111      %If Acc = 288 %and K = 1  %and L = 6    %andc
  112+         M = 4 %and N = -2 %and P = 1000             %c
  113+                   %then pass %else fail
  114  
  115      
  116  
  117      !*   Test the Simple Loop Forms
  118  
  119      Acc = 0
  120      Acc = Acc + 1 %for K = -2, 1, 2
  121      %If Acc = 5 %and K = 2 %then pass %else fail
  122   
  123  
  124  
  125      Acc = 0
  126      Acc = Acc + 1 %for K = 2, 1, 1
  127      %If Acc = 0 %and K = 1 %then pass %else fail
  128  
  129  
  130  
  131      New Lines (2)
  132      Print String ("Test the %WHILE Statement");   New Line
  133      Print String ("-------------------------");   New Line
  134  
  135  
  136      Acc = 0
  137      K   = 3
  138      %While  K > 3 %cycle
  139         Acc = Acc + 1
  140         K = K - 1
  141      %Repeat
  142      %If Acc = 0 %and K = 3 %then pass %else fail
  143  
  144  
  145      Acc = 0
  146      K   = 10
  147      %While  K > 0 %cycle
  148         Acc = Acc + 1
  149         K = K - 1
  150      %Repeat
  151      %If Acc = 10 %and K = 0 %then pass %else fail
  152  
  153  
  154     
  155      Acc = 0
  156      K   = 5
  157      %While  K > 3  %cycle
  158         L = 6
  159         %While  L > 4  %cycle
  160            M = 7
  161            %While  M > 5  %cycle
  162               N = 8
  163               %While  N > 6  %cycle
  164                  P = 9
  165                  %While  P > 7  %cycle
  166                     Q = 10
  167                     %While  Q > 8  %cycle
  168                        Acc = Acc + 1
  169                        Q = Q - 1
  170                     %Repeat
  171                     P = P - 1
  172                  %Repeat
  173                  N = N - 1
  174               %Repeat
  175               M = M - 1
  176            %Repeat
  177            L = L - 1
  178         %Repeat
  179         K = K - 1
  180      %Repeat
  181              
  182      %If Acc = 64 %then pass %else fail
  183  
  184      
  185  
  186  
  187     !*   Test the Simpler Loop Forms
  188  
  189      
  190      Acc = 0
  191      Acc = Acc + 1 %While Acc < 10
  192      %If Acc = 10 %then pass %else fail
  193  
  194  
  195      Acc = 0
  196      Acc = Acc + 1 %While Acc < 0
  197      %If Acc = 0 %then pass %else fail
  198  
  199  
  200  
  201  
  202      New Lines (2)
  203      Print String ("Test the %UNTIL Statement");   New Line
  204      Print String ("-------------------------");   New Line
  205  
  206      Acc = 0
  207      K   = 0
  208      %Cycle
  209         K = K - 1
  210         Acc = Acc + 1
  211      %Repeat %until K <= 0
  212  
  213      %If Acc = 1 %and K = -1 %then pass %else fail
  214  
  215     
  216      Acc = 0
  217      K   = 10
  218      %Cycle
  219         Acc = Acc + 1
  220         K   = K   - 1
  221      %Repeat %until K = 0
  222    
  223      %If Acc = 10 %and K = 0 %then pass %else fail
  224  
  225      
  226  
  227      !*  Test the simpler version of the loop.
  228  
  229      
  230      Acc = 0
  231      Acc = Acc + 1 %Until Acc = 10
  232  
  233      %If Acc = 10 %then pass %else fail
  234  
  235  
  236       
  237    
  238  
  239      New Lines (2)
  240      Print String ("Test the %EXIT and %CONTINUE Statements");   New Line
  241      Print String ("---------------------------------------");   New Line
  242  
  243  
  244      Acc = 0
  245      K   = 0
  246      %Cycle
  247         K = K + 1            { Just a dummy statement.
  248         %Exit
  249         Acc = Acc + 1
?access
  250      %Repeat
  251  
  252      %If Acc = 0 %then pass %else fail
  253  
  254  
  255  
  256      Acc = 0
  257      %Cycle
  258         %Exit %if Acc = 10
  259         Acc = Acc + 1
  260      %Repeat
  261  
  262      %If Acc = 10 %then pass %else fail
  263  
  264  
  265  
  266      Acc = 0
  267      K   = 0
  268      %Cycle
  269         %Cycle
  270            K = K + 1              { Dummy statement again.
  271            %Exit
  272            Acc = Acc + 1
?access
  273         %Repeat
  274         Acc = Acc + 10
  275         %Exit
  276      %Repeat
  277  
  278      %If Acc = 10 %then pass %else fail
  279  
  280  
  281  
  282      Acc = 0
  283      %For J = 1, 1, 5 %cycle
  284          %Exit %if J = 3
  285          %For K = 1, 1, 3 %cycle
  286              Acc = Acc + 1
  287              %Exit %if K = 2
  288          %Repeat
  289       %Repeat
  290  
  291      %If Acc = 4 %and J = 3 %and K = 2 %then pass %else fail
  292  
  293  
  294  
  295      Acc = 0
  296      %For K = 1, 1, 10 %cycle
  297          Acc = Acc + K
  298          %Continue %if K = 5
  299          Acc = Acc - K
  300      %Repeat
  301  
  302      %If Acc = 5 %then pass %else fail
  303  
  304  
  305  
  306  
  307  select output(1)
  308  write(failures,1);  printstring(" failure(s)");  newline
  309  
  310  %End %of %Program

  198 Statements compiled
