MUF (programming Language) - The MUF Stack

The MUF Stack

MUF programs work by manipulating data stored in a LIFO stack. Some programming languages include a primitive for explicitly putting data on the stack, usually called PUSH. In MUF, this operation is implicit: data included in source code or returned from a word or program automatically goes on the stack. The top item on the stack is removed with the POP primitive.

The following code:

"foo" "bar" POP POP

...would produce the following stack trace (the text within the parentheses represents the stack; the left end is the "bottom" of the stack, the right is the "top"):

"foo" ("foo") "bar" ("foo", "bar") POP ("foo") POP

This code begins with an empty stack. The statement "foo" (which is some data, of the type string) causes the string to be placed on the stack. The stack now consists of this one datum. The following statement "bar" (more data, another string) causes "bar" to be placed on the stack as well. "bar" is the top element. "foo" is below "bar". The statement POP causes the top element of the stack to be removed: "bar" is popped off the stack, into oblivion; the stack now once again consists of the single datum "foo". The second POP removes "foo" from the stack. The stack is now once again empty. A third POP statement at this point would cause a "stack underflow", and the program would crash.

An understanding of the stack is absolutely necessary for MUF programming, since virtually all primitives and user-defined words require data of a certain type to be on the stack, in the correct order, in order to perform their functions. For example, the stack effect note for the NOTIFY primitive is ( d s -- ). That is, the primitive requires a string to be on top of the stack, with a dbref immediately below that. If player Alice had a dbref of #99, then the code:

#99 "Hello there!" notify

...would output the string "Hello there!" to Alice's screen. After this happens, these two data would be removed from the stack: primitives and operators "use up" their input data. If the program did not have a dbref and a string on the top of the stack at runtime, in this order, the program would crash.

MUF includes a number of primitives that examine and manipulate the stack contents, including DUP, POP, SWAP, OVER, ROT, ROTATE, PICK, PUT, and DEPTH.

Read more about this topic:  MUF (programming Language)

Famous quotes containing the word stack:

    “Farewell to barn and stack and tree,
    Farewell to Severn shore.
    Terence, look your last at me,
    For I come home no more.
    —A.E. (Alfred Edward)