The Multi-Purpose Stack / Array / Queue Type

The stack variable type is the only type of variable in SLUDGE which can hold more than one element. It can be used as a stack (push elements on using pushToStack and then use popFromStack to pop them off the same end), a queue (enqueue elements, dequeue them off the opposite end) or a straightforward array (which can be indexed using square brackets). With the implementation used in SLUDGE, dequeue and popFromStack are identical.

Throughout this documentation, they are referred to as stacks.

Take, for example, the following code:

var stuff = newStack ("Hello?", "Hey!", "Bye then.");
# stuff now contains "Hello?", "Hey!", "Bye then."

pushToStack (stuff, "Aarrgghh!");
# stuff now contains "Aarrgghh!", "Hello?", "Hey!", "Bye then."

enqueue (stuff, "Er...?");
# stuff now contains "Aarrgghh!", "Hello?", "Hey!", "Bye then.", "Er...?"

var a = popFromStack (stuff);
# stuff now contains "Hello?", "Hey!", "Bye then.", "Er...?"
# a contains "Aarrgghh!"

var b = dequeue (stuff);
# stuff now contains "Hey!", "Bye then.", "Er...?"
# b contains "Hello?"

var c = stuff[1];
# stuff still contains "Hey!", "Bye then.", "Er...?"
# c contains "Bye then."

deleteFromStack (stuff, c);
# stuff now contains "Hey!", "Er...?"

When treating a stack as a Boolean, it is considered to be TRUE if it is nonempty and FALSE if it is empty. Therefore you can use the following structure to loop until a stack is empty (this example makes the ego character say every element of the stack in order):

while (myStack) say (ego, popFromStack (myStack));

A good example of using these stacks is for implementation of an inventory. Some built-in functions which return more than one element (getMatchingFiles and wait for example) also return information using the stack variable type.

See also: