         Edinburgh IMP77 Compiler - Version 8.4

    1  
    2  !Original grotty faulty version
    3  !%systemrealfn stor(%string(255)input)
    4  !  !reads a real from the string, assuming string starts with the real
    5  !  !(or blank spaces followed by a real)
    6  !  %integer sign=0,sym,pos=0
    7  !  %real value,exp
    8  !  !
    9  !  input=input."!";         !check that there is a finish character
   10  !  %cycle
   11  !    sym = charno(input,pos+1)
   12  !    %exit %if sym > ' '
   13  !    pos=pos+1
   14  !  %repeat
   15  !  %if sym = '-' %start
   16  !    sign = 1
   17  !    pos=pos+1;  sym = charno(input,pos+1)
   18  !  %finish
   19  !  value = 0
   20  !  %if sym # '.' %start
   21  !    %signal 6,5,pos %unless '0' <= sym <= '9';  !charno out of range
   22  !    %cycle
   23  !      value = value*10.0+(sym-'0')
   24  !      pos=pos+1;  sym = charno(input,pos+1)
   25  !    %repeat %until %not '0' <= sym <= '9'
   26  !  %finish
   27  !  %if sym = '.' %start
   28  !    exp = 10.0
   29  !    %cycle
   30  !      pos=pos+1;  sym = charno(input,pos+1)
   31  !      %exit %unless '0' <= sym <= '9'
   32  !      value = value+(sym-'0')/exp
   33  !      exp = exp*10.0
   34  !    %repeat
   35  !  %finish
   36  !  %if sym = '@' %start
   37  !    pos = pos+1
   38  !    sym = charno(input,pos+1); pos = pos+1
   39  !    value = value*10.0\(sym-'0')
   40  !  %finish
   41  !  value = -value %if sign # 0
   42  !  %result = value
   43  !%end;  !stor
   44  
   45  !Working version, courtesy of RMM (as if it wasn't obvious
   46  !from the grotesque 'aesthetic' source layout)
   47  
   48  %systemintegerfnspec stoi(%string(255) S)
   49  %system %real %function S To R (%string (255) Input)
   50    %integer Sign = 0,
   51+            Sym,
   52+            Pos = 1
   53    %long %real Value,
   54+               Exp
   55  
   56     %routine Next
   57        Pos = Pos + 1
   58        %if Pos > Length (Input) %start
   59           Sym = 0
   60        %else
   61           Sym = Char No (Input, Pos)
   62        %finish
   63     %end
   64  
   65     Sym = Char No (Input, Pos)
   66     %if Sym = '-' %start
   67        Sign = 1
   68        Next
   69     %finish
   70     Value = 0
   71     %if Sym # '.' %start
   72        %signal 6, 5, Pos %unless '0' <= Sym <= '9'           {Char No out of range}
   73        %cycle
   74           Value = Value*10.0 + (Sym - '0')
   75           Next
   76        %repeat %until %not '0' <= Sym <= '9'
   77     %finish
   78     %if Sym = '.' %start
   79        Exp = 10.0
   80        %cycle
   81           Next
   82           %exit %unless '0' <= Sym <= '9'
   83           Value = Value + (Sym - '0')/Exp
   84           Exp = Exp * 10.0
   85        %repeat
   86     %finish
   87     %if Sym = '@' %start
   88        Sym = S To I (Sub String (Input, Pos + 1, Length (Input)))
   89        Value = Value * 10.0\Sym
   90     %finish
   91     Value = -Value %if Sign # 0
   92     %result = Value
   93  %end {S To R}
   94  
   95  %systemrealfn ftor(%string(255)s)
   96    %result = stor(s)
   97  %end
   98  
   99  %endoffile

   45 Statements compiled
