         Edinburgh IMP77 Compiler - Version 8.4

    1  %const %string (1) SNL ="
    2+ "
    3  %begin
    4     %const %integer TRUE = 1, FALSE = 0
    5     %constant %integer  A stream = 1, B stream = 2
    6     %ownstring (255) A file, B file, pr, left, right
    7     %ownstring (255) %array File('A':'B')
    8     %owninteger %array Base('A':'B')
    9     %owninteger A count, B count
   10     %owninteger A num, B num
   11  
   12     %const %integer Max Lines = 512, Max Line Width = 128
   13     %byte %integer %array AA, BB (1:Max Lines*Max Line Width)
   14  
   15     %integer%fn  input ended
   16        %integer ch
   17        %on %event 9 %start
   18           %result = TRUE
   19        %finish
   20        ch = next symbol
   21        %result = FALSE
   22     %end
   23  
   24     %routine  Rd line (%string (*) %name s)
   25        %integer ch
   26        s = ""
   27        %cycle
   28           read symbol (ch)
   29           %exit %if ch = nl %or LENGTH(s) = Max Line Width
   30           s = s.tostring(ch)
   31        %repeat
   32     %end
   33  
   34     %byte %integer %map  A (%integer i, j)
   35        %result == AA (((Base('A') + i)&(Max Lines-1))*Max Line Width + j)
   36     %end
   37  
   38     %byte %integer %map  B (%integer i, j)
   39        %result == BB (((Base('B') + i)&(Max Lines-1))*Max Line Width + j)
   40     %end
   41  
   42     %routine  refill (%byte %integer %map x (%integer i, j),
   43+                      %integer n,
   44+                      %integer %name count)
   45  
   46        %integer i, j, ch
   47        %for i = Max Lines+1-n, 1, Max Lines %cycle
   48           %cycle
   49              %return %if input ended=TRUE
   50              Read symbol (ch)
   51           %repeat %until ch # nl
   52           %for j = 1, 1, Max Line Width-1 %cycle
   53              %exit %if ch = nl %OR CH<32
   54              x (i, j) = ch
   55              %return %if input ended=TRUE
   56              Read symbol (ch)
   57           %repeat
   58           x (i, j) = nl
   59           count = count + 1
   60        %repeat
   61     %end
   62  
   63     %routine  print (%byte %integer %map x (%integer i, j),
   64+                     %integer n,
   65+                              flag)
   66  
   67        %integer i, j
   68        %for i = 1, 1, n %cycle
   69           Print string ("""".File(flag).""",")
   70           Write (Base(flag)+i+1, 5)
   71           Print string (": ")
   72           j = 0
   73           %cycle
   74             j = j + 1
   75             %exit %if x (i, j) = nl
   76             Print symbol (x (i, j))
   77             %if j=57 %then newline %and spaces(20) %and printsymbol(':')
   78           %repeat
   79           newline
   80        %repeat
   81     %end
   82  
   83     %integer%fn  lines match (%integer A line, B line)
   84  
   85        %integer j
   86        %if A line > A count %or B line > B count %start
   87           %result = TRUE %if A line > A count %and B line > B count
   88           %result = FALSE
   89        %finish
   90        %for j = 1, 1, Max Line Width %cycle
   91           %result = FALSE %if A (A line, j) # B (B line, j)
   92           %result = TRUE %if A (A line, j) = nl
   93        %repeat
   94        %result = TRUE
   95     %end
   96  
   97     %routine  find match (%integer %name AL, BL)
   98  
   99        %integer i, j
  100        %for i = 1, 1, Max Lines %cycle
  101           AL = i %and BL = i %and %return %if lines match (i, i)=TRUE
  102           %if i > 1 %start
  103              %for j = 1, 1, I - 1 %cycle
  104                 AL = i %and BL = j %and %return %if lines match (i, j)=TRUE
  105                 AL = j %and BL = i %and %return %if lines match (j, i)=TRUE
  106              %repeat
  107           %finish
  108        %repeat
  109        AL = Max Lines+1; BL = Max Lines+1
  110     %end
  111  
  112  
  113  %integer fname len; 
  114  
  115  A num = Max Lines; B num = Max Lines
  116  Base('A') = 0; Base('B') = 0
  117  A count = 0; B count = 0
  118  
  119  %if cli param -> ("-h") %start
  120    print string("Syntax: compare oldfile newfile".snl)
  121    %stop
  122  %finish
  123  
  124  pr = "New file: "
  125  
  126  %unless cli param -> A file.(" ").B file %start
  127    A file = cli param
  128    %if A file # "" %start
  129     B file = ""; pr = "Against:  "
  130    %else
  131     A file = ""
  132     B file = ""
  133    %finish
  134  %finish
  135  
  136  A file <- left.right %while A file -> left.(" ").right
  137  B file <- left.right %while B file -> left.(" ").right
  138  
  139  File('A') = A file; File('B') = B file
  140  
  141  Prompt ("Old file: ")
  142  Rd line (A file) %while A file = ""
  143  Prompt (pr)
  144  Rd line (B file) %while B file = ""
  145  
  146  fname len = LENGTH(A file)
  147  %if fname len < LENGTH(B file) %then fname len = LENGTH(B file)
  148  %if fname len > 11 %then fname len = 11
  149  
  150  open input (A stream, A file)
  151  open input (B stream, B file)
  152  
  153  A file = "           ".A file
  154  B file = "           ".B file
  155  A file = SUBSTRING(A file, LENGTH(A file)-10, LENGTH(A file))
  156  B file = SUBSTRING(B file, LENGTH(B file)-10, LENGTH(B file))
  157  
  158  %cycle
  159     select input (A stream)
  160     refill (A, A num, A count)
  161     select input (B stream)
  162     refill (B, B num, B count)
  163     %exit %if A count <= 0 %and B count <= 0
  164     find match (A num, B num)
  165     %if A num # 1 %or B num # 1 %start
  166        print (A, A num-1, 'A')
  167        print (B, B num-1, 'B')
  168        Print string("---------------------".snl)
  169        %if A num > Max Lines %start
  170           Print string ("*** Mismatch too large")
  171           Newline
  172           %stop
  173        %finish
  174     %finish
  175     Base('A') = Base('A') + A num
  176     Base('B') = Base('B') + B num
  177     A count = A count - A num
  178     B count = B count - B num
  179  %repeat
  180  
  181  %end %of %program

  155 Statements compiled
