                         IMP Core Environment Standard

                      Section 7: "Derived" I/O Procedures

       The procedures in this section are all expressed as definitions in
    the IMP language.  In addition, all are defined in terms of procedures
    either in this or the previous sections of this document.

    *       predicate END OF INPUT

               This predicate is true if and only if examining the next
            symbol in the currently selected input stream would cause an
            input ended event (SIG0003; Input Ended) to be signalled.  The
            most common cause of this is that there is no next symbol
            available, implying that the input stream is positioned after
            the last available character of data.

                predicate End Of Input
                   from IMP include Events
                   integer Dummy
                   on Input Ended start
                      true
                   finish
                   Dummy = Next Symbol
                   false
                end

    *       routine READ ( name N )

                routine Read ( name X )
                   from IMP include TYPES {see section 11}
                   integer T = Type Of(X)

                   routine Skip White Space
                      Skip Symbol while Next Symbol <= 32
                   end

                   if T = String Type start
                      Skip White Space
                      if Next Symbol = '"' start
                         Skip Symbol
                         {read up to next '"'}
                      else
                         {read up until next symbol = white space}
                      finish
                      {assign or give signal if result is}
                      {too big for destination}
                   else if Its An Integer
                      Skip White Space
                      {parse integer value}
                      {assign or give signal if result is}
                      {too big for destination}
                   else if Its A Real

                      Skip White Space
                      {parse real value}
                      {assign or give signal if too big for destination}
                   else
                      {parameter error: cry for help}
                      signal 5, 3, T {check numbers later}
                   finish
                end

            Notes on the above definition of READ:

          1) skip white space AFTER check for legal parameter.
          2) signal guaranteed if bad parameter type NOT error.
          3) signal guaranteed if integer value valid but too big
             for destination.
          4) white space now excludes extended ASCII graphics set.

    Semi-formal grammar for string case:

       string body     = """", { ?any char?-"""" }, """" |
                               { ?any char > 32? } ;

       read S format   = white space, string body ;

            Grammar for integer case and real case would be the S TO I
            grammar and the S TO R grammar, with in each case the trailing
            white space removed.

    *       routine READ LINE ( string(*) name S )

               This procedure reads a text "line" from the currently
            selected input stream, up to and including its terminating NL
            character.  The parameter is a reference to a string which is
            built up with this sequence of characters as they are read in,
            implying that the parameter will contain a valid string even
            though the READ LINE procedure fails for some reason, for
            example because the end of the currently selected input stream
            has been reached.  Note that the terminating NL character is not
            included in the data assigned to the parameter.

                routine Read Line ( string(*) name S )
                   integer Sym
                   S = ""
                   cycle
                      Read Symbol(Sym)
                      return if Sym = NL
                      signal ... if Length(S) = Size Of(S)-1
                      S = S . To String(Sym)
                   repeat
                end

    *       routine NEW LINE

               As indicated in the definition below, this procedure is
            simply a shorthand for a call on the PRINT SYMBOL procedure with
            the predefined constant NL as parameter.  As mentioned in the
            previous section, the effect of this operation is defined by the
            stream facility associated with the currently selected output
            stream.  Commonly, for example on streams created using OPEN
            OUTPUT, this character is defined as indicating the end of a
            "line" of text output.  Other stream facilities, for example
            OPEN BINARY OUTPUT, will treat the character NL in the same way
            as any other character, that is simply as an 8-bit data item of
            a particular value.

                routine New Line
                   Print Symbol(NL)
                end

    *       routine NEW LINES ( integer N )

               This procedure causes a number 'N' of NL characters to be
            output to the currently selected output stream.  The effect of
            this, as with NEW LINE above, is dependent on the stream
            facility with which the output stream was created.  Note that,
            in the definition below, no action is taken if the value of N is
            less than or equal to 0.

                routine New Lines ( integer N )
                   while N > 0 cycle
                      N = N-1
                      New Line
                   repeat
                end

    *       routine SKIP SYMBOL

                routine Skip Symbol
                   integer Dummy
                   Read Symbol(Dummy)
                end

    *       routine SPACE

                routine Space
                   Print Symbol(32)
                end

    *       routine SPACES ( integer N )

                routine Spaces ( integer N )
                   while N > 0 cycle
                      N = N-1
                      Space
                   repeat
                end

    *       routine PRINT STRING ( string(255) S )

                routine Print String ( string(255) S )
                   integer I
                   for I = 1, 1, Length(S) cycle
                      Print Symbol(Charno(S,I))
                   repeat
                end

    *       routine PRINT ( long real R, integer A, B )

                routine Print ( long real R, integer A, B )
                   Print String(RtoS(R,A,B))
                end

    *       routine PRINT FLOATING ( long real R, integer A, B )

                routine Print Floating ( long real R, integer A, B )
                   Print String(FtoS(R,A,B))
                end

    *       routine PRINT FL ( long real R, integer A )

                routine Print Fl ( long real R, integer A )
                   Print Floating(R,1,A)
                end

    *       routine WRITE ( integer N, Places )

                routine Write ( integer N, Places )
                   Print String(ItoS(N,Places))
                end