$a include=template_file $n$lm IMP Core Environment Standard $b2$lm Section 7: "Derived" I/O Procedures $a sectno=7; pageno=1 $a indent=0 $p 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. $a indent=1 $b4$v5$t0 * $t %predicate END OF INPUT$b0 $p 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. $b$v9$l0 %predicate End Of Input %from IMP %include Events %integer Dummy %on Input Ended %start %true %finish Dummy = Next Symbol %false %end $b0 $b4$v5$t0 * $t %routine READ ( %name N )$b0 $b$v12$l0 %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 $b0 $b Notes on the above definition of READ: $b$v5$l0 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. $b0 $b$v5$l0 Semi-formal grammar for %string case: string body = """", { ?any char?-"""" }, """" | { ?any char > 32? } ; read S format = white space, string body ; $b0 $b$v5 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. $b4$v5$t0 * $t %routine READ LINE ( _s_t_r_i_n_g(*) %name S ) $p 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. $b$v9$l0 %routine Read Line ( _s_t_r_i_n_g(*) %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 $b0 $b4$v5$t0 * $t %routine NEW LINE$b0 $p 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. $b$v3$l0 %routine New Line Print Symbol(NL) %end $b0 $b4$v5$t0 * $t %routine NEW LINES ( %integer N )$b0 $p 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. $b$v5$l0 %routine New Lines ( %integer N ) %while N > 0 %cycle N = N-1 New Line %repeat %end $b0 $b4$v5$t0 * $t %routine SKIP SYMBOL$b0 $b$v4$l0 %routine Skip Symbol %integer Dummy Read Symbol(Dummy) %end $b0 $b4$v5$t0 * $t %routine SPACE$b0 $b$v3$l0 %routine Space Print Symbol(32) %end $b0 $b4$v5$t0 * $t %routine SPACES ( %integer N )$b0 $b$v5$l0 %routine Spaces ( %integer N ) %while N > 0 %cycle N = N-1 Space %repeat %end $b0 $b4$v5$t0 * $t %routine PRINT STRING ( _s_t_r_i_n_g(255) S ) $b$v5$l0 %routine Print String ( _s_t_r_i_n_g(255) S ) %integer I %for I = 1, 1, Length(S) %cycle Print Symbol(Charno(S,I)) %repeat %end $b0 $b4$v5$t0 * $t %routine PRINT ( %long %real R, %integer A, B )$b0 $b$v3$l0 %routine Print ( %long %real R, %integer A, B ) Print String(RtoS(R,A,B)) %end $b0 $b4$v5$t0 * $t %routine PRINT FLOATING ( %long %real R, %integer A, B )$b0 $b$v3$l0 %routine Print Floating ( %long %real R, %integer A, B ) Print String(FtoS(R,A,B)) %end $b0 $b4$v5$t0 * $t %routine PRINT FL ( %long %real R, %integer A )$b0 $b$v3$l0 %routine Print Fl ( %long %real R, %integer A ) Print Floating(R,1,A) %end $b0 $b4$v5$t0 * $t %routine WRITE ( %integer N, Places )$b0 $b$v3$l0 %routine Write ( %integer N, Places ) Print String(ItoS(N,Places)) %end $b0