next up previous contents
Next: The stack and Up: CHAPTER.3: PROCEDURES AND Previous: The DOT-notation for

Assignment as a two stage operation

The statement:

    2 + 2 -> x;
takes place in two main stages, viz:

    '2 + 2;'
then

    '-> x;'
First the arithmetical expression is evaluated and the result, 4, is left on the stack. Then the top element of the stack is removed and put into the variable x.

We can write a series of statements which put things on the stack followed by a series which remove than and put them in variables, for example:

    2; 3; -> x; -> y;
These four statements do the following:

    (1) Put 2 on the stack
    (2) Put 3 on the stack
    (3) Remove the top element (ie 3) and put it in the variable x
    (4) Remove the top element (ie 2) and put it in the variable y
So, the following will swap the values of x and y.

    x; y; -> x; -> y;
Expressions may be separated by commas, and assignments need no separators, so this can be abbreviated to:

    x, y -> x -> y;
Notice that `-> x' removes the top item from the stack, and assigns it to x, whereas the reverse operation:

    x;
Just COPIES the value of x onto the stack, leaving x unchanged. We can demonstrate this difference as follows.

    vars x;
    77, 88, 99;     ;;; put three things on the stack
    -> x;           ;;; take one off
    =>
    ** 77 88        ;;; two things left

    x; x; x; =>     ;;; copy value of x three times onto stack
    ** 99 99 99


Aaron Sloman
Fri Jan 2 03:17:44 GMT 1998