in
and
out
Input and output to stdin and stdout -- that is, from the keyboard or to
the screen -- are handled by a group of predefined queues. The simplest of
the group are in
and out
. in
can only
be a source and out
can only be a destination. Numerical input
and output is handled with attachments, as follows:
Q x in -> x x + 1 -> x x -> out
This will input a number, add one to it, and output it in numerical format. Note that at the end of this code snippet x is a null queue again (has no elements).
Character ASCII values can be input and output with the 'in
and 'out
predefined queues. 'in
reads a character
from stdin, and 'out
outputs a character to stdout, in much the
same way as before. They both convert to or from ASCII values, because queues
only store numbers.
So far we have only read one number or character at a time. But input and output would be tedious indeed if this were the only way. (Not that that would have stopped us from leaving it at that if it were in any way difficult to engineer longer input and output, however.) However, we can use the assignment operator rather than the attachment operators. Therefore, this code:
Q x x = 'in 'out = x
will read a string from stdin until return is pressed and output it to
stdout. Note that at the end of this code snippet x still contains the
string, as the assignment operator does not modify the source.
in
and out
are most often used with attachment,
while 'in
and 'out
are most often used with
assignment, but either can be used with either.
The double quotes " are the literal string operator. A literal string
such as "This is a string"
is equivalent to the queue
of the ASCII values of the characters in the string (which, for the example,
is a 16-element queue).
The predefined queues in, out, 'in, and 'out pretty much cover the keyboard and the screen (stdin and stdout). However, those are not the only sources of input and output that a program will want to utilize. Q-BAL provides variable input and output for those who want to use other sources of input and directions of output.
The Variable I/O system introduces five new predefined queues. They are
?
, in?
, out?
, 'in?
, and
'out?
. The first, represented by a single question mark, is a
queue of all the sources and destinations the programmer may want to use. It
begins the program as a null queue, and while it is a null queue the use of
the other four will cause an error to be generated. Variable I/O is
initialized by putting a number (usually defined by the .M directive to be a
descriptive word) into the queue ? as follows:
PRINTER -> ?
Assuming that earlier in the program there was a .M PRINTER x
statement, where x is whatever number the compiler/interpreter decides
represents the printer, this statement initializes the variable I/O queues.
The other four variable I/O queues function exactly like their standard I/O
counterparts except that they input from and output to only whatever area is
listed at the top of the ? queue.
You may have noticed that the last section was rather vague on what numbers represent which sources of input and destinations for output. This is because the representation is dependent on the compiler/interpreter. Luckily, the compiler/interpreter is required to provide include files (usually .inc or .qbi) which include the neccessary .M statements for common devices such as printers, mice, modems, etc. Also sure to be included are library functions which handle these devices, so that it is generally unneccessary to access them directly. (Don't worry, we'll get to functions in a bit.)