7.2.5 Creating new stacks
The description of multiple stack operations in this section, is
not part of standard REXX. Thus, this section describes a de facto
standard and you may find that few implementations support these
operations.
Just as the operations described above let the REXX programmer use
multiple buffers within one stack, there exists another set of
operations which let the programmer create multiple stacks. There
is really nothing fancy about this, except that a command will
swap the stack the interpreter correctly uses with another stack.
To the interpreter this is really equivalent to a situation where
a command empties the current stack, and sets up a new stack. When
one stack is empty, and the REXX program tries to read from the
stack, the request will not “overflow” to the previous stack (as
requests to an empty buffer “overflows” to the previous buffer).
Thus, the use of multiple stacks has even less direct impact on
REXX interpreters than multiple buffers.
Here, it is instructive to list the commands operating multiple
stacks that exists. This list has been taken from the MVS
environment, according to [REXXSAA].
[DELSTACK]
Is used to remove the most currently stack, and make the most
recent of the saved stacks the current stack. When there are
no saved stacks, the current stack is emptied.
[NEWSTACK]
Creates a new stack, which becomes the current stack. The old
current stack is put on the top of the list of saved stacks,
and can be retrieved as the current stack by a subsequent
DELSTACK.
[QBUF]
Counts the number of buffers in the current stack, and
returns that number as the return value. A REXX program
starting this command can retrieve this value as the special
variable RC.
[QELEM]
Counts the number of strings (i.e. elements) in the current
stack, and returns that value as the return value of the
command. This value can be retrieved in REXX as the special
variable RC. This operation is equivalent to the QUEUED()
built-in function in REXX; it has been probably included for
the benefit of other script languages that have less
functionality than REXX.
[QSTACK]
Counts the number of stacks (including the current stack) and
returns the value as the return value from the command. This
number can be retrieved in REXX as the special variable RC.
One can regard multiple buffers and stacks as two ways of
insulating the stack; where multiple stacks are a deeper and more
insulating method than buffers. Note that each stack can contain
multiple buffers, while a buffer can not contain any stacks. The
term “hard buffers” has been used about multiple stacks, as
opposed to normal buffers, which are sometimes called “soft
buffers”.
Also note that neither multiple stacks nor buffers are part of
standard REXX, so you might come across implementations that
support only multiple stacks, only buffers, or even none of them.
Example: Counting the number of buffers
In order to count the number of buffers on the stack, the
following method can be used (Regina syntax has been used for
buffer handling). This method is equivalent to the QBUF command
described above.
buffers = makebuf() - 1
call dropbuf
This will store the number of buffers in the stack in the variable
buffers. However, just as for the other examples using buffers,
this example also suffers from the fact that buffer handling is
fairly non-standard. Thus, you will have to adapt the code to
whatever system you want to use.
PREV NEXT